-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzex.zig
More file actions
149 lines (111 loc) · 3.6 KB
/
Copy pathzex.zig
File metadata and controls
149 lines (111 loc) · 3.6 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
// ********** imports ********** //
const Z80 = @import("zig80");
const std = @import("std");
const log = std.log;
const Io = std.Io;
const Allocator = std.mem.Allocator;
// ********** global var ********** //
const BOLD = "\x1b[1m";
const RESET = "\x1b[0m";
const start_addr = 0x100;
const rom_extention = ".com";
var z: Z80 = .init(.{
.memRead = &memRead,
.memWrite = &memWrite,
.ioRead = &ioRead,
.ioWrite = &ioWrite,
});
var memory: [65536]u8 = @splat(0);
// ********** private functions ********** //
fn memRead(addr: u16) u8 {
return memory[addr];
}
fn memWrite(addr: u16, val: u8) void {
memory[addr] = val;
}
fn ioRead(_: u16) u8 {
return 0xff;
}
fn ioWrite(_: u16, _: u8) void {}
fn loadRom(io: Io, allocator: Allocator, rom_name: []const u8) !void {
const base_path = "./tests/roms/";
const rom_path = try std.mem.concat(allocator, u8, &[_][]const u8{
base_path,
rom_name,
rom_extention,
});
defer allocator.free(rom_path);
const file = try Io.Dir.cwd().openFile(io, rom_path, .{ .mode = .read_only });
defer file.close(io);
var buffer: [1024]u8 = undefined;
var file_reader = file.reader(io, &buffer);
const reader = &file_reader.interface;
const rom_data = try reader.allocRemaining(allocator, .unlimited);
defer allocator.free(rom_data);
@memset(memory[0..], 0);
@memcpy(memory[start_addr .. rom_data.len + start_addr], rom_data);
memory[0x0000] = 0x76; // halt, end of tests
memory[0x0005] = 0xC9; // ret, after print
}
fn handleSyscall(writer: *Io.Writer) !void {
switch (z.c) {
2 => try writer.print("{c}", .{z.e}),
9 => {
var addr: u16 = (@as(u16, z.d) << 8) | z.e;
while (true) : (addr +%= 1) {
const char = memory[addr];
if (char == '$') {
break;
}
try writer.print("{c}", .{char});
}
},
else => |n| log.err("Unknown syscal: {d}", .{n}),
}
try writer.flush();
}
fn runTest(io: Io, allocaor: Allocator, writer: *Io.Writer, rom_name: []const u8) !void {
z.reset();
z.pc = start_addr;
try loadRom(io, allocaor, rom_name);
log.info("Running {s}{s}\n", .{ rom_name, rom_extention });
var nb_instructions: u64 = 0;
const timer: std.Io.Timestamp = .now(io, .awake);
while (!z.is_halted) {
z.step();
nb_instructions += 1;
if (z.pc == 0x0005) {
try handleSyscall(writer);
}
}
const duration = Io.Timestamp.untilNow(timer, io, .awake);
const test_time: f128 = @as(f128, @floatFromInt(duration.toNanoseconds())) / 1_000_000_000.0;
try writer.print("\n\n", .{});
try writer.flush();
log.info("Test {s}{s} took {d} cycles, and ran in {:.2}s", .{
rom_name,
rom_extention,
z.cycles,
test_time,
});
log.info("Test {s}{s} ran at {:.2} MHz ({:.2} MIPS)", .{
rom_name,
rom_extention,
@as(f128, @floatFromInt(z.cycles)) / test_time / 1_000_000.0,
@as(f128, @floatFromInt(nb_instructions)) / test_time / 1_000_000.0,
});
}
// ********** public functions ********** //
pub fn run(io: Io, alloc: Allocator, writer: *Io.Writer, rom: ?[]const u8) !void {
try writer.print("{s}Z80 ZEX Tests{s}\n\n", .{ BOLD, RESET });
try writer.flush();
if (rom) |r| {
try runTest(io, alloc, writer, r);
} else {
const roms = [_][]const u8{ "prelim", "zexdoc", "zexall" };
for (roms) |r| {
try runTest(io, alloc, writer, r);
try writer.flush();
}
}
}