Skip to content

Commit 2e3c8a4

Browse files
committed
zig 0.12
1 parent 1c84e1d commit 2e3c8a4

File tree

4 files changed

+70
-52
lines changed

4 files changed

+70
-52
lines changed

build.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/bin/bash
22
set -e
33

4-
echo zig version $(./zig/zig version)
5-
touch symbols.txt
6-
./zig/zig build
7-
llvm-objdump-6.0 --source -disassemble-all -section-headers -t zig-cache/bin/main > main.asm
8-
grep '^00000000.*:$' main.asm | sed 's/^00000000//' > symbols.txt
9-
./zig/zig build
4+
echo zig version $(zig version)
5+
#touch symbols.txt
6+
zig build
7+
echo missing llvm-objdump-6.0
8+
#llvm-objdump-14 --source -disassemble-all -section-headers -t zig-out/bin/main > main.asm
9+
#grep '^00000000.*:$' main.asm | sed 's/^00000000//' > symbols.txt
10+
#zig build
1011

1112
#ARCH=thumbv6m
1213
#SOURCE=$(ls mission0*.zig)
@@ -16,4 +17,5 @@ grep '^00000000.*:$' main.asm | sed 's/^00000000//' > symbols.txt
1617
#grep 'q[0-9].*#' asm.$ARCH | egrep -v '#(-|)(16|32|48|64|80|96|112|128)'
1718
#set -e
1819

19-
ls -l zig-cache/bin/main.img symbols.txt
20+
# ls -l zig-out/bin/main.img symbols.txt
21+
ls -l zig-out/bin/main.img

