Skip to content

Commit

Permalink
Wrap zero-page operations
Browse files Browse the repository at this point in the history
  • Loading branch information
remko committed Nov 1, 2023
1 parent 84e36b5 commit 82fa4f8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
8 changes: 2 additions & 6 deletions scripts/emuwasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,6 @@ const instructions = [
],
[
"LDZ2",
// Used to be (#T2^! (i32.load16_u (local.get $t))).
// Since LDZ2 does not wrap, this was ok, but performance is the same (and this is more consistent with LDZ)
`
(local.set $t (#T))
(#set 1 1)
Expand All @@ -439,8 +437,6 @@ const instructions = [
],
[
"STZ2",
// Used to be (i32.store16 (local.get $t) (local.get $n))
// Since STZ2 does not wrap, this was ok, but peformance is actually worse.
`
(local.set $t (#T))
(local.set $n (#N))
Expand Down Expand Up @@ -877,7 +873,7 @@ function generate() {
// zero-page (not wrapping)
[
"poke2z",
`(i32.store8 #1 #2) (i32.store8 (i32.add #1 (i32.const 1)) #3)`,
`(i32.store8 #1 #2) (i32.store8 (i32.and (i32.add #1 (i32.const 1)) (i32.const 0xff)) #3)`,
],
];
for (const [regname, regidx] of [
Expand Down Expand Up @@ -912,7 +908,7 @@ function generate() {
reg + "2<zmem!",
`(i32.store8 offset=${offset} (i32.and (i32.add (local.get $${stack}p) (i32.const ${
regidx + 1
})) (i32.const 0xff)) (i32.load8_u #1)) (i32.store8 offset=${offset} (i32.and (i32.add (local.get $${stack}p) (i32.const ${regidx})) (i32.const 0xff)) (i32.load8_u (i32.add #1 (i32.const 1))))`,
})) (i32.const 0xff)) (i32.load8_u #1)) (i32.store8 offset=${offset} (i32.and (i32.add (local.get $${stack}p) (i32.const ${regidx})) (i32.const 0xff)) (i32.load8_u (i32.and (i32.add #1 (i32.const 1)) (i32.const 0xff))))`,
],
[
// Copy from memory (with expression)
Expand Down
21 changes: 13 additions & 8 deletions src/tests/opctest.tal
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,19 @@
AND ;Dict/ram-wrap test-part

( Part 9
> Testing that zero-page is not wrapping )
> Testing that zero-page is wrapping )

#1234 #ff STZ2
#ff LDZ #0100 LDA ADD #46 EQU
#00ff LDA #0000 LDA ADD #46 EQU
;Dict/zp-wrap test-part

( Part 10
> Testing that device page is wrapping
> TODO )
( Part 10
> Testing that device page is wrapping )

#1234 #ff DEO2
#ff DEI #00 DEI ADD #46 EQU
;Dict/dev-wrap test-part
#0000 DEO #00ff DEO

( end )

Expand All @@ -105,7 +109,7 @@ BRK
( name ) "Opctest 0a
( details ) "A 20 "Testing 20 "Program 0a
( author ) "By 20 "Devine 20 "Lu 20 "Linvega 0a
( date ) "31 20 "Oct 20 "2023 $2
( date ) "1 20 "Nov 20 "2023 $2

@test-part ( f name* -- )
pstr ?{
Expand Down Expand Up @@ -277,12 +281,12 @@ JMP2r
&g #8000 #0200 MUL2 [ #0000 ] EQU2 JMP2r
&h #2222 #0003 MUL2 [ #6666 ] EQU2 JMP2r
@op-div ;Dict/div !set
&a #10 #02 DIV [ #08 ] EQU JMP2r
&a #10 #06 DIV [ #02 ] EQU JMP2r
&b #20 #20 DIV [ #01 ] EQU JMP2r
&c #34 #01 DIV [ #34 ] EQU JMP2r
&d #02 #ef DIV [ #00 ] EQU JMP2r
&e #02 #00 DIV [ #00 ] EQU JMP2r
&f #1000 #0040 DIV2 [ #0040 ] EQU2 JMP2r
&f #03e8 #0006 DIV2 [ #00a6 ] EQU2 JMP2r
&g #abcd #1234 DIV2 [ #0009 ] EQU2 JMP2r
&h #8000 #0200 DIV2 [ #0040 ] EQU2 JMP2r
&i #2222 #0003 DIV2 [ #0b60 ] EQU2 JMP2r
Expand Down Expand Up @@ -420,6 +424,7 @@ JMP2r
&stack-wrap "Stack-wrap $1
&ram-wrap "RAM-wrap $1
&zp-wrap "Zeropage-wrap $1
&dev-wrap "Devices-wrap $1
&result "Result: $1
&passed 20 "passed! 0a $1
&missed "Opcode 20 "Failed 20 "-- 20 $1
Expand Down
6 changes: 3 additions & 3 deletions src/tests/suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ function loadTests() {
deo(port, value);
},
dei(port) {
throw new Error("not implemented");
return uxn.dev[port];
},
});
});
Expand Down Expand Up @@ -454,12 +454,12 @@ function loadTests() {
expect(wst()).to.eql([0xab, 0xcd]);
});

