Browse code

Architecture

neauoire authored on 02/02/2021 17:40:41
Showing 2 changed files
... ...
@@ -1,4 +1,4 @@
1 1
 < core >
2 2
 
3
-01 ADD
3
+01 02 ADD
4 4
 
... ...
@@ -16,18 +16,24 @@ WITH REGARD TO THIS SOFTWARE.
16 16
 #define FLAG_CARRY 0x04
17 17
 #define FLAG_TRAPS 0x08
18 18
 
19
-#define STACK_DEPTH 256
20
-
21 19
 typedef unsigned char Uint8;
22 20
 typedef unsigned short Uint16;
23 21
 
22
+typedef struct {
23
+	Uint8 ptr;
24
+	Uint8 dat[256];
25
+} Stack;
26
+
27
+typedef struct {
28
+	Uint16 ptr;
29
+	Uint8 dat[65536];
30
+} Memory;
31
+
24 32
 typedef struct {
25 33
 	Uint8 literal, status;
26
-	Uint8 mptr, sptr, rsptr;
27
-	Uint8 memory[STACK_DEPTH];
28
-	Uint8 stack[STACK_DEPTH];
29
-	Uint8 rstack[STACK_DEPTH];
30 34
 	Uint16 counter;
35
+	Stack wst, rst;
36
+	Memory rom, ram;
31 37
 } Computer;
32 38
 
33 39
 Computer cpu;
... ...
@@ -65,25 +71,25 @@ echo(Uint8 *s, Uint8 len, char *name)
65 71
 void
66 72
 spush(Uint8 v)
67 73
 {
68
-	cpu.stack[cpu.sptr++] = v;
74
+	cpu.wst.dat[cpu.wst.ptr++] = v;
69 75
 }
70 76
 
71 77
 Uint8
72 78
 spop(void)
73 79
 {
74
-	return cpu.stack[--cpu.sptr];
80
+	return cpu.wst.dat[--cpu.wst.ptr];
75 81
 }
76 82
 
77 83
 void
78 84
 rspush(Uint8 v)
79 85
 {
80
-	cpu.rstack[cpu.rsptr++] = v;
86
+	cpu.rst.dat[cpu.rst.ptr++] = v;
81 87
 }
82 88
 
83 89
 Uint8
84 90
 rspop(void)
85 91
 {
86
-	return cpu.rstack[--cpu.rsptr];
92
+	return cpu.rst.dat[--cpu.rst.ptr];
87 93
 }
88 94
 
89 95
 #pragma mark - Operations
... ...
@@ -91,17 +97,17 @@ rspop(void)
91 97
 /* clang-format off */
92 98
 
93 99
 void op_brk() { setflag(FLAG_HALT, 1); }
94
-void op_rts() {	cpu.mptr = rspop(); }
95
-void op_lit() { cpu.literal += cpu.memory[cpu.mptr++]; }
100
+void op_rts() {	cpu.rom.ptr = rspop(); }
101
+void op_lit() { cpu.literal += cpu.rom.dat[cpu.rom.ptr++]; }
96 102
 void op_drp() { spop(); }
97
-void op_dup() { spush(cpu.stack[cpu.sptr - 1]); }
103
+void op_dup() { spush(cpu.wst.dat[cpu.wst.ptr - 1]); }
98 104
 void op_swp() { Uint8 b = spop(), a = spop(); spush(b); spush(a); }
99
-void op_ovr() { spush(cpu.stack[cpu.sptr - 2]); }
105
+void op_ovr() { spush(cpu.wst.dat[cpu.wst.ptr - 2]); }
100 106
 void op_rot() { Uint8 c = spop(),b = spop(),a = spop(); spush(b); spush(c); spush(a); }
101
-void op_jmp() { cpu.mptr = spop(); }
102
-void op_jsr() { rspush(cpu.mptr); cpu.mptr = spop(); }
103
-void op_jmq() { Uint8 a = spop(); if(getflag(FLAG_ZERO)){ cpu.mptr = a; } setflag(FLAG_ZERO,0); }
104
-void op_jsq() { Uint8 a = spop(); if(getflag(FLAG_ZERO)){ rspush(cpu.mptr); cpu.mptr = a; } setflag(FLAG_ZERO,0); }
107
+void op_jmp() { cpu.rom.ptr = spop(); }
108
+void op_jsr() { rspush(cpu.rom.ptr); cpu.rom.ptr = spop(); }
109
+void op_jmq() { Uint8 a = spop(); if(getflag(FLAG_ZERO)){ cpu.rom.ptr = a; } setflag(FLAG_ZERO,0); }
110
+void op_jsq() { Uint8 a = spop(); if(getflag(FLAG_ZERO)){ rspush(cpu.rom.ptr); cpu.rom.ptr = a; } setflag(FLAG_ZERO,0); }
105 111
 void op_equ() { setflag(FLAG_ZERO, spop() == spop()); }
106 112
 void op_neq() { setflag(FLAG_ZERO, spop() != spop()); }
107 113
 void op_lth() {	setflag(FLAG_ZERO, spop() < spop()); }
... ...
@@ -135,11 +141,12 @@ reset(void)
135 141
 	int i;
136 142
 	cpu.status = 0x00;
137 143
 	cpu.counter = 0x00;
138
-	cpu.mptr = 0x00;
139
-	cpu.sptr = 0x00;
144
+	cpu.rom.ptr = 0x00;
145
+	cpu.wst.ptr = 0x00;
140 146
 	cpu.literal = 0x00;
147
+	cpu.rst.ptr = 0x00;
141 148
 	for(i = 0; i < 256; i++)
142
-		cpu.stack[i] = 0x00;
149
+		cpu.wst.dat[i] = 0x00;
143 150
 }
144 151
 
145 152
 int
... ...
@@ -152,20 +159,20 @@ error(char *name)
152 159
 void
153 160
 load(FILE *f)
154 161
 {
155
-	fread(cpu.memory, sizeof(cpu.memory), 1, f);
162
+	fread(cpu.rom.dat, sizeof(cpu.rom.dat), 1, f);
156 163
 }
157 164
 
158 165
 int
159 166
 eval()
160 167
 {
161
-	Uint8 instr = cpu.memory[cpu.mptr++];
168
+	Uint8 instr = cpu.rom.dat[cpu.rom.ptr++];
162 169
 	if(cpu.literal > 0) {
163 170
 		spush(instr);
164 171
 		cpu.literal--;
165 172
 		return 1;
166 173
 	}
167 174
 	if(instr < 24) {
168
-		if(cpu.sptr < opr[instr][0])
175
+		if(cpu.wst.ptr < opr[instr][0])
169 176
 			return error("Stack underflow");
170 177
 		/* TODO stack overflow */
171 178
 		(*ops[instr])();
... ...
@@ -203,7 +210,6 @@ main(int argc, char *argv[])
203 210
 	load(f);
204 211
 	run();
205 212
 	/* print result */
206
-	echo(cpu.stack, 0x40, "stack");
207
-	echo(cpu.memory, 0x40, "memory");
213
+	echo(cpu.wst.dat, 0x40, "stack");
208 214
 	return 0;
209 215
 }