-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.c
More file actions
168 lines (140 loc) · 3.17 KB
/
misc.c
File metadata and controls
168 lines (140 loc) · 3.17 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Fairweather 'C' implementation of a MISC (Minimal Instruction Set CPU) virtual machine
with a GForth kernel. This program is unlikely to run or compile on a non-Windows machine.
It can be built with the GCC compiler as follows:
gcc misc.c -o misc.exe
MIT License
Copyright (c) 2021 Steve Teal
*/
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<stdint.h>
#include<string.h>
#define RAM_SIZE (32768)
#define ESC (27)
uint16_t ram[RAM_SIZE];
uint16_t accu;
uint16_t sf;
uint16_t zf;
uint16_t cf;
uint16_t pc;
uint16_t key;
int loadfile(char *fileName)
{
FILE *fp;
int index;
fp = fopen(fileName,"rb");
if(fp)
{
fread(ram,sizeof(uint16_t),RAM_SIZE,fp);
fclose(fp);
for(index=0;index<RAM_SIZE;index++)
{
ram[index] = ram[index] >> 8 | ram[index] << 8;
}
}
return (fp != NULL);
}
uint16_t getkey(void)
{
key = getch()&0xff;
return key;
}
void writeAccu(uint16_t value)
{
accu = value;
sf = value&0x8000?1:0;
zf = value?0:1;
}
uint16_t read(uint16_t address)
{
switch(address)
{
case 0: return pc;
case 1: return pc + 2;
case 2: return pc + 4;
case 3: return pc + 6;
case 7: return read(accu);
case 8: return accu;
case 0xfffd: return 0;
case 0xfffe: return getkey();
case 0xffff: return kbhit()?0:1;
default: break;
}
return address<RAM_SIZE?ram[address]:0;
}
uint16_t sub(uint16_t a, uint16_t b)
{
uint16_t s;
b = ~b;
s = a + b + 1;
cf = (((a&b)|((a|b)&~s))&0x8000)?0:1;
return s;
}
uint16_t add(uint16_t a, uint16_t b)
{
uint16_t s;
s = a + b;
cf = (((a&b)|((a|b)&~s))&0x8000)?1:0;
return s;
}
uint16_t shiftright(uint16_t a)
{
uint16_t s;
s = a >> 1 | cf << 15;
cf = a & 1;
return s;
}
void write(uint16_t address, uint16_t data)
{
switch(address)
{
case 0: pc = data; return;
case 1: pc = sf?data:pc; return;
case 2: pc = zf?data:pc; return;
case 4: pc = cf?data:pc; return;
case 7: write(accu,data); return;
case 8: writeAccu(data); return;
case 9: writeAccu(sub(accu,data)); return;
case 11: writeAccu(add(data,accu)); return;
case 12: writeAccu(data ^ accu); return;
case 13: writeAccu(data | accu); return;
case 14: writeAccu(data & accu); return;
case 15: writeAccu(shiftright(data)); return;
case 0xfffc: if(data!=13)putch(data&0xff); return;
default: break;
}
if(address<RAM_SIZE)
{
ram[address] = data;
}
}
int main(int argc, char **argv)
{
uint16_t src;
uint16_t dst;
uint16_t temp;
if(argc != 2)
{
printf("Usage: misc <binfile.bin>\n");
return 0;
}
if(!loadfile(argv[1]))
{
printf("File: %s not found\n",argv[1]);
return 0;
}
key = 0;
pc = 0x10; /* Program counter reset value */
while(key != ESC)
{
src = ram[pc];
dst = ram[pc+1];
temp = read(src);
pc+=2;
write(dst,temp);
}
return 0;
}
/* End of file */