MightyPirates / TIS-3D

TIS-100 inspired low-tech computing in Minecraft.
https://www.curseforge.com/minecraft/mc-mods/tis-3d
Other
108 stars 35 forks source link

Expand the possibilities with LAST #57

Closed thecodewarrior closed 6 years ago

thecodewarrior commented 7 years ago

I was thinking about the LAST port, and was wondering if it could be made even more useful, so I decided to take a stab at it to see how I do.

I changed the LAST port to instead be called DYN because it's more general than LAST was, and added a DYNP register.

DYN dynamically points to one of [NIL, UP, RIGHT, DOWN, LEFT]. It is automatically set with the port the last successful ANY operation happened on, just like LAST, but in addition to that the program can read and manipulate it using the DYNP register. The DYNP mapping table thingy is as follows: {-1=NIL, 0=UP, 1=RIGHT, 2=DOWN, 3=LEFT}

If you set DYNP to a value less than -1 it will be set to -1.

If you set DYNP to a value greater than 3 it will be wrapped (using modulus) to within 0-3, allowing you to rotate by simply adding a number to DYNP.

Here's the test program I used:

MOV 65 ANY
MOV DYNP ACC
ADD 2
MOV ACC DYNP
MOV 66 DYN

That program writes 65 to wherever it can (I placed a stack next to it) and then reads what side was written to using DYNP, rotates it 180º, writes that value back to DYNP, and then writes 66 to that side. Basically it writes to any side, then writes exclusively to the opposite side.

It would be incredibly useful for "asynchronous" code because you could wait for any input, and execute different code depending on the side input.

One downside to this, or possibly upside, is that it grants you an extra 2.5 bits of memory if you aren't using ANY or DYN, but I don't think that would cause much of a balance problem. I don't know the design of the mod all that well though. Having one more place to store values could be a deal breaker.

Vexatos commented 7 years ago

LAST conforms to TIS-100 specifications.

thecodewarrior commented 7 years ago

Are you against possibly adding a ROT command (don't like the name, but all I could come up with on the spot), or some other similar command, that can rotate the LAST value so you can do this kind of stuff? That's all that's really needed. To cross over signals you just do this:

MOV ANY ACC
ROT 2
MOV ACC LAST
fnuecke commented 7 years ago

That's an interesting approach to making programs more reusable! I've actually been pondering adding a simple #define preprocessor macro with that goal (so you could #define PORT_IN LEFT and then do MOV PORT_IN ACC e.g.), but couldn't come up with a nice way of keeping that reversible (for copying back to the code bible) without keeping the "raw" input around, which is... meh, as I'd like to have the macro processor bound to the code bible.

I would like to avoid modifying opcodes from the original specification, but I'm pretty open to adding new opcodes. One to modify LAST... hmm, if it's write-only, why not. Though some other name would be required, yeah, since ROT sounds way too shift with wrap. Though I can't think of something nice right away, either.

esotericist commented 6 years ago

I know I am very, very late to this discussion (I somehow managed to miss this PR for over a year), but maybe for the opcode name consider TURN? If you're thinking of the LAST register in terms of the physical directions, and visualize it as an imaginary knob pointed in a specific direction, it is the kind of thing someone might have come up with in the hypothetical alternate universe TIS-100 comes from, and in my opinion it doesn't really collide significantly with related concepts.

fnuecke commented 6 years ago

All right, added both support for defines (e5bb9ebe6eea13e3e0ff989301faa46dddc2b098) as well as two new instructions to manipulate LAST, RRLAST and RLLAST, rotating the value one to the right or the left, respectively, unless it is NIL in which case it does nothing (6e3b48312d21243f97b547d0a3dbec7e7130b5a9). Yes, it needs two ops to flip LAST by 180 degrees, but I'm fine with that given how much simpler this is on all ends.