build.zig

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1+
// qemu working, real working, emit asm, symbol table, readme
2+
13
pub fn build(b: *std.build.Builder) !void {
24
const display_option = b.option(bool, "display", "graphics display for qemu") orelse false;
3-
const build_exe = b.addExecutable("main", "main.zig");
4-
_ = buildExeDetails: {
5-
build_exe.emit_asm = .emit;
6-
build_exe.install();
7-
build_exe.setBuildMode(b.standardReleaseOptions());
5+
const build_exe = b.addExecutable(.{ .name = "main", .optimize = b.standardOptimizeOption(.{}), .root_source_file = .{ .path = "main.zig" }, .target = model.target });
6+
_ = build_exe_details: {
7+
// build_exe.emit_asm = .emit;
88
build_exe.setLinkerScriptPath(std.build.FileSource.relative("linker_script.ld"));
9-
build_exe.setTarget(model.target);
109
build_exe.link_function_sections = true;
11-
break :buildExeDetails 0;
10+
break :build_exe_details 0;
1211
};
13-
const format_source = b.addFmt(&[_][]const u8{ "build.zig", "main.zig" });
14-
const install_raw = b.addInstallRaw(build_exe, "main.img", std.build.InstallRawStep.CreateOptions{});
12+
const format_source = b.addFmt(.{ .paths = &[_][]const u8{ "build.zig", "main.zig" } });
13+
const install_raw = b.addInstallArtifact(build_exe, .{ .dest_sub_path = "main.img" });
1514
const make_hex_file = addCustomStep(b, MakeHexFileStep{ .input_name = "zig-out/bin/main.img", .output_name = "main.hex" });
1615
const run_qemu = b.addSystemCommand(&[_][]const u8{
1716
"qemu-system-arm",
@@ -25,27 +24,27 @@ pub fn build(b: *std.build.Builder) !void {
2524
if (display_option) "gtk" else "none",
2625
});
2726

28-
_ = declareDependencies: {
27+
_ = declare_dependencies: {
2928
build_exe.step.dependOn(&format_source.step);
3029
install_raw.step.dependOn(&build_exe.step);
3130
make_hex_file.step.dependOn(&install_raw.step);
3231
run_qemu.step.dependOn(&install_raw.step);
33-
break :declareDependencies 0;
32+
break :declare_dependencies 0;
3433
};
3534

36-
_ = declareCommandLineSteps: {
37-
b.step("make-hex", "make hex file to copy to device").dependOn(&make_hex_file.step);
35+
_ = declare_command_line_steps: {
36+
b.step("hex", "make hex file to copy to device").dependOn(&make_hex_file.step);
3837
b.step("qemu", "run in qemu").dependOn(&run_qemu.step);
3938
b.default_step.dependOn(&build_exe.step);
40-
break :declareCommandLineSteps 0;
39+
break :declare_command_line_steps 0;
4140
};
4241
}
4342

4443
const MakeHexFileStep = struct {
4544
step: std.build.Step = undefined,
4645
input_name: []const u8,
4746
output_name: []const u8,
48-
pub fn make(step: *std.build.Step) anyerror!void {
47+
pub fn make(step: *std.build.Step, _: *std.Progress.Node) anyerror!void {
4948
const self = @fieldParentPtr(MakeHexFileStep, "step", step);
5049
const cwd = fs.cwd();
5150
const image = try cwd.openFile(self.input_name, fs.File.OpenFlags{});
@@ -61,9 +60,12 @@ const MakeHexFileStep = struct {
6160
}
6261
while (offset < n) {
6362
if (offset % 0x10000 == 0) {
64-
try writeHexRecord(hex, 0, 0x04, &[_]u8{ @truncate(u8, offset >> 24), @truncate(u8, offset >> 16) });
63+
var two: [2]u8 = undefined;
64+
two[0] = @truncate(offset >> 24);
65+
two[1] = @truncate(offset >> 16);
66+
try writeHexRecord(hex, 0, 0x04, &two);
6567
}
66-
const i = std.math.min(hex_record_len, n - offset);
68+
const i = @min(hex_record_len, n - offset);
6769
try writeHexRecord(hex, offset % 0x10000, 0x00, read_buf[offset .. offset + i]);
6870
offset += i;
6971
}
@@ -73,11 +75,11 @@ const MakeHexFileStep = struct {
7375
fn writeHexRecord(file: fs.File, offset: usize, code: u8, bytes: []u8) !void {
7476
var record_buf: [1 + 2 + 1 + hex_record_len + 1]u8 = undefined;
7577
var record: []u8 = record_buf[0 .. 1 + 2 + 1 + bytes.len + 1];
76-
record[0] = @truncate(u8, bytes.len);
77-
record[1] = @truncate(u8, offset >> 8);
78-
record[2] = @truncate(u8, offset >> 0);
78+
record[0] = @truncate(bytes.len);
79+
record[1] = @truncate(offset >> 8);
80+
record[2] = @truncate(offset >> 0);
7981
record[3] = code;
80-
for (bytes) |b, i| {
82+
for (0.., bytes) |i, b| {
8183
record[4 + i] = b;
8284
}
8385
var checksum: u8 = 0;
@@ -94,7 +96,13 @@ const MakeHexFileStep = struct {
9496
pub fn addCustomStep(self: *std.build.Builder, customStep: anytype) *@TypeOf(customStep) {
9597
var allocated = self.allocator.create(@TypeOf(customStep)) catch unreachable;
9698
allocated.* = customStep;
97-
allocated.*.step = std.build.Step.init(.custom, @typeName(@TypeOf(customStep)), self.allocator, @TypeOf(customStep).make);
99+
var options: std.build.Step.StepOptions = undefined;
100+
options.id = .custom;
101+
options.name = @typeName(@TypeOf(customStep));
102+
options.owner = self;
103+
options.makeFn = @TypeOf(customStep).make;
104+
options.max_rss = 100 * 1024 * 1024;
105+
allocated.*.step = std.build.Step.init(options);
98106
return allocated;
99107
}
100108

main.zig

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1-
export var vector_table linksection(".vector_table") = packed struct {
1+
export var vector_table linksection(".vector_table") = extern struct {
22
initial_sp: u32 = model.memory.ram.stack_bottom,
33
reset: EntryPoint = reset,
44
system_exceptions: [14]EntryPoint = [1]EntryPoint{exception} ** 14,
55
interrupts: [model.number_of_peripherals]EntryPoint = [1]EntryPoint{exception} ** model.number_of_peripherals,
6-
const EntryPoint = fn () callconv(.C) noreturn;
6+
const EntryPoint = *const fn () callconv(.C) noreturn;
77
}{};
88

99
fn reset() callconv(.C) noreturn {
10+
model.memory.ram.prepare();
11+
Uart.prepare();
12+
log("https://github.com/markfirmware/zig-vector-table is running on a microbit!", .{});
13+
while (true) {
14+
Uart.update();
15+
}
16+
}
17+
18+
fn reset2() callconv(.C) noreturn {
1019
model.memory.ram.prepare();
1120
Uart.prepare();
1221
Timers[0].prepare();
@@ -46,8 +55,9 @@ fn exception() callconv(.C) noreturn {
4655
panicf("arm exception ipsr.isr_number {}", .{isr_number});
4756
}
4857

49-
pub fn panic(message: []const u8, trace: ?*std.builtin.StackTrace) noreturn {
58+
pub fn panic(message: []const u8, trace: ?*std.builtin.StackTrace, status_code: ?usize) noreturn {
5059
_ = trace;
60+
_ = status_code;
5161
panicf("panic(): {s}", .{message});
5262
}
5363

@@ -70,7 +80,7 @@ const Ficr = struct {
7080
pub fn isQemu() bool {
7181
return deviceId() == 0x1234567800000003;
7282
}
73-
pub const contents = @intToPtr(*[64]u32, 0x10000000);
83+
pub const contents = @as(*[64]u32, @ptrFromInt(0x10000000));
7484
};
7585

7686
const Gpio = struct {
@@ -94,19 +104,19 @@ const Gpio = struct {
94104
};
95105

96106
const Peripheral = struct {
97-
fn at(base: u32) type {
107+
fn at(comptime base: u32) type {
98108
assert(base == 0xe000e000 or base == 0x50000000 or base & 0xfffe0fff == 0x40000000);
99109
return struct {
100110
const peripheral_id = base >> 12 & 0x1f;
101111
fn mmio(address: u32, comptime T: type) *align(4) volatile T {
102-
return @intToPtr(*align(4) volatile T, address);
112+
return @as(*align(4) volatile T, @ptrFromInt(address));
103113
}
104114
fn event(offset: u32) Event {
105115
var e: Event = undefined;
106116
e.address = base + offset;
107117
return e;
108118
}
109-
fn typedRegister(offset: u32, comptime the_layout: type) type {
119+
fn typedRegister(comptime offset: u32, comptime the_layout: type) type {
110120
return struct {
111121
pub const layout = the_layout;
112122
pub noinline fn read() layout {
@@ -117,7 +127,7 @@ const Peripheral = struct {
117127
}
118128
};
119129
}
120-
fn register(offset: u32) type {
130+
fn register(comptime offset: u32) type {
121131
return typedRegister(offset, u32);
122132
}
123133
fn registerGroup(offsets: RegisterGroup) type {
@@ -277,19 +287,19 @@ pub const Pins = packed struct {
277287
}
278288
pub fn mask(self: Pins) u32 {
279289
assert(@sizeOf(Pins) == 4);
280-
return @bitCast(u32, self);
290+
return @bitCast(self);
281291
}
282292
pub fn maskUnion(self: Pins, other: Pins) Pins {
283-
return @bitCast(Pins, self.mask() | other.mask());
293+
return @bitCast(self.mask() | other.mask());
284294
}
285295
pub fn outRead(self: Pins) u32 {
286-
return (@bitCast(u32, Gpio.registers.out.read()) & self.mask()) >> self.bitPosition(0);
296+
return (@as(u32, @bitCast(Gpio.registers.out.read())) & self.mask()) >> self.bitPosition(0);
287297
}
288298
fn bitPosition(self: Pins, i: u32) u5 {
289-
return @truncate(u5, @ctz(self.mask()) + i);
299+
return @truncate(@ctz(self.mask()) + i);
290300
}
291301
pub fn read(self: Pins) u32 {
292-
return (@bitCast(u32, Gpio.registers.in.read()) & self.mask()) >> self.bitPosition(0);
302+
return (@as(u32, @bitCast(Gpio.registers.in.read())) & self.mask()) >> self.bitPosition(0);
293303
}
294304
pub fn set(self: Pins) void {
295305
Gpio.registers.out.set(self);
@@ -300,7 +310,7 @@ pub const Pins = packed struct {
300310
pub fn write(self: Pins, x: u32) void {
301311
var new = Gpio.registers.out.read().mask() & ~self.mask();
302312
new |= (x << self.bitPosition(0)) & self.mask();
303-
Gpio.registers.out.write(@bitCast(Pins, new));
313+
Gpio.registers.out.write(@bitCast(new));
304314
}
305315
pub fn writeWholeMask(self: Pins) void {
306316
Gpio.registers.out.write(self);
@@ -420,7 +430,7 @@ pub const TimeKeeper = struct {
420430

421431
pub const Timers = [_]@TypeOf(Timer(0x40008000)){ Timer(0x40008000), Timer(0x40009000), Timer(0x4000a000) };
422432

423-
fn Timer(base: u32) type {
433+
fn Timer(comptime base: u32) type {
424434
return struct {
425435
const max_width = if (base == 0x40008000) @as(u32, 32) else 16;
426436
const p = Peripheral.at(base);
@@ -505,7 +515,7 @@ const Uart = struct {
505515
var tx_queue: [3]u8 = undefined;
506516
var tx_queue_read: usize = undefined;
507517
var tx_queue_write: usize = undefined;
508-
var updater: ?fn () void = undefined;
518+
var updater: ?*fn () void = null;
509519
pub fn drainTx() void {
510520
while (tx_queue_read != tx_queue_write) {
511521
loadTxd();
@@ -531,9 +541,7 @@ const Uart = struct {
531541
registers.txd.write(tx_queue[tx_queue_read]);
532542
tx_queue_read = (tx_queue_read + 1) % tx_queue.len;
533543
tx_busy = true;
534-
if (updater) |an_updater| {
535-
an_updater();
536-
}
544+
updater.?();
537545
}
538546
}
539547
pub fn log(comptime fmt: []const u8, args: anytype) void {
@@ -567,7 +575,7 @@ const Uart = struct {
567575
}
568576
pub fn readByte() u8 {
569577
events.rx_ready.clearEvent();
570-
return @truncate(u8, registers.rxd.read());
578+
return @truncate(registers.rxd.read());
571579
}
572580
};
573581

system_model.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ pub const memory = struct {
1414
extern var _ram_data_initial_values: u8;
1515
pub fn prepare() void {
1616
const ram_bss = asSlice(&_ram_bss_start, &_ram_data_end);
17-
std.mem.set(u8, ram_bss, 0);
17+
@memset(ram_bss, 0);
1818
const ram_data = asSlice(&_ram_data_start, &_ram_data_end);
19-
std.mem.copy(u8, ram_data, @ptrCast([*]u8, &_ram_data_initial_values)[0..ram_data.len]);
19+
@memcpy(ram_data, @as([*]u8, @ptrCast(&_ram_data_initial_values))[0..ram_data.len]);
2020
}
2121
fn asSlice(start_ptr: *u8, end_ptr: *u8) []u8 {
22-
return @ptrCast([*]u8, start_ptr)[0 .. @ptrToInt(end_ptr) - @ptrToInt(start_ptr)];
22+
return @as([*]u8, @ptrCast(start_ptr))[0 .. @as(usize, @intFromPtr(end_ptr)) - @as(usize, @intFromPtr(start_ptr))];
2323
}
2424
};
2525
};

0 commit comments

Comments
 (0)