Skip to content

Commit

Permalink
[MOS6502] Fix to use W65C816 memory
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Jun 18, 2024
1 parent 0427ca0 commit ee86a7d
Show file tree
Hide file tree
Showing 9 changed files with 1,148 additions and 1,228 deletions.
3 changes: 3 additions & 0 deletions debugger/mos6502/pins_mos6502.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ void PinsMos6502::checkHardwareType() {
pinMode(PIN_BE, OUTPUT);
}
// Keep PIN_SO HIGH using pullup
// Set 16-bit memory
Target6502.setMems(mos6502::Memory);
} else {
// PIN_PHI1O/W65C816_ABORT keeps HIGH, means W65C816S.
Expand All @@ -195,6 +196,8 @@ void PinsMos6502::checkHardwareType() {
// Enable BE
digitalWriteFast(PIN_BE, HIGH);
pinMode(PIN_BE, OUTPUT);
// Set 24-bit memory
Target6502.setMems(w65c816::Memory);
}
}

Expand Down
4 changes: 2 additions & 2 deletions debugger/mos6502/regs_mos6502.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ void RegsMos6502::print() const {
if (Pins.native65816()) {
// clang-format off
// 012345678901234567890123456789012345678901234567890123456789012
static constexpr char line[] = "K=xx PC=xxxx S=xxxx D=xxxx B=xx X=xxxx Y=xxxx C=xxxx P=NVMXDIZC";
static constexpr char line[] = "K:PC=xx:xxxx S=xxxx D=xxxx B=xx X=xxxx Y=xxxx C=xxxx P=NVMXDIZC";
// clang-format on
static auto &buffer = *new CharBuffer(line);
buffer.hex8(2, _pbr);
buffer.hex8(5, _pbr);
buffer.hex16(8, _pc);
buffer.hex16(15, _s);
buffer.hex16(22, _d);
Expand Down
51 changes: 41 additions & 10 deletions samples/w65c816/arith.asm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ stack = *-1
initialize:
clc
xce ; native mode
longa off
rep #P_X ; 16-bit index
longi on
ldx #stack
Expand All @@ -33,24 +34,41 @@ initialize:
;;; @param A char
;;; @clobber A
putchar:
pha ; save A
putchar_loop:
php
sep #P_M ; 8-bit memory
longa off
jsr _putchar
plp
rts
_putchar:
pha
_putchar_loop:
lda ACIA_status
and #TDRE_bm
beq putchar_loop
beq _putchar_loop
pla ; restore A
sta ACIA_data
rts

newline:
php
sep #P_M ; 8-bit memory
longa off
lda #$0D
jsr putchar
jsr _putchar
lda #$0A
bra putchar
jsr _putchar
plp
rts

putspace:
php
sep #P_M ; 8-bit memory
longa off
lda #' '
bra putchar
jsr _putchar
plp
rts

;;; Print "X op Y"
;;; @params A op letter
Expand Down Expand Up @@ -98,7 +116,6 @@ comp_out:
jsr expr
jmp newline


arith:
ldx #18000
ldy #28000
Expand Down Expand Up @@ -276,8 +293,15 @@ arith:
addsi2:
php
rep #P_M ; 16-bit memory
jsr add16
longa on
txa
phy
clc
adc 1,S
tax
ply
plp
longa off
rts

;;; Subtraction
Expand All @@ -289,8 +313,14 @@ addsi2:
subsi2:
php
rep #P_M ; 16-bit memory
jsr sub16
txa
phy
sec
sbc 1,S
tax
ply
plp
longa off
rts

;;; Signed compare
Expand All @@ -302,9 +332,10 @@ subsi2:
;;; C=-1; BMI (minuend < subtrahend)
longa off
cmpsi2:
php
rep #P_M ; 16-bit memory
jsr cmp16
sep #P_M ; 8-bit memory
plp
rts

;;; Multiply: result = multiplicand * multiplier
Expand Down
74 changes: 14 additions & 60 deletions samples/w65c816/arith.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,22 @@
;;; Print signed 16-bit integer as decimal
;;; @param X value
;;; @clobber C
longa off
print_int16:
php
sep #P_M ; 8-bit memory
longa off
rep #P_M ; 16-bit memory
longa on
phy ; save Y
phx ; save X
lda 2,S ; hi(value)
txa
bpl print_int16_print
lda #'-'
jsr putchar
rep #P_M ; 16-bit memory
longa on
txa
eor #$FFFF
inc A
tax
print_int16_print:
rep #P_M ; 16-bit memory
longa off
jsr print_uint16
plx ; restore X
ply ; restore Y
Expand All @@ -34,12 +31,10 @@ print_int16_print:
;;; @clobber C X Y
longa on
print_uint16:
inx
dex
txa
beq print_uint16_digit ; branch if value==0
print_uint16_inner:
inx
dex
txa
beq print_uint16_exit
ldy #10
jsr udiv16
Expand All @@ -52,36 +47,6 @@ print_uint16_digit:
print_uint16_exit:
rts

;;; Addition: result = summand + addend
;;; @param X summand
;;; @param Y addend
;;; @return X result
;;; @clobber C
longa on
add16:
txa
phy
clc
adc 1,S
tax
ply
rts

;;; Subtraction: result = minuend - subtrahend
;;; @param X minuend
;;; @param Y subtrahend
;;; @return X result
;;; @clobber C
longa on
sub16:
txa
phy
sec
sbc 1,S
tax
ply
rts

;;; Signed compare: minuend - subtrahend
;;; @param X minuend
;;; @param Y subtrahend
Expand Down Expand Up @@ -119,7 +84,7 @@ cmp16_eq:
;;; @clobber C Y
longa on
umul16:
lda #0
lda #0
pha ; result (1,S)
bra umul16_check
umul16_loop:
Expand All @@ -135,9 +100,8 @@ umul16_sr:
txa
asl A ; multiplicand <<= 1
tax
umul16_check:
iny
dey
umul16_check:
tya
bne umul16_loop ; while multiplier != 0
umul16_end:
plx ; X=result
Expand All @@ -154,18 +118,14 @@ mul16:
txa
eor 1,S
sta 1,S ; save (multiplicand^multiplier)
iny
dey
bpl mul16_multiplicand
tya
bpl mul16_multiplicand
eor #$FFFF
inc A
tay ; negate multiplier
mul16_multiplicand:
inx
dex
bpl mul16_multiply
txa
bpl mul16_multiply
eor #$FFFF
inc A
tax ; negate multiplicand
Expand All @@ -188,10 +148,8 @@ mul16_return:
;;; @clobber C
longa on
udiv16:
iny
dey
beq udiv16_return ; branch if Y==0
tya ; C=divisor
beq udiv16_return ; branch if Y==0
ldy #0 ; y=bits
udiv16_prep_loop:
iny
Expand Down Expand Up @@ -237,18 +195,14 @@ div16:
txa
eor 1,S
sta 1,S ; save (dividend^divisor)
iny
dey
bpl div16_dividend
tya
bpl div16_dividend
eor #$FFFF
inc A
tay ; negate divisor
div16_dividend:
inx
dex
bpl div16_multiply
txa
bpl div16_multiply
eor #$FFFF
inc A
tax ; negate dividend
Expand Down
Loading

0 comments on commit ee86a7d

Please sign in to comment.