... | ... |
@@ -1,7 +1,9 @@ |
1 | 1 |
AlignAfterOpenBracket: DontAlign |
2 | 2 |
AlignEscapedNewlines: DontAlign |
3 |
+AlignOperands: DontAlign |
|
3 | 4 |
AllowShortBlocksOnASingleLine: Empty |
4 | 5 |
AllowShortCaseLabelsOnASingleLine: true |
6 |
+AllowShortEnumsOnASingleLine: true |
|
5 | 7 |
AllowShortIfStatementsOnASingleLine: false |
6 | 8 |
AllowShortLoopsOnASingleLine: false |
7 | 9 |
AlwaysBreakAfterDefinitionReturnType: TopLevel |
... | ... |
@@ -7,10 +7,11 @@ clang-format -i uxn.c |
7 | 7 |
# remove old |
8 | 8 |
rm -f ./uxnasm |
9 | 9 |
rm -f ./uxn |
10 |
+rm -f ./boot.rom |
|
10 | 11 |
|
11 | 12 |
# debug(slow) |
12 | 13 |
cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxnasm.c -o uxnasm |
13 |
-# cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c -o uxn |
|
14 |
+cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined uxn.c -o uxn |
|
14 | 15 |
|
15 | 16 |
# build(fast) |
16 | 17 |
# cc uxn.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -o uxn |
... | ... |
@@ -19,5 +20,5 @@ cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werr |
19 | 20 |
# echo "Size: $(du -sk ./uxn)" |
20 | 21 |
|
21 | 22 |
# run |
22 |
-./uxnasm program.usm program.rom |
|
23 |
-# ./uxn program.rom |
|
23 |
+./uxnasm example.usm boot.rom |
|
24 |
+./uxn boot.rom |
... | ... |
@@ -11,20 +11,38 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | 11 |
WITH REGARD TO THIS SOFTWARE. |
12 | 12 |
*/ |
13 | 13 |
|
14 |
+#define FLAG_HALT 0x01 |
|
15 |
+#define FLAG_ZERO 0x02 |
|
16 |
+#define FLAG_CARRY 0x04 |
|
17 |
+#define FLAG_TRAPS 0x08 |
|
18 |
+ |
|
14 | 19 |
#define STACK_DEPTH 256 |
15 |
-#define ECHO 1 |
|
16 | 20 |
|
17 | 21 |
typedef unsigned char Uint8; |
18 |
-typedef unsigned char Uint16; |
|
19 | 22 |
|
20 | 23 |
typedef struct { |
21 |
- |
|
24 |
+ Uint8 literal; |
|
25 |
+ Uint8 status, counter; |
|
26 |
+ Uint8 memory[STACK_DEPTH]; |
|
27 |
+ Uint8 mptr, sptr; |
|
28 |
+ Uint8 stack[STACK_DEPTH]; |
|
29 |
+ Uint8 address[STACK_DEPTH]; |
|
22 | 30 |
} Computer; |
23 | 31 |
|
24 |
-Uint8 sptr; |
|
25 |
-Uint8 stack[STACK_DEPTH]; |
|
26 |
-Uint8 address[STACK_DEPTH]; |
|
27 |
-Uint16 memory[STACK_DEPTH]; |
|
32 |
+void |
|
33 |
+setflag(Computer *cpu, char flag, int b) |
|
34 |
+{ |
|
35 |
+ if(b) |
|
36 |
+ cpu->status |= flag; |
|
37 |
+ else |
|
38 |
+ cpu->status &= (~flag); |
|
39 |
+} |
|
40 |
+ |
|
41 |
+int |
|
42 |
+getflag(Computer *cpu, char flag) |
|
43 |
+{ |
|
44 |
+ return cpu->status & flag; |
|
45 |
+} |
|
28 | 46 |
|
29 | 47 |
void |
30 | 48 |
echo(Uint8 *s, Uint8 len, char *name) |
... | ... |
@@ -34,51 +52,79 @@ echo(Uint8 *s, Uint8 len, char *name) |
34 | 52 |
for(i = 0; i < len; ++i) { |
35 | 53 |
if(i % 16 == 0) |
36 | 54 |
printf("\n"); |
37 |
- if(sptr == i) |
|
38 |
- printf("[%02x]", s[i]); |
|
39 |
- else |
|
40 |
- printf(" %02x ", s[i]); |
|
55 |
+ printf("%02x ", s[i]); |
|
41 | 56 |
} |
42 |
- printf("\n"); |
|
57 |
+ printf("\n\n"); |
|
43 | 58 |
} |
44 | 59 |
|
45 | 60 |
void |
46 |
-op_push(Uint8 *s, Uint8 v) |
|
61 |
+op_push(Uint8 *s, Uint8 *ptr, Uint8 v) |
|
47 | 62 |
{ |
48 |
- s[sptr++] = v; |
|
63 |
+ s[*ptr] = v; |
|
64 |
+ (*ptr) += 1; |
|
49 | 65 |
} |
50 | 66 |
|
51 | 67 |
void |
52 |
-op_pop(Uint8 *s) |
|
68 |
+op_pop(Uint8 *s, Uint8 *ptr) |
|
53 | 69 |
{ |
54 |
- s[sptr--] = 0x00; |
|
70 |
+ s[*ptr--] = 0x00; |
|
55 | 71 |
} |
56 | 72 |
|
57 | 73 |
void |
58 | 74 |
reset(Computer *cpu) |
59 | 75 |
{ |
76 |
+ int i; |
|
77 |
+ cpu->status = 0x00; |
|
78 |
+ cpu->counter = 0x00; |
|
79 |
+ cpu->mptr = 0x00; |
|
80 |
+ cpu->sptr = 0x00; |
|
81 |
+ cpu->literal = 0x00; |
|
82 |
+ for(i = 0; i < 256; i++) |
|
83 |
+ cpu->stack[i] = 0x00; |
|
60 | 84 |
} |
61 | 85 |
|
62 | 86 |
int |
63 |
-disk(Computer *cpu, FILE *f) |
|
87 |
+error(char *name) |
|
64 | 88 |
{ |
65 |
- int i; |
|
66 |
- unsigned short buffer[256]; |
|
67 |
- reset(cpu); |
|
68 |
- if(!fread(buffer, sizeof(buffer), 1, f)) |
|
69 |
- return 0; |
|
70 |
- |
|
71 |
- for(i = 0; i < 128; i++) { |
|
72 |
- cpu->memory[i * 2] |= (buffer[i] >> 8) & 0xFF; |
|
73 |
- cpu->memory[i * 2 + 1] |= buffer[i] & 0xFF; |
|
74 |
- } |
|
89 |
+ printf("Error: %s\n", name); |
|
90 |
+ return 0; |
|
91 |
+} |
|
92 |
+ |
|
93 |
+void |
|
94 |
+load(Computer *cpu, FILE *f) |
|
95 |
+{ |
|
96 |
+ fread(cpu->memory, sizeof(cpu->memory), 1, f); |
|
97 |
+} |
|
75 | 98 |
|
76 |
- return 1; |
|
99 |
+void |
|
100 |
+eval(Computer *cpu) |
|
101 |
+{ |
|
102 |
+ Uint8 instr = cpu->memory[cpu->mptr++]; |
|
103 |
+ |
|
104 |
+ if(cpu->literal > 0) { |
|
105 |
+ printf("push: %02x[%d](%d)\n", instr, cpu->literal, cpu->sptr); |
|
106 |
+ op_push(cpu->stack, &cpu->sptr, instr); |
|
107 |
+ cpu->literal--; |
|
108 |
+ return; |
|
109 |
+ } |
|
110 |
+ switch(instr) { |
|
111 |
+ case 0x0: setflag(cpu, FLAG_HALT, 1); break; |
|
112 |
+ case 0x1: cpu->literal += 4; break; |
|
113 |
+ default: printf("Unknown instruction: #%02x\n", instr); |
|
114 |
+ } |
|
77 | 115 |
} |
78 | 116 |
|
79 | 117 |
void |
80 |
-run(Computer *cpu, int debug) |
|
118 |
+run(Computer *cpu) |
|
81 | 119 |
{ |
120 |
+ int i; |
|
121 |
+ while((cpu->status & FLAG_HALT) == 0) |
|
122 |
+ eval(cpu); |
|
123 |
+ /* debug */ |
|
124 |
+ printf("ended @ %d | ", cpu->counter); |
|
125 |
+ for(i = 0; i < 4; i++) |
|
126 |
+ printf("%d-", (cpu->status & (1 << i)) != 0); |
|
127 |
+ printf("\n\n"); |
|
82 | 128 |
} |
83 | 129 |
|
84 | 130 |
int |
... | ... |
@@ -90,11 +136,11 @@ main(int argc, char *argv[]) |
90 | 136 |
return error("No input."); |
91 | 137 |
if(!(f = fopen(argv[1], "rb"))) |
92 | 138 |
return error("Missing input."); |
93 |
- if(!disk(&cpu, f)) |
|
94 |
- return error("Unreadable input."); |
|
95 |
- run(&cpu, ECHO); |
|
139 |
+ reset(&cpu); |
|
140 |
+ load(&cpu, f); |
|
141 |
+ run(&cpu); |
|
96 | 142 |
/* print result */ |
97 |
- echo(stack, 0x40, "stack"); |
|
98 |
- echo(memory, 0x40, "memory"); |
|
143 |
+ echo(cpu.stack, 0x40, "stack"); |
|
144 |
+ echo(cpu.memory, 0x40, "memory"); |
|
99 | 145 |
return 0; |
100 | 146 |
} |
... | ... |
@@ -20,7 +20,39 @@ typedef struct { |
20 | 20 |
Uint8 data[PRGLEN]; |
21 | 21 |
} Program; |
22 | 22 |
|
23 |
-char opcodes[][4] = {"BRK", "LIT", "DUP", "DRP", "SWP", "SLP", "PSH", "POP", "JMP", "JSR", "RST", "BEQ", "EQU", "NEQ", "LTH", "GTH"}; |
|
23 |
+char opcodes[][4] = { |
|
24 |
+ "BRK", |
|
25 |
+ "LIT", |
|
26 |
+ "DUP", |
|
27 |
+ "DRP", |
|
28 |
+ "SWP", |
|
29 |
+ "SLP", |
|
30 |
+ "PSH", |
|
31 |
+ "POP", /* --- */ |
|
32 |
+ "JMP", |
|
33 |
+ "JSR", |
|
34 |
+ "RST", |
|
35 |
+ "BEQ", |
|
36 |
+ "EQU", |
|
37 |
+ "NEQ", |
|
38 |
+ "LTH", |
|
39 |
+ "GTH", /* --- */ |
|
40 |
+ "---", |
|
41 |
+ "---", |
|
42 |
+ "---", |
|
43 |
+ "---", |
|
44 |
+ "---", |
|
45 |
+ "---", |
|
46 |
+ "---", |
|
47 |
+ "---", /* --- */ |
|
48 |
+ "---", |
|
49 |
+ "---", |
|
50 |
+ "---", |
|
51 |
+ "---", |
|
52 |
+ "---", |
|
53 |
+ "---", |
|
54 |
+ "---", |
|
55 |
+ "---"}; |
|
24 | 56 |
|
25 | 57 |
Program p; |
26 | 58 |
|
... | ... |
@@ -65,34 +97,22 @@ Uint8 |
65 | 97 |
getopcode(char *s) |
66 | 98 |
{ |
67 | 99 |
int i; |
100 |
+ if(s[0] == '{') /* TODO catch closing */ |
|
101 |
+ return 0x01; |
|
68 | 102 |
for(i = 0; i < 16; ++i) |
69 | 103 |
if(scmp(opcodes[i], suca(s))) |
70 | 104 |
return i; |
71 | 105 |
return 0xff; |
72 | 106 |
} |
73 | 107 |
|
74 |
-void |
|
75 |
-echo(Uint8 *s, Uint8 len, Uint8 ptr, char *name) |
|
76 |
-{ |
|
77 |
- int i; |
|
78 |
- printf("%s\n", name); |
|
79 |
- for(i = 0; i < len; ++i) { |
|
80 |
- if(i % 16 == 0) |
|
81 |
- printf("\n"); |
|
82 |
- if(ptr == i) |
|
83 |
- printf("[%02x]", s[i]); |
|
84 |
- else |
|
85 |
- printf(" %02x ", s[i]); |
|
86 |
- } |
|
87 |
- printf("\n"); |
|
88 |
-} |
|
89 |
- |
|
90 | 108 |
void |
91 | 109 |
pass1(FILE *f) |
92 | 110 |
{ |
93 | 111 |
char word[64]; |
94 | 112 |
while(fscanf(f, "%s", word) == 1) { |
95 | 113 |
int op = getopcode(word); |
114 |
+ if(word[0] == '}') |
|
115 |
+ continue; |
|
96 | 116 |
if(op == 0xff) |
97 | 117 |
op = shex(word); |
98 | 118 |
p.data[p.ptr++] = op; |
... | ... |
@@ -117,6 +137,5 @@ main(int argc, char *argv[]) |
117 | 137 |
pass1(f); |
118 | 138 |
fwrite(p.data, sizeof(p.data), 1, fopen(argv[2], "wb")); |
119 | 139 |
fclose(f); |
120 |
- echo(p.data, 0x40, 0, "program"); |
|
121 | 140 |
return 0; |
122 | 141 |
} |