-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtizr80.zig
349 lines (305 loc) · 10.4 KB
/
tizr80.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
const builtin = @import("builtin");
const std = @import("std");
const Commands = @import("commands.zig");
const Cpu = @import("cpu.zig");
const Keypad = @import("ports/keypad.zig");
const Memory = @import("memory.zig");
const Ports = @import("ports.zig");
const Sync = @import("sync.zig");
const util = @import("util.zig");
pub const options = @import("options");
pub const Signal = enum {
DevChanged,
FlashSizeChanged,
TransferTotal,
TransferProgress,
TransferComplete,
LcdFrame,
SoftCmd,
};
pub const CreateOptions = struct {
allocator: std.mem.Allocator,
threading: enum { SingleThreaded, MultiThreaded } = if (builtin.single_threaded)
.SingleThreaded
else
.MultiThreaded,
signal_handler: SignalHandler = null,
};
pub const Property = enum {
device,
register,
shadow_register,
key,
flash_size,
memory_z80,
memory_adl,
flash,
ram,
port,
transfer,
// options.debugger only
watch,
watch_address,
watch_size,
watch_flags,
memory_z80_watch_flags,
memory_adl_watch_flags,
flash_watch_flags,
ram_watch_flags,
port_watch_flags,
pub const Key = union(Property) {
device: Device,
register: Cpu.RegisterId,
shadow_register: Cpu.RegisterId,
key: Keypad.Key,
flash_size: u24,
memory_z80: u16,
memory_adl: u24,
flash: u23,
ram: u19,
port: u16,
transfer: Transfer,
watch: ?u24,
watch_address: u24,
watch_size: u24,
watch_flags: u24,
memory_z80_watch_flags: u24,
memory_adl_watch_flags: u24,
flash_watch_flags: u24,
ram_watch_flags: u24,
port_watch_flags: u24,
};
};
pub const Device = enum {
unknown,
ti84pce,
ti84pcepe,
ti83pce,
ti83pceep,
ti84pcet,
ti84pcetpe,
};
pub const Transfer = enum {
total,
progress,
remaining,
@"error",
};
pub const SignalHandler = ?*const fn (*TiZr80, Signal) void;
const TiZr80 = @This();
allocator: std.mem.Allocator,
signal_handler: ?*const fn (*TiZr80, Signal) void,
sync: ?Sync = undefined,
mem: Memory = undefined,
ports: Ports = undefined,
cpu: Cpu = undefined,
commands: Commands = undefined,
thread: ?std.Thread = null,
pub fn create(create_options: CreateOptions) !*TiZr80 {
const self = try create_options.allocator.create(TiZr80);
errdefer create_options.allocator.destroy(self);
try self.init(create_options);
return self;
}
pub fn init(self: *TiZr80, create_options: CreateOptions) !void {
errdefer self.* = undefined;
self.* = TiZr80{
.allocator = create_options.allocator,
.signal_handler = create_options.signal_handler,
};
switch (create_options.threading) {
.SingleThreaded => self.sync = null,
.MultiThreaded => {
std.debug.assert(!builtin.single_threaded);
try self.sync.?.init();
},
}
errdefer if (self.sync) |*sync| sync.deinit();
try self.mem.init(self.allocator);
errdefer self.mem.deinit(self.allocator);
try self.ports.init(self.allocator);
errdefer self.ports.deinit(self.allocator);
try self.cpu.init(self.allocator);
errdefer self.cpu.deinit(self.allocator);
try self.commands.init(self.allocator);
errdefer self.commands.deinit(self.allocator);
errdefer if (self.thread) |thread| thread.join();
if (self.sync) |*sync| {
const thread = try std.Thread.spawn(
.{},
runLoop,
.{self},
);
const name = "TiZr80";
thread.setName(name[0..std.math.min(name.len, std.Thread.max_name_len)]) catch {};
self.thread = thread;
sync.start();
}
errdefer if (self.sync) |*sync| sync.stop();
}
pub fn deinit(self: *TiZr80) void {
if (self.sync) |*sync| sync.stop();
if (self.thread) |thread| thread.join();
self.commands.deinit(self.allocator);
self.cpu.deinit(self.allocator);
self.ports.deinit(self.allocator);
self.mem.deinit(self.allocator);
if (self.sync) |*sync| sync.deinit();
self.* = undefined;
}
pub fn destroy(self: *TiZr80) void {
const allocator = self.allocator;
self.deinit();
allocator.destroy(self);
}
fn IntTypeForBuffer(buffer: []const u8) type {
return std.meta.Int(.unsigned, std.math.min(buffer.len, 3) * 8);
}
fn getRaw(self: *TiZr80, property: Property.Key) ?u24 {
return switch (property) {
.register => |id| self.cpu.getAny(id),
.shadow_register => |id| self.cpu.getAnyShadow(id),
.key => |key| self.ports.keypad.getKey(key),
.flash => |address| self.mem.peek(address),
.ram => |address| self.mem.peek(Memory.ram_start + @as(u24, address)),
.port => |address| self.ports.peek(address),
else => util.todo("Unimplemented getRaw"),
};
}
pub fn get(self: *TiZr80, property: Property.Key) ?u24 {
const needs_sync = switch (property) {
else => true,
};
if (needs_sync) if (self.sync) |*sync| sync.enter();
defer if (needs_sync) if (self.sync) |*sync| sync.leave();
return self.getRaw(property);
}
pub fn getSlice(self: *TiZr80, property: Property.Key, buffer: []u8) void {
const needs_sync = switch (property) {
else => true,
};
if (needs_sync) if (self.sync) |*sync| sync.enter();
defer if (needs_sync) if (self.sync) |*sync| sync.leave();
switch (property) {
.flash => |address| for (buffer) |*value, offset| {
value.* = @intCast(u8, self.getRaw(.{ .flash = address + @intCast(u23, offset) }).?);
},
.ram => |address| for (buffer) |*value, offset| {
value.* = @intCast(u8, self.getRaw(.{ .ram = address + @intCast(u19, offset) }).?);
},
.port => |address| for (buffer) |*value, offset| {
value.* = @intCast(u8, self.getRaw(.{ .port = address + @intCast(u16, offset) }).?);
},
else => if (self.getRaw(property)) |value|
inline for (.{ 3, 2, 1, 0 }) |len| {
const IntType = std.meta.Int(.unsigned, len * 8);
if (len <= buffer.len)
break std.mem.writeIntSliceLittle(IntType, buffer, @intCast(IntType, value));
} else unreachable,
}
}
fn setRaw(self: *TiZr80, property: Property.Key, value: ?u24) void {
switch (property) {
.register => |id| self.cpu.setAny(id, value.?),
.shadow_register => |id| self.cpu.setAnyShadow(id, value.?),
.key => |key| self.ports.keypad.setKey(key, @intCast(u1, value.?)),
.ram => |address| self.mem.poke(Memory.ram_start + @as(u24, address), @intCast(u8, value.?)),
.port => |address| self.ports.poke(address, @intCast(u8, value.?)),
else => util.todo("Unimplemented setRaw"),
}
}
pub fn set(self: *TiZr80, property: Property.Key, value: ?u24) void {
const needs_sync = switch (property) {
.key => false,
else => true,
};
if (needs_sync) if (self.sync) |*sync| sync.enter();
defer if (needs_sync) if (self.sync) |*sync| sync.leave();
self.setRaw(property, value);
}
pub fn setSlice(self: *TiZr80, property: Property.Key, buffer: []const u8) void {
const needs_sync = switch (property) {
.key => false,
else => true,
};
if (needs_sync) if (self.sync) |*sync| sync.enter();
defer if (needs_sync) if (self.sync) |*sync| sync.leave();
switch (property) {
.ram => |address| for (buffer) |value, offset| {
self.setRaw(.{ .ram = address + @intCast(u19, offset) }, value);
},
.port => |address| for (buffer) |value, offset| {
self.setRaw(.{ .port = address + @intCast(u16, offset) }, value);
},
else => {
self.setRaw(property, inline for (.{ 3, 2, 1, 0 }) |len| {
if (len <= buffer.len)
break std.mem.readIntSliceLittle(std.meta.Int(.unsigned, len * 8), buffer);
});
},
}
}
pub const CommandSplitError = Commands.Error || util.ArgumentSplitter.Error;
pub fn commandSplit(self: *TiZr80, line: []const u8) CommandSplitError!i32 {
var argument_splitter = util.ArgumentSplitter.init(self.allocator, line);
defer argument_splitter.deinit();
const arguments = try argument_splitter.restOwned();
defer self.allocator.free(arguments);
return self.commandSlices(arguments);
}
pub fn commandPointers(self: *TiZr80, arguments: [*:null]const ?[*:0]const u8) Commands.Error!i32 {
const slices = try self.allocator.alloc([:0]const u8, std.mem.len(arguments));
defer self.allocator.free(slices);
for (slices) |*slice, index| slice.* = std.mem.span(arguments[index].?);
return self.commandSlices(slices);
}
pub fn commandSlices(self: *TiZr80, arguments: []const [:0]const u8) Commands.Error!i32 {
if (self.sync) |*sync| sync.enter();
defer if (self.sync) |*sync| sync.leave();
return self.commands.run(arguments);
}
pub fn runLoop(self: *TiZr80) void {
while (self.sync.?.loop()) self.cpu.step() else return;
std.debug.assert(!self.sync.?.loop());
}
pub fn sleep(self: *TiZr80) bool {
return if (self.sync) |*sync| sync.sleep() else false;
}
pub fn wake(self: *TiZr80) bool {
return if (self.sync) |*sync| sync.wake() else false;
}
fn testCreate(allocator: std.mem.Allocator) !void {
const core = try TiZr80.create(.{ .allocator = allocator });
defer core.destroy();
}
fn testInit(allocator: std.mem.Allocator) !void {
const core = try allocator.create(TiZr80);
defer allocator.destroy(core);
try core.init(.{ .allocator = allocator });
defer core.deinit();
}
test "tizr80 create/init" {
try testCreate(std.testing.allocator);
try testInit(std.testing.allocator);
}
test "tizr80 create/init allocation failures" {
try std.testing.checkAllAllocationFailures(std.testing.allocator, testCreate, .{});
try std.testing.checkAllAllocationFailures(std.testing.allocator, testInit, .{});
}
test "tizr80 sleep/wake" {
const core = try TiZr80.create(.{ .allocator = std.testing.allocator });
defer core.destroy();
try std.testing.expect(!core.sleep());
try std.testing.expect(core.wake());
try std.testing.expect(!core.wake());
try std.testing.expect(core.sleep());
try std.testing.expect(!core.sleep());
}
test "tizr80 single threaded" {
const core = try TiZr80.create(.{ .allocator = std.testing.allocator, .threading = .SingleThreaded });
defer core.destroy();
}
test {
std.testing.refAllDecls(TiZr80);
_ = @import("as.zig");
}