it("should not wrap around zero page", () => {
it("should wrap around zero page", () => {
uxn.load([LIT, 0xff, LDZ2]);
uxn.ram[0xff] = 0xab;
uxn.ram[0x00] = 0xcd;
uxn.eval(PROGRAM_OFFSET);
expect(wst()).to.eql([0xab, 0x80]);
expect(wst()).to.eql([0xab, 0xcd]);
});
});

Expand Down
16 changes: 8 additions & 8 deletions src/uxn.wat
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
(local.set $t (i32.load8_u offset=0x10000 (local.get $wstp)))
(local.set $wstp (i32.and (i32.add (local.get $wstp) (i32.const 255)) (i32.const 0xff)))
(i32.store8 offset=0x10000 (i32.and (i32.add (local.get $wstp) (i32.const 1)) (i32.const 0xff)) (i32.load8_u (local.get $t)))
(i32.store8 offset=0x10000 (local.get $wstp) (i32.load8_u (i32.add (local.get $t) (i32.const 1))))
(i32.store8 offset=0x10000 (local.get $wstp) (i32.load8_u (i32.and (i32.add (local.get $t) (i32.const 1)) (i32.const 0xff))))
(br $loop)

);; STZ2
Expand All @@ -409,7 +409,7 @@
(local.set $l (i32.load8_u offset=0x10000 (i32.and (i32.add (local.get $wstp) (i32.const 2)) (i32.const 0xff))))
(local.set $wstp (i32.and (i32.add (local.get $wstp) (i32.const 3)) (i32.const 0xff)))
(i32.store8 (local.get $t) (local.get $l))
(i32.store8 (i32.add (local.get $t) (i32.const 1)) (local.get $n))
(i32.store8 (i32.and (i32.add (local.get $t) (i32.const 1)) (i32.const 0xff)) (local.get $n))
(br $loop)

);; LDR2
Expand Down Expand Up @@ -867,7 +867,7 @@
(local.set $t (i32.load8_u offset=0x10100 (local.get $rstp)))
(local.set $rstp (i32.and (i32.add (local.get $rstp) (i32.const 255)) (i32.const 0xff)))
(i32.store8 offset=0x10100 (i32.and (i32.add (local.get $rstp) (i32.const 1)) (i32.const 0xff)) (i32.load8_u (local.get $t)))
(i32.store8 offset=0x10100 (local.get $rstp) (i32.load8_u (i32.add (local.get $t) (i32.const 1))))
(i32.store8 offset=0x10100 (local.get $rstp) (i32.load8_u (i32.and (i32.add (local.get $t) (i32.const 1)) (i32.const 0xff))))
(br $loop)

);; STZ2r
Expand All @@ -876,7 +876,7 @@
(local.set $l (i32.load8_u offset=0x10100 (i32.and (i32.add (local.get $rstp) (i32.const 2)) (i32.const 0xff))))
(local.set $rstp (i32.and (i32.add (local.get $rstp) (i32.const 3)) (i32.const 0xff)))
(i32.store8 (local.get $t) (local.get $l))
(i32.store8 (i32.add (local.get $t) (i32.const 1)) (local.get $n))
(i32.store8 (i32.and (i32.add (local.get $t) (i32.const 1)) (i32.const 0xff)) (local.get $n))
(br $loop)

);; LDR2r
Expand Down Expand Up @@ -1331,15 +1331,15 @@
(local.set $t (i32.load8_u offset=0x10000 (local.get $wstp)))
(local.set $wstp (i32.and (i32.add (local.get $wstp) (i32.const 254)) (i32.const 0xff)))
(i32.store8 offset=0x10000 (i32.and (i32.add (local.get $wstp) (i32.const 1)) (i32.const 0xff)) (i32.load8_u (local.get $t)))
(i32.store8 offset=0x10000 (local.get $wstp) (i32.load8_u (i32.add (local.get $t) (i32.const 1))))
(i32.store8 offset=0x10000 (local.get $wstp) (i32.load8_u (i32.and (i32.add (local.get $t) (i32.const 1)) (i32.const 0xff))))
(br $loop)

);; STZ2k
(local.set $t (i32.load8_u offset=0x10000 (local.get $wstp)))
(local.set $n (i32.load8_u offset=0x10000 (i32.and (i32.add (local.get $wstp) (i32.const 1)) (i32.const 0xff))))
(local.set $l (i32.load8_u offset=0x10000 (i32.and (i32.add (local.get $wstp) (i32.const 2)) (i32.const 0xff))))
(i32.store8 (local.get $t) (local.get $l))
(i32.store8 (i32.add (local.get $t) (i32.const 1)) (local.get $n))
(i32.store8 (i32.and (i32.add (local.get $t) (i32.const 1)) (i32.const 0xff)) (local.get $n))
(br $loop)

);; LDR2k
Expand Down Expand Up @@ -1792,15 +1792,15 @@
(local.set $t (i32.load8_u offset=0x10100 (local.get $rstp)))
(local.set $rstp (i32.and (i32.add (local.get $rstp) (i32.const 254)) (i32.const 0xff)))
(i32.store8 offset=0x10100 (i32.and (i32.add (local.get $rstp) (i32.const 1)) (i32.const 0xff)) (i32.load8_u (local.get $t)))
(i32.store8 offset=0x10100 (local.get $rstp) (i32.load8_u (i32.add (local.get $t) (i32.const 1))))
(i32.store8 offset=0x10100 (local.get $rstp) (i32.load8_u (i32.and (i32.add (local.get $t) (i32.const 1)) (i32.const 0xff))))
(br $loop)

);; STZ2kr
(local.set $t (i32.load8_u offset=0x10100 (local.get $rstp)))
(local.set $n (i32.load8_u offset=0x10100 (i32.and (i32.add (local.get $rstp) (i32.const 1)) (i32.const 0xff))))
(local.set $l (i32.load8_u offset=0x10100 (i32.and (i32.add (local.get $rstp) (i32.const 2)) (i32.const 0xff))))
(i32.store8 (local.get $t) (local.get $l))
(i32.store8 (i32.add (local.get $t) (i32.const 1)) (local.get $n))
(i32.store8 (i32.and (i32.add (local.get $t) (i32.const 1)) (i32.const 0xff)) (local.get $n))
(br $loop)

);; LDR2kr
Expand Down

0 comments on commit 82fa4f8

Please sign in to comment.