-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrec.zig
142 lines (114 loc) · 4.57 KB
/
srec.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
pub fn writer(comptime Address: type, inner_writer: anytype, options: Writer_Options) !Writer(Address, @TypeOf(inner_writer)) {
return Writer(Address, @TypeOf(inner_writer)).init(inner_writer, options);
}
pub const Writer_Options = struct {
line_ending: ?[]const u8 = null,
header_data: []const u8 = "",
pretty: bool = false,
};
pub fn Writer(comptime Address: type, comptime Inner_Writer: type) type {
switch (@typeInfo(Address).Int.bits) {
16, 24, 32 => {},
else => @compileError("Invalid address type; must be u32, u24, or u16"),
}
return struct {
inner: Inner_Writer,
data_rec_count: usize,
line_ending: []const u8,
pretty: bool,
const Self = @This();
pub fn init(inner_writer: Inner_Writer, options: Writer_Options) !Self {
var self = Self{
.inner = inner_writer,
.data_rec_count = 0,
.line_ending = options.line_ending orelse default_line_ending(),
.pretty = options.pretty,
};
try self.write_record('0', 0, options.header_data);
return self;
}
fn write_byte(self: *Self, d: u8) !void {
try self.inner.writeByte("0123456789ABCDEF"[d >> 4]);
try self.inner.writeByte("0123456789ABCDEF"[@as(u4, @truncate(d))]);
}
fn write_record(self: *Self, record_type: u8, address: anytype, data: []const u8) !void {
const A = @TypeOf(address);
std.debug.assert(@bitSizeOf(A) <= 32);
try self.inner.writeByte('S');
try self.inner.writeByte(record_type);
if (self.pretty) {
try self.inner.writeByte(' ');
}
var checksum: u8 = @intCast(data.len + 3);
try self.write_byte(checksum);
if (self.pretty) {
try self.inner.writeByte(' ');
}
if (comptime @bitSizeOf(A) > 24) {
const address_part: u8 = @truncate(address >> 24);
try self.write_byte(address_part);
checksum +%= address_part;
}
if (comptime @bitSizeOf(A) > 16) {
const address_part: u8 = @truncate(address >> 16);
try self.write_byte(address_part);
checksum +%= address_part;
}
const address_high: u8 = @truncate(address >> 8);
try self.write_byte(address_high);
checksum +%= address_high;
const address_low: u8 = @truncate(address);
try self.write_byte(address_low);
checksum +%= address_low;
if (self.pretty) {
try self.inner.writeByte(' ');
}
for (data) |d| {
try self.write_byte(d);
checksum +%= d;
}
if (self.pretty) {
try self.inner.writeByte(' ');
}
try self.write_byte(checksum ^ 0xFF);
try self.inner.writeAll(self.line_ending);
self.data_rec_count += 1;
}
pub fn write(self: *Self, address: Address, data: []const u8) !void {
var start = address;
var remaining = data;
const data_record_type = switch(@bitSizeOf(Address)) {
16 => '1',
24 => '2',
32 => '3',
else => unreachable,
};
while (remaining.len > 32) {
try self.write_record(data_record_type, start, remaining[0..32]);
start += 32;
remaining = remaining[32..];
}
try self.write_record(data_record_type, start, remaining);
}
pub fn finish(self: *Self, termination_address: Address) !void {
if (self.data_rec_count <= 0xFFFF) {
const count: u16 = @intCast(self.data_rec_count);
try self.write_record('5', count, "");
} else if (self.data_rec_count <= 0xFFFFFF) {
const count: u24 = @intCast(self.data_rec_count);
try self.write_record('6', count, "");
}
const termination_record_type = switch (@bitSizeOf(Address)) {
16 => '9',
24 => '8',
32 => '7',
else => unreachable,
};
try self.write_record(termination_record_type, termination_address, "");
}
};
}
fn default_line_ending() []const u8 {
return if (@import("builtin").target.os.tag == .windows) "\r\n" else "\n";
}
const std = @import("std");