Skip to content

Commit a491e64

Browse files
authored
Aviron: Fix SBIW instruction setting SREG incorrectly (#724)
* Aviron: Cleanup testrunner IO to look more like main's * Aviron: Fix sbiw instruction setting SREG, add tests
1 parent cebe3b1 commit a491e64

File tree

7 files changed

+274
-6
lines changed

7 files changed

+274
-6
lines changed

sim/aviron/src/lib/Cpu.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ const instructions = struct {
755755
cpu.sreg.z = (res == 0);
756756
cpu.sreg.n = ((res & 0x8000) != 0);
757757
cpu.sreg.c = ((src & 0x8000) == 0) and ((res & 0x8000) != 0);
758-
cpu.sreg.v = cpu.sreg.c;
758+
cpu.sreg.v = ((src & 0x8000) != 0) and ((res & 0x8000) == 0);
759759
cpu.sreg.s = (cpu.sreg.n != cpu.sreg.v);
760760
}
761761

sim/aviron/src/testrunner.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ fn run_test(
172172
};
173173

174174
// Check if it was a system exit (via IO)
175-
const exit_mode: ExitMode = if (io.exit_requested)
176-
.{ .system_exit = io.exit_code.? }
175+
const exit_mode: ExitMode = if (io.exit_code) |code|
176+
.{ .system_exit = code }
177177
else switch (result) {
178178
.breakpoint => .breakpoint,
179179
.enter_sleep_mode => .enter_sleep_mode,
@@ -310,7 +310,6 @@ const IO = struct {
310310

311311
// Exit status tracking
312312
exit_code: ?u8 = null,
313-
exit_requested: bool = false,
314313

315314
const DataBusType = aviron.Bus(.{ .address_type = u24 });
316315
const IOBusType = aviron.IOBus;
@@ -433,7 +432,6 @@ const IO = struct {
433432
switch (reg) {
434433
.exit => {
435434
io.exit_code = value & mask;
436-
io.exit_requested = true;
437435
},
438436

439437
.stdio => io.stdout.append(value & mask) catch @panic("out of memory"),
@@ -497,7 +495,9 @@ const IO = struct {
497495

498496
fn dev_check_exit(ctx: *anyopaque) ?u8 {
499497
const io: *IO = @ptrCast(@alignCast(ctx));
500-
if (io.exit_requested) return io.exit_code;
498+
if (io.exit_code) |code| {
499+
return code;
500+
}
501501
return null;
502502
}
503503
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! {
2+
//! "exit": "breakpoint",
3+
//! "cpus": ["atmega2560", "atmega328p"],
4+
//! "precondition": {
5+
//! "r24": 0,
6+
//! "r25": 0,
7+
//! "r26": 0,
8+
//! "r27": 128,
9+
//! "r28": 8,
10+
//! "r29": 0,
11+
//! "r30": 255,
12+
//! "r31": 255 },
13+
//! "postcondition": {
14+
//! "r16": 21,
15+
//! "r17": 24,
16+
//! "r18": 2,
17+
//! "r19": 20,
18+
//! "r24": 248,
19+
//! "r25": 255,
20+
//! "r26": 255,
21+
//! "r27": 127,
22+
//! "r28": 0,
23+
//! "r29": 0,
24+
//! "r30": 192,
25+
//! "r31": 255 }
26+
//! }
27+
28+
; Test case 1: 0x0000 - 0x08 = 0xFFF8
29+
; Precondition: r25:r24 = 0x0000
30+
; Expected: r25:r24 = 0xFFF8, C=1, N=1, V=0, S=1, Z=0
31+
; SREG should be 0x15 (C=1, Z=0, N=1, V=0, S=1)
32+
sbiw r24, 0x8
33+
in r16, 0x3F ; Save SREG to r16
34+
35+
; Test case 2: 0x8000 - 0x01 = 0x7FFF (overflow)
36+
; Precondition: r27:r26 = 0x8000
37+
; Expected: r27:r26 = 0x7FFF, C=0, N=0, V=1, S=1, Z=0
38+
; SREG should be 0x18 (C=0, Z=0, N=0, V=1, S=1)
39+
sbiw r26, 0x1
40+
in r17, 0x3F ; Save SREG to r17
41+
42+
; Test case 3: 0x0008 - 0x08 = 0x0000 (zero result)
43+
; Precondition: r29:r28 = 0x0008
44+
; Expected: r29:r28 = 0x0000, C=0, N=0, V=0, S=0, Z=1
45+
; SREG should be 0x02 (C=0, Z=1, N=0, V=0, S=0)
46+
sbiw r28, 0x8
47+
in r18, 0x3F ; Save SREG to r18
48+
49+
; Test case 4: 0xFFFF - 0x3F = 0xFFC0
50+
; Precondition: r31:r30 = 0xFFFF
51+
; Expected: r31:r30 = 0xFFC0, C=0, N=1, V=0, S=1, Z=0
52+
; SREG should be 0x14 (C=0, Z=0, N=1, V=0, S=1)
53+
sbiw r30, 0x3F
54+
in r19, 0x3F ; Save SREG to r19
55+
56+
break
320 Bytes
Binary file not shown.

sim/aviron/testsuite/instructions/sbiw-atmega2560.elf.json

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
320 Bytes
Binary file not shown.

sim/aviron/testsuite/instructions/sbiw-atmega328p.elf.json

Lines changed: 106 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)