Skip to content

Commit

Permalink
Merge pull request #136 from davidgiven/z80
Browse files Browse the repository at this point in the history
Fix incorrect casting of 16 bit to 32 bit signed values on the Z80.
  • Loading branch information
davidgiven authored Jan 17, 2024
2 parents 596072f + d512b6b commit e64b72b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/cowbe/archz80.cow.ng
Original file line number Diff line number Diff line change
Expand Up @@ -1881,7 +1881,7 @@ gen hlhl := CAST24(hl, sext==0)

gen hlhl := CAST24(hl, sext!=0) uses a
{
E_mov(REG_A, REG_L);
E_mov(REG_A, REG_H);
E_add(REG_A, REG_A);
E_sbc(REG_A, REG_A);
E_exx();
Expand Down
8 changes: 8 additions & 0 deletions tests/casts.good
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ mone as uint8: yes
mone as uint32: yes
mone as int8: yes
mone as int32: yes
twohundred as uint8: yes
twohundred as uint32: yes
twohundred as int8: yes
twohundred as int32: yes
casts2
one as uint8: yes
one as uint32: yes
Expand All @@ -34,6 +38,10 @@ mone as uint8: yes
mone as uint32: yes
mone as int8: yes
mone as int32: yes
twohundred as uint8: yes
twohundred as uint32: yes
twohundred as int8: yes
twohundred as int32: yes
castu4
one as uint8: yes
one as uint16: yes
Expand Down
10 changes: 10 additions & 0 deletions tests/casts.test.cow
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ casts1();
sub castu2() is
var one: uint16 := 1;
var mone: uint16 := -1;
var twohundred: uint16 := 200;

print("castu2\n");
print("one as uint8"); if (one as uint8) == 1 then yes(); else no(); end if;
Expand All @@ -46,12 +47,17 @@ sub castu2() is
print("mone as uint32"); if (mone as uint32) == 0xffff then yes(); else no(); end if;
print("mone as int8"); if (mone as int8) == -1 then yes(); else no(); end if;
print("mone as int32"); if (mone as int32) == 0xffff then yes(); else no(); end if;
print("twohundred as uint8"); if (twohundred as uint8) == 200 then yes(); else no(); end if;
print("twohundred as uint32"); if (twohundred as uint32) == 200 then yes(); else no(); end if;
print("twohundred as int8"); if (twohundred as int8) == 200 then yes(); else no(); end if;
print("twohundred as int32"); if (twohundred as int32) == 200 then yes(); else no(); end if;
end sub;
castu2();

sub casts2() is
var one: int16 := 1;
var mone: int16 := -1;
var twohundred: int16 := 200;

print("casts2\n");
print("one as uint8"); if (one as uint8) == 1 then yes(); else no(); end if;
Expand All @@ -62,6 +68,10 @@ sub casts2() is
print("mone as uint32"); if (mone as uint32) == 0xffffffff then yes(); else no(); end if;
print("mone as int8"); if (mone as int8) == -1 then yes(); else no(); end if;
print("mone as int32"); if (mone as int32) == -1 then yes(); else no(); end if;
print("twohundred as uint8"); if (twohundred as uint8) == 200 then yes(); else no(); end if;
print("twohundred as uint32"); if (twohundred as uint32) == 200 then yes(); else no(); end if;
print("twohundred as int8"); if (twohundred as int8) == 200 then yes(); else no(); end if;
print("twohundred as int32"); if (twohundred as int32) == 200 then yes(); else no(); end if;
end sub;
casts2();

Expand Down

0 comments on commit e64b72b

Please sign in to comment.