Example

Custom interpreter to move a box (for Shuk's challenge #1)

3

SP4CEBAR 2024-04-05 00:15

This custom interpreter can move the box!

W=$8010
DO
A=PEEKW(W)+2^16
G=PEEKW(W+3)+2^16
POKE A,PEEK(A)+PEEK(W+2)*(PEEK(G) AND 1)
ROR G,1
W=PEEK(W+5)+$8010
IF W=$8028 THEN WAIT 1
LOOP
#2:C
FFFFFFFFFFFFFFFF7F7F7FFFFEB686FE
20FFFA008006
76FF0100800C
00FE50008012
01FE50008018
01FEFF70FF1E
01FE0170FF24
00FEFF70FF2A
00FE0170FF18

These 8 instructions occupy 6 bytes each.

W holds the address in memory of the current instruction Each instruction has 4 parameters:

  1. A (found at W+0) is the address of the memory location to modify
  2. (found at W+2) is the value to add to the value at memory location A. You can get the effect of negative numbers (subtracting) within this byte using $100 minus this value as a value.
  3. G (found at W+3) is the address of either the user input register or it points to a sequence of eight 1's to neutralize any effects. And have parameter 2's value be THE value.
  4. (found at W+5) contains the next instruction's address with $8010 subtracted from it so that this address information fits in a single byte. This allows it to run a bit of setup code, and then run a loop

What do the instructions do?

  1. Turn both backgrounds off, and go to 2.
  2. Turn the gamepad on, and go to 3.
  3. Center the sprite's X coordinate, go to 4.
  4. Center the sprite's Y coordinate, go to 5.
  5. Step in negative Y if the up button is pressed, go to 6.
  6. Step in positive Y if the down button is pressed, go to 7.
  7. Step in negative X if the left button is pressed, go to 8.
  8. Step in positive X if the right button is pressed, go to 5.

Detailed description

  1. Turn both backgrounds off. This could be done with NX commands or with "POKE $FF20, 1", but instead, it looks like this: "20FF FA 0080 06". This translates to modifying the value at $FF20, adding $FA (which equates to -6) to it, $8000 can be ignored, and at $8010+6 = $8016 is the next instruction
  2. Turn the gamepad on. This could be done with "POKE $FF76, 1", but it looks like this: 76FF 01 0080 0C This means: add 1 to the value at $FF76, and at $801C is the next instruction
  3. Center the sprite's X coordinate. Instead of "POKE $FE00, 80", it's: 00FE 50 0080 12. This means: add 5 to the value at $FE00, then go to $8022.
  4. Center the sprite's Y coordinate. "POKE $FE01, 80" looks like this "01FE 50 0080 18". Which is very similar to the previous instruction.
  5. Step in negative Y if the up button is pressed looks like "01FE FF 70FF 1E". If the least significant bit of the gamepad register byte at $FF70 is on then $FF (equivalent to -1) is added to the sprite's Y position at $FE01.
  6. Step in positive Y if the down button is pressed. "01FE 01 70FF 24" is similar to the previous but it adds 1 instead of subtracting 1.
  7. Step in negative X if the left button is pressed. "00FE FF 70FF 2A" is similar to 6 but it modifies the sprite's X register instead.
  8. Step in positive X if the right button is pressed. "00FE 01 70FF 18" is similar to the previous one but adds 1 instead of subtracting one. The next instruction is at $8028 which is the fifth operation.

Side note

Location $8000 (the character image data) contains a byte with the data $FF, the code uses this as a dummy byte, to keep the bit shifter and the logical AND busy when they aren't needed without those operations modifying anything.


SP4CEBAR 2024-04-05 00:19 (Edited)

In theory, this xor interpreter should also be quite capable:
'DO
'A=PEEKW(W)+2^16
'G=PEEKW(W+2)+2^16
'POKE A,PEEK(A) XOR PEEK(G)
W=PEEK(W+4)+$8010
'LOOP

'THIS IMPLEMENTATION OF XOR SHOULD BE ABLE TO
'0000 SELECT BITS TO KEEP
'1111 SELECT BITS TO INVERT
'CLEAR A MEMORY LOCATION, BY USING THE SAME ADDRESS FOR BOTH PARAMETERS
'WRITE DATA TO AN EMPTY MEMORY LOCATION
'WRITE A VALUE TO ANY PREDICTABLE MEMORY LOCATION


Shuk 2024-04-10 03:29

Wow that’s cool but I used Ctrl C + V to make the box move. But I have to credit you though for making something that makes sense good job! :D


SP4CEBAR 2024-04-10 12:34

Thank you! I just had to explore whether or not this would work.


Log in to reply.