An open API service indexing awesome lists of open source software.

https://github.com/coldnew/4bit-cpu-for-times-table

An 4bit-cpu to calculate times table
https://github.com/coldnew/4bit-cpu-for-times-table

Last synced: about 1 month ago
JSON representation

An 4bit-cpu to calculate times table

Awesome Lists containing this project

README

          

#+TITLE: A 4bit CPU for print times table

# Badge
[[https://travis-ci.org/coldnew/4bit-cpu-for-times-table][https://travis-ci.org/coldnew/4bit-cpu-for-times-table.svg?branch=master]]

In c, we prints the times table by following code:

#+BEGIN_SRC c
#include

int main(int argc, char *argv[])
{

for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
printf("%d x %d = %d\n", i, j, i * j);
}
printf("\n");
}

return 0;
}
#+END_SRC

How do you implement a 4bit CPU for calculate times table ?

** Registers

| Register | Description |
|----------+-------------------------|
| R0 | tmp data |
| R1 | tmp data |
| R2 | store ALU result (High) |
| R3 | store ALU result (Low) |
| PC_H | Program Counter (High) |
| PC_L | Program Counter (Low) |
| Z | Zero flag |

** Instructions

| cmd | nibble | ASM | Description |
|-----+--------+-------------------+-------------------------------------------------|
| 0 | 1 | sys_exit | Exit application |
| 1 | 1 | sys_write | print "R1 x R2 = Result" |
| 2 | 2 | inc | Rx += 1 |
| 3 | 3 | mul | Calculate Rx x Rx, store result to R2 and R3 |
| 4 | 3 | cmp | Compare Rx and val, set Z = 1 if rx >= val |
| 5 | 3 | blt | Jump to address if Z = 1, clear flag after done |
| 6 | 3 | mov | Rx = val |

** Assembly

#+BEGIN_SRC asm
_init:
mov r0 1 ; i = 1

_for_i:
mov r1 1 ; j = 1

_for_j:
mul r0 r1 ; i * j
sys_write ; print("R1 x R2 = Result")
inc r1 ; j++
cmp r1 10 ; if (j < 10)
blt _for_j ; goto _for_j

inc r0 ; i++
cmp r0 10 ; if (i < 10)
blt _for_i ; goto _for_i

sys_exit ; Exit application
#+END_SRC