Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Writing an emulator for a slightly unusual abstract FSM

Name: Anonymous 2016-05-12 15:20

what would be a relatively hassle-free way of writing an elegant, intuitive code that emulates a finite state machine (no jumps or conditionals) with a self-modifying instruction set (or maybe just two instruction sets: one for modifying data, other for modifying the first instruction set)? I don't care about performance right now (I doubt it will get to the stage when I need to care about that but if it does, some fuckery with C function pointers will be inevitable; right now I care about readability and being able to prototype quickly) so functional is OK but I'm thinking of instructions having four-bit length and I'm not sure how good purefuncs are at handling that

Name: Anonymous 2016-05-13 19:36

>>15
If the instruction set is closed under operations, then have a fixed length instruction word with a fixed number of argument bits and instruction bits. For 16 instructions, make the first four correspond to the instruction, and the last twelve correspond to an address, for example. Twelve address bits mean you can get 4096 words on the input tape, which should be plenty for whatever you are trying to do, and not have to worry about addressing outside of memory. Store each instruction in a structure containing the 16 bit word and a union with bitfields. Then iterate over the input tape with a big switch statement on the instruction bits of that union field. To plan out the instructions, try writing simple programs to get a feel for what you like. For example, look at the program I made with list operations in https://bbs.progrider.org/prog/read/1444460614. That uses loops, thus conditionals and jumps, but it could be unrolled easily to make fibs.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List