-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.c
More file actions
67 lines (57 loc) · 1.33 KB
/
cpu.c
File metadata and controls
67 lines (57 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "cpu.h"
#define DATA_LEN 6
/**
* Load the binary bytes from a .ls8 source file into a RAM array
*/
void cpu_load(struct cpu *cpu)
{
char data[DATA_LEN] = {
// From print8.ls8
0b10000010, // LDI R0,8
0b00000000,
0b00001000,
0b01000111, // PRN R0
0b00000000,
0b00000001 // HLT
};
int address = 0;
for (int i = 0; i < DATA_LEN; i++) {
cpu->ram[address++] = data[i];
}
// TODO: Replace this with something less hard-coded
}
/**
* ALU
*/
void alu(struct cpu *cpu, enum alu_op op, unsigned char regA, unsigned char regB)
{
switch (op) {
case ALU_MUL:
// TODO
break;
// TODO: implement more ALU ops
}
}
/**
* Run the CPU
*/
void cpu_run(struct cpu *cpu)
{
int running = 1; // True until we get a HLT instruction
while (running) {
// TODO
// 1. Get the value of the current instruction (in address PC).
// 2. Figure out how many operands this next instruction requires
// 3. Get the appropriate value(s) of the operands following this instruction
// 4. switch() over it to decide on a course of action.
// 5. Do whatever the instruction should do according to the spec.
// 6. Move the PC to the next instruction.
}
}
/**
* Initialize a CPU struct
*/
void cpu_init(struct cpu *cpu)
{
// TODO: Initialize the PC and other special registers
}