| ... | ... |
@@ -15,6 +15,7 @@ WITH REGARD TO THIS SOFTWARE. |
| 15 | 15 |
#define ECHO 1 |
| 16 | 16 |
|
| 17 | 17 |
typedef unsigned char Uint8; |
| 18 |
+typedef unsigned char Uint16; |
|
| 18 | 19 |
|
| 19 | 20 |
typedef struct {
|
| 20 | 21 |
|
| ... | ... |
@@ -23,12 +24,13 @@ typedef struct {
|
| 23 | 24 |
Uint8 sptr; |
| 24 | 25 |
Uint8 stack[STACK_DEPTH]; |
| 25 | 26 |
Uint8 address[STACK_DEPTH]; |
| 26 |
-Uint8 memory[STACK_DEPTH]; |
|
| 27 |
+Uint16 memory[STACK_DEPTH]; |
|
| 27 | 28 |
|
| 28 | 29 |
void |
| 29 |
-stackprint(Uint8 *s, Uint8 len) |
|
| 30 |
+echo(Uint8 *s, Uint8 len, char *name) |
|
| 30 | 31 |
{
|
| 31 | 32 |
int i; |
| 33 |
+ printf("%s\n", name);
|
|
| 32 | 34 |
for(i = 0; i < len; ++i) {
|
| 33 | 35 |
if(i % 16 == 0) |
| 34 | 36 |
printf("\n");
|
| ... | ... |
@@ -65,13 +67,12 @@ disk(Computer *cpu, FILE *f) |
| 65 | 67 |
reset(cpu); |
| 66 | 68 |
if(!fread(buffer, sizeof(buffer), 1, f)) |
| 67 | 69 |
return 0; |
| 68 |
- /* |
|
| 69 |
- |
|
| 70 |
+ |
|
| 70 | 71 |
for(i = 0; i < 128; i++) {
|
| 71 | 72 |
cpu->memory[i * 2] |= (buffer[i] >> 8) & 0xFF; |
| 72 | 73 |
cpu->memory[i * 2 + 1] |= buffer[i] & 0xFF; |
| 73 | 74 |
} |
| 74 |
- */ |
|
| 75 |
+ |
|
| 75 | 76 |
return 1; |
| 76 | 77 |
} |
| 77 | 78 |
|
| ... | ... |
@@ -92,12 +93,8 @@ main(int argc, char *argv[]) |
| 92 | 93 |
if(!disk(&cpu, f)) |
| 93 | 94 |
return error("Unreadable input.");
|
| 94 | 95 |
run(&cpu, ECHO); |
| 95 |
- /* program */ |
|
| 96 |
- op_push(stack, 0xef); |
|
| 97 |
- op_pop(stack); |
|
| 98 |
- op_push(stack, 0x02); |
|
| 99 |
- op_push(stack, 0x03); |
|
| 100 | 96 |
/* print result */ |
| 101 |
- stackprint(stack, 0x40); |
|
| 97 |
+ echo(stack, 0x40, "stack"); |
|
| 98 |
+ echo(memory, 0x40, "memory"); |
|
| 102 | 99 |
return 0; |
| 103 | 100 |
} |
| ... | ... |
@@ -11,20 +11,69 @@ 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 BUFLEN 256 |
|
| 14 |
+#define PRGLEN 256 |
|
| 15 | 15 |
|
| 16 | 16 |
typedef unsigned char Uint8; |
| 17 |
-typedef unsigned char Uint16; |
|
| 17 |
+typedef unsigned short Uint16; |
|
| 18 | 18 |
|
| 19 |
-unsigned short program[BUFLEN]; |
|
| 19 |
+typedef struct {
|
|
| 20 |
+ int ptr; |
|
| 21 |
+ Uint16 data[PRGLEN]; |
|
| 22 |
+} Program; |
|
| 23 |
+ |
|
| 24 |
+Program p; |
|
| 25 |
+ |
|
| 26 |
+#pragma mark - Helpers |
|
| 27 |
+ |
|
| 28 |
+int |
|
| 29 |
+scmp(char *a, char *b) /* string compare */ |
|
| 30 |
+{
|
|
| 31 |
+ int i = 0; |
|
| 32 |
+ while(a[i] == b[i]) |
|
| 33 |
+ if(!a[i++]) |
|
| 34 |
+ return 1; |
|
| 35 |
+ return 0; |
|
| 36 |
+} |
|
| 37 |
+ |
|
| 38 |
+int |
|
| 39 |
+shex(char *s) /* string to num */ |
|
| 40 |
+{
|
|
| 41 |
+ int n = 0, i = 0; |
|
| 42 |
+ char c; |
|
| 43 |
+ while((c = s[i++])) |
|
| 44 |
+ if(c >= '0' && c <= '9') |
|
| 45 |
+ n = n * 16 + (c - '0'); |
|
| 46 |
+ else if(c >= 'a' && c <= 'f') |
|
| 47 |
+ n = n * 16 + 10 + (c - 'a'); |
|
| 48 |
+ return n; |
|
| 49 |
+} |
|
| 50 |
+ |
|
| 51 |
+#pragma mark - Helpers |
|
| 52 |
+ |
|
| 53 |
+Uint8 |
|
| 54 |
+getopcode(char *s) |
|
| 55 |
+{
|
|
| 56 |
+ if(scmp(s, "add")) {
|
|
| 57 |
+ return 0x01; |
|
| 58 |
+ } |
|
| 59 |
+ return 0; |
|
| 60 |
+} |
|
| 20 | 61 |
|
| 21 | 62 |
void |
| 22 | 63 |
pass1(FILE *f) |
| 23 | 64 |
{
|
| 24 |
- int instrid = 0; |
|
| 25 |
- char line[BUFLEN]; |
|
| 26 |
- while(fgets(line, BUFLEN, f)) {
|
|
| 27 |
- printf("%s\n", line);
|
|
| 65 |
+ char word[64]; |
|
| 66 |
+ while(fscanf(f, "%s", word) == 1) {
|
|
| 67 |
+ int lit = 0, val = 0; |
|
| 68 |
+ if(word[0] == '#') {
|
|
| 69 |
+ lit = 0; |
|
| 70 |
+ val = shex(word + 1); |
|
| 71 |
+ } else {
|
|
| 72 |
+ lit = 1; |
|
| 73 |
+ val = getopcode(word); |
|
| 74 |
+ } |
|
| 75 |
+ printf("#%d -> %s[%02x %02x]\n", p.ptr, word, lit, val);
|
|
| 76 |
+ p.data[p.ptr++] = (val << 8) + (lit & 0xff); |
|
| 28 | 77 |
} |
| 29 | 78 |
} |
| 30 | 79 |
|
| ... | ... |
@@ -44,6 +93,7 @@ main(int argc, char *argv[]) |
| 44 | 93 |
if(!(f = fopen(argv[1], "r"))) |
| 45 | 94 |
return error("Missing input.");
|
| 46 | 95 |
pass1(f); |
| 47 |
- fwrite(program, sizeof(program), 1, fopen(argv[2], "wb")); |
|
| 96 |
+ fwrite(p.data, sizeof(p.data), 1, fopen(argv[2], "wb")); |
|
| 97 |
+ fclose(f); |
|
| 48 | 98 |
return 0; |
| 49 | 99 |
} |