or: How I Learned to Stop Worrying and Love the nested function
So, let me set the stage:
My code is all hosted on a NAS-box for safekeeping, I access this NAS-box
using a variety of file-share methods.
Let's say I want to access this using the command line. Well, simply put,
you can't. At least, not with CMD.EXE.
So how does one access these files?
PowerShell. Now, I'm a bash person, so PS is very scary for me to use for
the first time in forever.
Ah well, so I set up a powershell prompt pointed at the root directory of
my code-base and get to work. Python works, VS works, time for the main
course.
So, how do we go from this:
To this:
Well, with the power of trial and error. I wish I was joking when I say this, but it took forever just to make this thing translate 1 line, let alone 10 of them.
So, how does it work? To explain that, ill have to explain how each line is translated, and work our way up from there.
Imagine the line "A STA B", This utilizes all 3 possible parts of an instruction. These parts are as follows.
When we're doing that translation part, we
need to consider the labels when translating a line.
So, we can easily figure out the first step of our process; Gather the
labels and keep them safe.
However, we don't do this until AFTER translating the opcode. Don't ask
why, that's just how I programmed it.
Next, we need to consider the opcode, every opcode translated into a
different number, from 0 to 9. Many people may not be able to agree on the
source opcodes, so we need to make an universal solution.
My solution? A huge table that translates from opcode to instruction. See
below:
So, we can translate a single line, but what about the operand and labels, so, next we go through each line and find the operands. If an operand equals a label, we substitute in the line number. Simple really.
Finally, we patch up our file and write it to disk; a file is born!
This is it, the main bit, we now need to run the file and somehow have it do things. Enter, LMCvm.py.
This absolute whale of a file contains (in essence) 2 things, a "VM" class with functions for loading a file into memory, and a function that does the FDE cycle that a real computer would.
So, to run a file, simple, load it into the VM's memory, and run the "cycleVM(...)" function repeatedly until the program halts... and it works! Amazing. I sadly don't have much to show here, but....
Curses is pretty well known right? Right? Well, after making my program do the thing i wanted it to do, I aimed higher.
Enter: LMCgui.py
Yes, I made my program into a Command-line GUI program. No, I did not enjoy it.
400+ lines of code, Countless hours wasted. Mostly feature-complete. Completely painful.
Probably has a memory leak or 50, probably....
I had made a monster, one which ate up too much of my life.
Of course, I wasn't done. The fun thing about LMC is that they left the "4" instruction free to use. And so, it began...
So, what did I do?
A lot.
A LOT
Too much. What you witness before you is the attempt at expanding a children's education toy into a computer, with memory pages and registers galore.
Does it work? Yes. Will I keep working on it? No.
I should never be allowed to touch things like this ever again