Close

Conditional Jumps, Comparisons, and Output

A project log for 8-Bit CPU in The Ultimate Nerd Game

An attempt at making an 8 bit CPU in TUNG (The Ultimate Nerd Game)

Buzz PendarvisBuzz Pendarvis 06/07/2020 at 04:320 Comments

Yesterday, I added a zero flag, a carry flag, a negative flag, and the compliments of all three. Before today, I didn't have anything that used these flags. Today, I added instructions to jump if certain flags were set and instructions to compare numbers to the A register.

In order to test these instructions, I made a small program to count to four and then halt.

In assembly, this program looks something like this:

LDA 0       ;Reset A
Loop:       ;Counting loop
STA 0xFF    ;Write to display
ADD 0x01    ;Adds 1 to A
CMP 0x05    ;Checks if A=5
JMP NZ Loop ;Loops if A!=5
HLT         ;Stops if A=5

Because it never displays after A=5, the program only counts to four.

The instruction STA, which is used in the demo, writes to a location in memory. Although I haven't added the instructions for locations pointed to by registers, they will be implemented soon. I will also be adding instructions for reading from memory, which will allow for a program to take input and use RAM.

I also fixed a bug in the hardware that caused the ADD instruction to act extremely weirdly. Instead of adding the numbers it was supposed to, it would often add the wrong numbers, or the right numbers at the wrong time. In fixing this, I was also able to fix some problems I was having with the flags.

Finally, I added one new instruction that serves no purpose whatsoever. HCF, or Halt and Catch Fire, permanently disconnects the clock from the CPU. There is no way outside of the CPU to recover from an HCF. The only way to fix the CPU after an HCF is to go into the CPU and press a button to reconnect the clock.

Here's a current list of all opcodes and instructions:

0x00 - NOP
0x01 - LDA    (immediate)
0x02 - LDB    (immediate)
0x03 - LDC    (immediate)
0x04 - LDD    (immediate)
0x05 - LDE    (immediate)
0x06 - JMP    (immediate)

0x08 - ADD B
0x09 - ADD C
0x0A - ADD D
0x0B - ADD E
0x0C - ADD    (immediate)

0x10 - CMP    (immediate)
0x11 - CMP A
0x12 - CMP B
0x13 - CMP C
0x14 - CMP D
0x15 - CMP E
0x16 - JMP Z  (immediate)
0x17 - JMP NZ (immediate)

0x20 - STA    (immediate)

0x26 - JMP C  (immediate)
0x27 - JMP NC (immediate)

0x36 - JMP P  (immediate)
0x37 - JMP N  (immediate)

0xFE - HCF
0xFF - HLT

And a current image:

Sadly, I may not be able to work on the CPU tomorrow or the next day, but I will come back to it as soon as I can and continue posting.

Discussions