-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlmc.c
269 lines (228 loc) · 5.54 KB
/
lmc.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
* lmc - Little Man Computer
* Copyright (C) 2020 David McMackins II
*
* Redistributions, modified or unmodified, in whole or in part, must retain
* applicable copyright or other legal privilege notices, these conditions, and
* the following license terms and disclaimer. Subject to these conditions,
* the holder(s) of copyright or other legal privileges, author(s) or
* assembler(s), and contributors of this work hereby grant to any person who
* obtains a copy of this work in any form:
*
* 1. Permission to reproduce, modify, distribute, publish, sell, sublicense,
* use, and/or otherwise deal in the licensed material without restriction.
*
* 2. A perpetual, worldwide, non-exclusive, royalty-free, irrevocable patent
* license to reproduce, modify, distribute, publish, sell, use, and/or
* otherwise deal in the licensed material without restriction, for any and all
* patents:
*
* a. Held by each such holder of copyright or other legal privilege,
* author or assembler, or contributor, necessarily infringed by the
* contributions alone or by combination with the work, of that privilege
* holder, author or assembler, or contributor.
*
* b. Necessarily infringed by the work at the time that holder of
* copyright or other privilege, author or assembler, or contributor made
* any contribution to the work.
*
* NO WARRANTY OF ANY KIND IS IMPLIED BY, OR SHOULD BE INFERRED FROM, THIS
* LICENSE OR THE ACT OF DISTRIBUTION UNDER THE TERMS OF THIS LICENSE,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
* A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS,
* ASSEMBLERS, OR HOLDERS OF COPYRIGHT OR OTHER LEGAL PRIVILEGE BE LIABLE FOR
* ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN ACTION OF CONTRACT, TORT,
* OR OTHERWISE ARISING FROM, OUT OF, OR IN CONNECTION WITH THE WORK OR THE USE
* OF OR OTHER DEALINGS IN THE WORK.
*/
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#ifndef NUM_MAILBOXES
# define NUM_MAILBOXES 100
#endif
#ifndef NUM_DIGITS
# define NUM_DIGITS 3
#endif
#ifndef MAX_VALUE
# define MAX_VALUE 999
#endif
struct lmc_cpu
{
int a;
int pc;
int instruction;
int opcode;
int addr;
bool neg;
bool halted;
bool error; /* controls program exit code */
};
struct lmc
{
int mailboxes[NUM_MAILBOXES];
struct lmc_cpu cpu;
};
static void
bad_instruction(struct lmc *lmc)
{
struct lmc_cpu *cpu = &lmc->cpu;
fprintf(stderr, "Bad instruction! (%d)\n", cpu->instruction);
fprintf(stderr, "a = %d\n", cpu->a);
fprintf(stderr, "pc = %d\n", cpu->pc);
fprintf(stderr, "opcode = %d\n", cpu->opcode);
fprintf(stderr, "addr = %d\n", cpu->addr);
fprintf(stderr, "neg = %d\n", !!cpu->neg);
fprintf(stderr, "halt = %d\n", !!cpu->halted);
cpu->halted = true;
cpu->error = true;
}
typedef void (*lmc_op)(struct lmc *lmc);
static void
lmc_halt(struct lmc *lmc)
{
lmc->cpu.halted = true;
}
static void
lmc_add(struct lmc *lmc)
{
lmc->cpu.a += lmc->mailboxes[lmc->cpu.addr];
lmc->cpu.neg = lmc->cpu.a > MAX_VALUE;
if (lmc->cpu.neg)
lmc->cpu.a -= MAX_VALUE + 1;
}
static void
lmc_sub(struct lmc *lmc)
{
lmc->cpu.a -= lmc->mailboxes[lmc->cpu.addr];
lmc->cpu.neg = lmc->cpu.a < 0;
if (lmc->cpu.neg)
lmc->cpu.a += MAX_VALUE + 1;
}
static void
lmc_store(struct lmc *lmc)
{
lmc->mailboxes[lmc->cpu.addr] = lmc->cpu.a;
}
static void
lmc_load(struct lmc *lmc)
{
lmc->cpu.a = lmc->mailboxes[lmc->cpu.addr];
}
static void
lmc_branch(struct lmc *lmc)
{
lmc->cpu.pc = lmc->cpu.addr;
}
static void
lmc_branch_zero(struct lmc *lmc)
{
if (0 == lmc->cpu.a)
lmc->cpu.pc = lmc->cpu.addr;
}
static void
lmc_branch_positive(struct lmc *lmc)
{
if (!lmc->cpu.neg)
lmc->cpu.pc = lmc->cpu.addr;
}
static void
lmc_io(struct lmc *lmc)
{
int rc;
switch (lmc->cpu.addr)
{
case 1:
rc = 0;
while (rc != 1 || lmc->cpu.a < 0 || lmc->cpu.a > MAX_VALUE)
{
printf("Input number (0-%d): ", MAX_VALUE);
rc = scanf("%d", &lmc->cpu.a);
}
break;
case 2:
printf("%d\n", lmc->cpu.a);
break;
default:
bad_instruction(lmc);
break;
}
}
const lmc_op OPS[] =
{
lmc_halt,
lmc_add,
lmc_sub,
lmc_store,
NULL, /* no 4xx instruction in ISA */
lmc_load,
lmc_branch,
lmc_branch_zero,
lmc_branch_positive,
lmc_io
};
int
main(int argc, char *argv[])
{
struct lmc lmc;
FILE *input_file;
int c, i;
if (argc != 2)
{
fprintf(stderr, "Usage: lmc <input>\n");
return 1;
}
memset(&lmc, 0, sizeof lmc);
input_file = fopen(argv[1], "rb");
if (!input_file)
{
fprintf(stderr, "Error opening %s: %s\n", argv[1],
strerror(errno));
return 1;
}
i = 0;
while ((c = fgetc(input_file)) != EOF)
{
int *mailbox;
if (c > 9) /* not a digit */
{
fprintf(stderr, "Digit %d at position %d is too big\n",
c, i);
return 1;
}
mailbox = &lmc.mailboxes[i / NUM_DIGITS];
*mailbox *= 10;
*mailbox += c;
++i;
}
if (ferror(input_file))
{
fprintf(stderr, "Error loading %s: %s\n", argv[1],
strerror(errno));
fclose(input_file);
return 1;
}
fclose(input_file);
if ((i % NUM_DIGITS) != 0)
{
fprintf(stderr,
"File size is not a multiple of the number of digits per mailbox\n");
return 1;
}
printf("%s loaded. %d mailboxes.\n", argv[1], i / NUM_DIGITS);
while (!lmc.cpu.halted)
{
lmc_op op;
lmc.cpu.instruction = lmc.mailboxes[lmc.cpu.pc++];
lmc.cpu.opcode = lmc.cpu.instruction / NUM_MAILBOXES;
lmc.cpu.addr = lmc.cpu.instruction % NUM_MAILBOXES;
if (lmc.cpu.opcode > 9 || (op = OPS[lmc.cpu.opcode]) == NULL)
{
bad_instruction(&lmc);
break;
}
op(&lmc);
}
return !!lmc.cpu.error;
}