!pr1
Solutions to Adam Levin's Painting Puzzle....Adam Levin, et al

The puzzle, published last month, was to write a program which would fill all RAM from $0000 through $BFFF with the same value.  What value is your choice.

The listing of my solution follows.  It executes at $9966, which is inside the middle DOS buffer.  To get it there, you can BLOAD or BRUN it.  A few seconds after the screen fill's up with "Y" characters, the program has completely filled RAM from $0000 through $BFFD with $99.

Lines 1080-1200 fill all the RAM not occupied by my program (addresses $0000-$98FF and $99C8-$BFFF) with $99.  I first fill the RAM from $99C8 up, and then from $0000 up through $98FF.  You have to forgive the self-modifying code in a puzzle solution like this.

Lines 1210-1280 store a NOP and a JMP $0000 at the end of RAM.  Lines 1320-1350 store $99 into $9900-$9999.  It's getting hot in here!

Lines 1390-1590 get executed more than once.  The first time, they store $99 into $999B-$99A3, and $99A5.  By this time every byte from $0000 through $99A5 is set $99.  All those bytes can be executed as "STA $9999,Y" instructions, and the JMP $0000 we placed at the end of RAM will do just that.  When we get back up to line 1430, at $99A9, we start moving Y again and store $99 into $99A6-99AC and $99AE.  It progressively keeps covering itself up, and eventually it is all gone:

       999B
       999C
       999D 99A6
       999E 99A7 99AF
       999F 99A8 99B0
       99A0 99A9 99B1 99B7
       99A1 99AA 99B2 99B8
       99A2 99AB 99B3 99B9 99BD
       99A3 99AC 99B4 99BA 99BE 99C1
       ...
       99A5 99A3 99B6 99BC 99C0 99C3 99C5 99C6


<<<<code here >>>.


Bob S-C's solution

The program loads at $800, but actually executes at $100.  Lines 1030-1080 move the filler program down to $100 and jump to it.  This solution fills all of RAM from $0000-BFFF with $48, which is a "PHA" instruction.

To keep from running off the end of RAM into the I/O space, I took advantage of the fact that the keyboard register can be read at both $C000 and $C001.  Lines 1140-1160 wait until you type a zero key ("0").  The ASCII code for "0" is $B0.  Two $B0 values in a row at $C000 and $C001 will dis-assemble as a BCS to $BFB2.  Hence my solution finishes with an infinite loop running from $BFB2 to $C001.

Lines 1170-1290 fill RAM from $200-$BFFF with $48's, which are "PHA" opcodes.  Lines 1300-1330 do the same with page zero.

Line 1350 jumps to $200, which means that the PHA opcodes start being executed.  Since the stack is only 256 bytes long, and since the stack pointer wraps around, by the time the PHA at $2FF has executed all of page 1 will have been filled with $48.  Since carry is set, when execution reaches $C000 the processor will go into that infinite loop I mentioned above.


<<<<code here>>>>

David Johnson's solution

My solution uses the power of the 65802.  There was no restriction to the 6502 mentioned in the puzzle last month.  All 49152 locations of motherboard RAM are filled with $DB, which happens to be the opcode value for the "STP" opcode.  STP means "stop the processor", so once all RAM is filled it quits!

I use the MVP instruction to do the actual filling.  The MVP instruction is located at $0000.  I first put $DB into $BFFF.  Then I set up the registers so that MVP will copy $BFFF into $BFFE, then $BFFE into $BFFD, and so on down to copying $0001 into $0000.  By this time the MVP runs out, and the processor executes the STP opcode at $0003.

The 2nd and 3rd bytes of the MVP opcode specify which 64K memory banks to use; on a 65802 these don't do anything, because the bank addresses don't get out of the chip.  On a 65816 my program won't work correctly, because the bank bytes will be changed at the end.  First the Source bank address will be changed, so that a byte will be copied from $DB.0002 into $00.0001.  Now the Destination Bank Address is changed, to we don't know what:  we will finally copy $DB.0001 into $xx.0000.  That last byte-move could be catastrophic (who knows, since we don't have any 65816-based systems yet?).  Anyway, my program works fine in an Apple equipped with a 65802.
