-
Notifications
You must be signed in to change notification settings - Fork 6
init commit Comp architect Toua #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
097125f
6fb34ac
0514689
57b464c
bcd0518
ac04506
bb80a55
b6b9e54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,18 @@ | |
| /** | ||
| * Load the binary bytes from a .ls8 source file into a RAM array | ||
| */ | ||
|
|
||
| unsigned char cpu_ram_read(struct cpu *cpu, unsigned char mem) | ||
| { | ||
| return cpu->ram[mem]; | ||
| } | ||
|
|
||
| void cpu_ram_write(struct cpu *cpu, unsigned char mem, unsigned char mdr) | ||
| { | ||
| cpu->ram[mem] = mdr; | ||
| } | ||
|
|
||
|
|
||
| void cpu_load(struct cpu *cpu) | ||
| { | ||
| const int DATA_LEN = 6; | ||
|
|
@@ -45,11 +57,30 @@ void alu(struct cpu *cpu, enum alu_op op, unsigned char regA, unsigned char regB | |
| void cpu_run(struct cpu *cpu) | ||
| { | ||
| int running = 1; // True until we get a HLT instruction | ||
| unsigned char IR, operandA, operandB; | ||
|
|
||
| while (running) { | ||
| // TODO | ||
| // 1. Get the value of the current instruction (in address PC). | ||
| IR = cpu_ram_read(cpu, cpu->PC); | ||
| operandA = cpu_ram_read(cpu, cpu->PC+1); | ||
| operandB = cpu_ram_read(cpu, cpu->PC+1); | ||
| // 2. switch() over it to decide on a course of action. | ||
| switch (IR) { | ||
| case LDI: | ||
| cpu->registers[operandA] = operandB; | ||
| cpu->PC+=3; | ||
| break; | ||
| case PRN: | ||
| printf("%d\n", cpu->registers[operandA]); | ||
| cpu->PC+=2; | ||
| break; | ||
| case HTL: | ||
| running = 0; | ||
| cpu->PC+=1; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eventually you may come across a way to increase the PC using the instruction code itself (hint: this involves bit shifting). |
||
| break | ||
| } | ||
|
|
||
| // 3. Do whatever the instruction should do according to the spec. | ||
| // 4. Move the PC to the next instruction. | ||
| } | ||
|
|
@@ -61,6 +92,11 @@ void cpu_run(struct cpu *cpu) | |
| void cpu_init(struct cpu *cpu) | ||
| { | ||
| // TODO: Initialize the PC and other special registers | ||
| cpu->PC = 0; | ||
|
|
||
|
|
||
|
|
||
| // TODO: Zero registers and RAM | ||
| memset(cpu->ram, 0, sizeof cpu->ram); | ||
| memset(cpu->registers, 0, sizeof cpu->registers); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,11 @@ | |
| struct cpu { | ||
| // TODO | ||
| // PC | ||
| unsigned char PC; | ||
| // registers (array) | ||
| unsigned char registers[6]; | ||
| // ram (array) | ||
| unsigned char ram[256]; | ||
| }; | ||
|
|
||
| // Instructions | ||
|
|
@@ -15,6 +18,8 @@ struct cpu { | |
| // literals should be used. | ||
|
|
||
| #define LDI 0b10000010 | ||
| #define PRN 0b01000111 | ||
| #define HTL 0b00000001 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be |
||
| // TODO: more instructions here. These can be used in cpu_run(). | ||
|
|
||
| // Function declarations | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!