-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.zig
410 lines (391 loc) · 14.2 KB
/
processor.zig
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
const c = @import("c.zig");
const window = @import("window.zig");
const format = @import("format.zig");
const ExceptionCode = enum {
unknown_register,
not_followed_by_goto,
stack_overflow,
unknown_instruction,
};
fn handle_exception(code: ExceptionCode) void {
if (exception_handler == .none) {
@memset(window.pixels[0 .. window.size * (window.cell_size * window.square_size) * 2], .{ .red = 0, .green = 0, .blue = 0 });
const explanation = switch (code) {
// zig fmt: off
.unknown_register => " WITH UNKNOWN REGISTER",
.not_followed_by_goto => " NOT FOLLOWED BY GOTO ",
.stack_overflow => " CAUSED STACK OVERFLOW",
.unknown_instruction => " IS UNKNOWN ",
// zig fmt: on
};
var x: u16 = 0;
inline for (.{ memory[exception_address..][0..4], explanation }) |string| {
for (string) |character| {
window.text.draw_character(
character,
x * window.cell_size,
(x / 16) * window.cell_size,
.{ .red = 0xff, .green = 0, .blue = 0 },
);
x += 1;
}
}
window.redraw = true;
state = .halt;
} else {
xy.x = @intFromEnum(code);
w = exception_address;
address = @intFromEnum(exception_handler);
}
}
// TODO: include this code at the end if there's not already such a label
// and instead of running this through the assembler include it in byte form directly (which is not even that unreadable)
//GOTO BOOT_END
//BOOT:
// SETW 0
// SETX 0
// SETY 0
// TRGT W
// PTR
// POINTER
// KEYBOARD KBD
// POINTER -> PTR
// SETW 'W'
//
// SKBH:
// SPTH
//
// REGE
//A
// SCLR WHITE
// GOTO 0
//WHITE:
// DATA $FFFFFF
//BOOT_END:
var w: u32 = 0;
var xy: packed struct(u8) {
x: u4 = 0,
y: u4 = 0,
} = .{};
pub var memory: [2 << 15]u8 = undefined;
var target: u8 = 'W';
var decimal = false;
var stack: [256]u32 = undefined;
var stack_pointer: u8 = 0;
var color: [3]u8 = .{ 0xff, 0xff, 0xff };
var exception_handler: enum(u16) { none, _ } = .none;
var exception_address: u16 = undefined;
var address: u16 = 0;
//pub fn initialize() void {
// TODO: special BOOT: and exception: labels that can be replaced by user code
// the default BOOT: code is going to set the values currently set above.
// and by default they'll be random.
//const seed = seed: {
// var timespec: posix.timespec = undefined;
// posix.clock_gettime(.REALTIME, ×pec) catch break :seed 0;
// break :seed @as(u64, @truncate(@as(usize, @bitCast(timespec.nsec))));
//};
//var random_number_generator = Random.DefaultPrng.init(seed);
//w = @truncate(random_number_generator.next());
//xy = @bitCast(@as(u8,@truncate(random_number_generator.next())));
//}
fn get_integer() u32 {
const integer: u32 = @bitCast(memory[address..][0..4].*);
address +%= 4;
return integer;
}
fn get_register() u8 {
const register = memory[address];
address +%= 1;
return register;
}
fn mnemonic(string: *const [4:0]u8) u32 {
return @bitCast(@as([4]u8, string.*));
}
// TODO:
//CALL -> SAVE (ADDRESS) + GOTO
//RETN -> RSTR (ADDRESS) + GOTO
const State = union(enum) { execute_next_instruction, wait_milliseconds: u32, key_handler, halt };
var state: State = .execute_next_instruction;
pub fn process() void {
switch (state) {
.execute_next_instruction => execute_next_instruction(),
.wait_milliseconds => |milliseconds_remaining| {
// Split up the waiting to keep the program responsive.
const division = 100;
const nanoseconds_per_microseconds = 1000;
const nanoseconds_per_milliseconds = 1000 * nanoseconds_per_microseconds;
var remaining: c.timespec = undefined;
if (milliseconds_remaining < division) {
state = .execute_next_instruction;
const duration = c.timespec{ .sec = 0, .nsec = milliseconds_remaining * nanoseconds_per_milliseconds };
_ = c.nanosleep(&duration, &remaining);
} else {
// TODO: workaround for (duplicate issue) https://github.com/ziglang/zig/issues/21462:
// change State{ to .{ once that issue is fixed.
state = State{ .wait_milliseconds = milliseconds_remaining -| division };
const duration = c.timespec{ .sec = 0, .nsec = 100 * nanoseconds_per_milliseconds };
_ = c.nanosleep(&duration, &remaining);
}
},
.key_handler => {},
.halt => {},
}
}
fn execute_next_instruction() void {
exception_address = address;
const instruction_operation_code = get_integer();
switch (instruction_operation_code) {
mnemonic("ADDI") => {
const integer = get_integer();
switch (target) {
'W' => w += integer,
'X' => xy.x += @truncate(integer),
'Y' => xy.y += @truncate(integer),
'Z' => xy = @bitCast(@as(u8, @bitCast(xy)) + @as(u8, @truncate(integer))),
else => unreachable,
}
},
mnemonic("CLRS") => {
const integer = get_integer();
var clear_color = memory[integer..][0..3].*;
clear_color = @bitCast(if (@import("builtin").cpu.arch.endian() == .big)
clear_color
else
@as([3]u8, @bitCast(@byteSwap(@as(u24, @bitCast(clear_color))))));
@memset(window.pixels[0 .. window.size * window.size], window.Color{ .red = clear_color[0], .green = clear_color[1], .blue = clear_color[2] });
window.redraw = true;
},
mnemonic("CPTO") => {
const destination = get_register();
const source = target;
switch (destination) {
'W' => switch (source) {
'W' => {},
'X' => w = xy.x,
'Y' => w = xy.y,
'Z' => w = @as(u8, @bitCast(xy)),
else => unreachable,
},
'X' => switch (source) {
'W' => xy.x = @truncate(w),
'X' => {},
'Y' => xy.x = xy.y,
'Z' => xy.x = @truncate(@as(u8, @bitCast(xy))),
else => unreachable,
},
'Y' => switch (source) {
'W' => xy.y = @truncate(w),
'X' => xy.y = xy.x,
'Y' => {},
'Z' => xy.y = @truncate(@as(u8, @bitCast(xy))),
else => unreachable,
},
'Z' => switch (source) {
'W' => xy = @bitCast(@as(u8, @truncate(w))),
'X' => xy = @bitCast(@as(u8, xy.x)),
'Y' => xy = @bitCast(@as(u8, xy.y)),
'Z' => {},
else => unreachable,
},
else => return handle_exception(.unknown_register),
}
},
mnemonic("DDEC") => {
decimal = false;
},
mnemonic("DECR") => {
const register = get_register();
switch (register) {
'W' => w -%= 1,
'X' => xy.x -%= 1,
'Y' => xy.y -%= 1,
'Z' => xy = @bitCast(@as(u8, @bitCast(xy)) -% 1),
else => return handle_exception(.unknown_register),
}
},
mnemonic("DRAW") => {
const integer = get_integer();
for (0..16) |y| {
window.draw_bits(
16,
@bitCast(memory[integer + y * 4 ..][0..2].*),
@as(u16, xy.x) * window.cell_size,
((@as(u16, xy.y) * window.cell_size) + @as(u16, @intCast(y))),
.{ .red = color[0], .green = color[1], .blue = color[2] },
);
}
window.redraw = true;
},
mnemonic("EDEC") => {
decimal = true;
},
mnemonic("GOTO") => {
const integer = get_integer();
address = @truncate(integer);
},
mnemonic("HALT") => {
window.stop = true;
},
mnemonic("IFEQ") => {
const integer = get_integer();
const next_instruction = get_integer();
address -%= 4;
if (next_instruction != mnemonic("GOTO")) {
return handle_exception(.not_followed_by_goto);
}
if (switch (target) {
'W' => w != integer,
'X' => xy.x != integer,
'Y' => xy.y != integer,
'Z' => @as(u8, @bitCast(xy)) != integer,
else => unreachable,
}) {
// Take the else branch.
address +%= 8;
}
},
mnemonic("IFNE") => {
const integer = get_integer();
const next_instruction = get_integer();
address -%= 4;
if (next_instruction != mnemonic("GOTO")) {
return handle_exception(.not_followed_by_goto);
}
if (switch (target) {
'W' => w == integer,
'X' => xy.x == integer,
'Y' => xy.y == integer,
'Z' => @as(u8, @bitCast(xy)) == integer,
else => unreachable,
}) {
// Take the else branch.
address +%= 8;
}
},
mnemonic("IFPP") => {
const next_instruction = get_integer();
address -%= 4;
if (next_instruction != mnemonic("GOTO")) {
return handle_exception(.not_followed_by_goto);
}
if (!window.pointer_pressed) {
// Take the else branch.
address +%= 8;
}
},
mnemonic("INCR") => {
const register = get_register();
switch (register) {
'W' => w +%= 1,
'X' => xy.x +%= 1,
'Y' => xy.y +%= 1,
'Z' => xy = @bitCast(@as(u8, @bitCast(xy)) +% 1),
else => return handle_exception(.unknown_register),
}
if (decimal) {
if (xy.x == 10) {
xy.x = 0;
xy.y += 1;
}
if (xy.y == 10) {
xy.y = 0;
}
}
},
mnemonic("LDPP") => {
xy.x = window.pointer_x;
xy.y = window.pointer_y;
},
mnemonic("LDKK") => {
w = window.last_pressed_key;
},
// TODO: if it turns out using W for this is just too much, the only solution would perhaps be an offset/index register.
// INCI
// DECI
// SETI <- this actually takes a register to copy from
// SITW <- or this: "set I to W". W is the only one that makes sense anyway.
// CWTI <- "copy W to I"?
mnemonic("LOAD") => {
const integer = get_integer() +% w;
switch (target) {
'W' => w = @as(u32, @bitCast(memory[integer..][0..4].*)),
'X' => xy.x = @truncate(memory[integer]),
'Y' => xy.y = @truncate(memory[integer]),
'Z' => xy = @bitCast(memory[integer]),
else => unreachable,
}
},
mnemonic("PRNT") => {
window.text.draw_character(
@truncate(w),
@as(u8, xy.x) * window.cell_size,
@as(u8, xy.y) * window.cell_size,
.{ .red = color[0], .green = color[1], .blue = color[2] },
);
window.redraw = true;
},
mnemonic("RSTR") => {
stack_pointer, const overflow = @subWithOverflow(stack_pointer, 1);
if (overflow == 1) return handle_exception(.stack_overflow);
const register = get_register();
switch (register) {
'W' => w = stack[stack_pointer],
'X' => xy.x = @truncate(stack[stack_pointer]),
'Y' => xy.y = @truncate(stack[stack_pointer]),
'Z' => xy = @bitCast(@as(u8, @truncate(stack[stack_pointer]))),
else => return handle_exception(.unknown_register),
}
},
mnemonic("SAVE") => {
const register = get_register();
stack[stack_pointer] = switch (register) {
'W' => w,
'X' => xy.x,
'Y' => xy.y,
'Z' => @as(u8, @bitCast(xy)),
else => return handle_exception(.unknown_register),
};
stack_pointer +%= 1;
},
mnemonic("SCLR") => {
const integer = get_integer();
color = memory[integer..][0..3].*;
color = @bitCast(if (@import("builtin").cpu.arch.endian() == .big)
color
else
@as([3]u8, @bitCast(@byteSwap(@as(u24, @bitCast(color))))));
},
mnemonic("SEXH") => {
const integer = get_integer();
exception_handler = @enumFromInt(@as(u16, @truncate(integer)));
},
mnemonic("SETW") => {
const integer = get_integer();
w = integer;
},
mnemonic("SETX") => {
const integer = get_integer();
xy.x = @truncate(integer);
},
mnemonic("SETY") => {
const integer = get_integer();
xy.y = @truncate(integer);
},
mnemonic("TRGT") => {
const register = get_register();
switch (register) {
'W', 'X', 'Y', 'Z' => {},
else => return handle_exception(.unknown_register),
}
target = register;
},
mnemonic("WAIT") => {
const integer = get_integer();
// TODO: workaround for (duplicate issue) https://github.com/ziglang/zig/issues/21462:
// change State{ to .{ once that issue is fixed.
state = State{ .wait_milliseconds = integer };
},
else => return handle_exception(.unknown_instruction),
}
}