Thursday, 12 December 2019

Elektor processor dissection and reconstruction

To understand how a machine works you can, perhaps, take it apart and put it back together again.  Whilst Elektor exp5 is great to see and you can look at the code to get a general idea of its construction, something more is needed to become familiar and understand it better.[October 2019]

Dissection


Stage 1
The processor has a debug serial input allowing you to type in single character commands and get text output.  I checked that I could add commands myself to look at the processor

Stage 2
I removed unwanted peripherals from Top.v, the top level function:  DAC, accelerometer, SPI.
I then took out UART processing as this requires a lot of code.  Subsequent stages use LEDs for output.

Stage 3
I slowed down the CPU to 16Hz (400 ticks) so that LED changes appear in real time - ie without needing to insert delays.
At the end of this stage Top.v is small but we still have a working CPU running C programs and producing output.

Stage 4
Firstly we remove the code for LED output from the processor and use LEDs for debugging output instead.
We can see, in Icarus each opcode being processed.
We can remove the special states div1, div2, readmemat3 without affecting processing.
Finally we can remove all the opcodes from the CPU except for jumps.  A test program now loops but other codes are treated as NOP.
The end product is a processor with a clock, program counter and jump instructions.

Construction

1 Clock
We start a new MAX1000 project and add ALTPLL clock and LPM_COUNTER IP.  In a skeleton top level program Board.v we incorporate these components and output appropriate bits from the counter to LEDs so that we can see binary values being incremented.
In Quartus we need to add LED, clock pins and timing (SDC) information.

2 States
Add a skeleton Cpu.v which just switches between the states fetch, decode, readmem etc.
Add Testbench.v so that we can run tests in icarus first.
Add USR_BTN which stops the processor when pressed, we can use this for single stepping.
Use LEDs to see the processor cycles through instructions and states.

3,4 Codemem, datamem
We implement JMP and NOP instructions.
We use a program copied from stage 4 above and can see the program counter increasing until the JMP and then looping round.
We can now implement instructions ST (store), LD (load) to access memory and LDIND, STIND.  We setup a stack at the end of datamem  and implement CALL, RET. Add stack operations e.g. PUSHR0, ADDSP,....
We also add HALT to finish the program.

5 Arithmetic
Add arithmetic, logic and comparison instructions:
IADD
XOR, OR, AND, COM, NEG, MUL
CMPEQ/NE/LT/LE/GT/GE, CMPULT/ULE/UGT/UGE
Also add conditional jumping
A few more optimiser instructions (added to C by the author to decrease number of instructions) were also added.
At this stage it is possible to compile and run a c program containing code like result=i+i;

6 Output
All peripheral output is directed by the OUTA instruction. Initially we implement channel 5 to set LED values.  We then add channels 9 (output character), 8 set output speed and input channel 5 (determine bits left to transmit).  The CPU needs code to process the channels and Top.v needs corresponding details for physical hardware processing.
We have to add TXuart.v to do the bit-banging.
We can then run compiled programs including the C putchar() function.

7 Input, debug and load
Add RXuart.v module
Add, irq processing to CPU
Add bootload/standalone parameter to switch program load.
This was quite an extensive step at the end of which we had most C instructions available to us.
Quite a lot of code 

8 RTC, DAC, LIS
Finally we add other peripheral functions to the processor so we are confident we have a complete working system.

Conclusion
This was a time-consuming and very worthwhile exercise which allowed me to understand how the Elektor-provided verilog code creates an 8080 processor.  I kept variable names and formatted code the same so that the final result doesn't look radically different from the starting version but I understand content a lot better.

No comments:

Post a Comment