Search code examples
cpucpu-architecture

Microcode emulator in Python


I am trying to deal with my computer architecture lab. The task is to create a model of processor in Python. One of the aspects is using microcode to execute machine instruction. As I understand, i should create a storage to microcode commands and somehow map machine instruction to microcode. I have an example of Control Unit for hardwired architecture for this task:

scheme of the control unit

Nevertheless, I don't understand how to update it to use the microcode.

What components need I add to the scheme to implement microcode and how to emulate in on Python?

I suggest to create a dictionary to store the mapping from machine instruction to microcode programs to emulate a microcode of real process. But I don't know if it is good way to implement this and either do I do right thing?


Solution

  • Microcode is generally useful if you are making a multicycle processor; the microcode program tells the processor what to do each cycle.

    Your diagram includes a Step Counter, which implies a multicycle design.

    (Let's differentiate between the instruction set you're emulating overall, calling those instructions machine code instructions, while the microcode also has instructions, we'll call microcode instructions.)

    The operation goes something like this: the Instruction Decoder takes a machine code instruction, and a step number (e.g. starting at 0) as input and looks up a microcode instruction to execute, while also computing what the next step is (i.e. some non-zero Step Counter value, if current instruction needs more cycles, and eventually back to, say, 0, to run a next machine code instruction.

    The design and form of the microcode instructions can be vastly different from the machine code instructions — it can be as wide as needed, having lots of 1-bit control signals, while also encoding a next step for the Step Counter, and perhaps Sel-Next and other Signals.


    What components need I add to the scheme to implement microcode and how to emulate in on Python?

    You'll need a microcode instruction store.  The way hardware would do it would be as an array of microcode instruction words.  The array is accessed by index as you might expect — this is pretty normal for hardware.

    I suggest to create a dictionary to store the mapping from machine instruction to microcode programs to emulate a microcode of real process. But I don't know if it is good way to implement this and either do I do right thing?

    You can lookup microcode instructions from your microcode instruction store, using Python dictionary, but far more expected would be to make your microcode instruction store a simple array as would be done in actual hardware.  Simple array lookup by some numeric index is the approach to reading an instruction from such store.

    For starters, I would focus on the microcode instruction capabilities and binary format, writing the microcode for just one of the machine code instructions in binary, by hand, and entered into a lookup table that is an array of microcode instructions.

    There will be multiple microcode instructions for a single machine code instruction and suggest where to focus, for now, is on handling a single machine code instruction that requires multiple microcode instructions (cycles).

    Some questions to ponder:

    • How to access the first "step 0" microcode instruction for a given machine code instruction?  This goes to using the machine code instruction to identify an initial entry in the microcode array, by finding an array index in the machine code instruction, e.g. using its opcode or other.

    • Where to store and how to access additional/secondary microcode instructions for a given machine code instruction.  This goes to using the Step Control to indicate access to secondary microcode instructions.

    Once you've figured out how to use a microcode instruction store, then use the microcode instruction to send signals to the rest of the processor, while also choosing the next step (another microcode instruction in continuation of current instruction, or, next instruction starting over at step 0).