6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sun Nov 10, 2024 7:18 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun Mar 12, 2023 3:53 pm 
Offline
User avatar

Joined: Fri Feb 17, 2023 11:59 pm
Posts: 163
Location: Lviv, Ukraine
I'm trying to improve the jumptable for my simple REPL environment.
Basically I'm mapping every ASCII character to some routine as such:
Code:
.segment "JUMPTABLE"
CMD_JUMPTABLE:
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    .word cmd_noop, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, cmd_printmem
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, cmd_jmp, 0, 0, cmd_printmem, 0, 0
    .word 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

.code
  ; ...
  ; Register A contains ASCII character code
    asl a  ; jumptable addresses are word-sized
    tax
    jmp (CMD_JUMPTABLE, X)


This works, but I really want to get rid of all the zeroes and do something like this:
Code:
.segment "JUMPTABLE"
.align $100
JUMPTABLE_START:
    .repeat 128
    .word $0
    .endrepeat
; The following lines are pseudocode.
JUMPTABLE_START + ' ' = .word cmd_noop
JUMPTABLE_START + 'j' = .word cmd_jmp
JUMPTABLE_START + 'm' = .word cmd_printmem
JUMPTABLE_START + '?' = .word cmd_printmem


Any thoughts on whether this is achievable with ca65?

_________________
/Andrew

deck65 - 6502 slab with screen and keyboard | ПК-88 - SBC based on KM1810VM88 (Ukrainian i8088 clone) | leo80 - simple Z80 SBC
nice65 - 6502 assembly linter | My parts, footprints & 3D models for KiCad/FreeCAD


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 12, 2023 5:14 pm 
Offline

Joined: Sat Feb 19, 2022 10:14 pm
Posts: 147
Try using a macro that takes the ascii character and jump address as parameters. Have the macro keep track of the current position (using the set command) and use the repeat control command to fill in the empty entries up to that point. So something like this:
Code:
.macro jmptbl char addr
    .repeat char - JTcount - 1
       .word 0
    .endrep
    .word addr
    JTcount .set char
.endmacro
JUMPTABLE_START:
JTcount .set 0  ; initialize count
jmptbl ' ' cmd_noop
jmptbl 'j' cmd_jmp
jmptbl 'm' cmd_printmem
jmptbl '?' cmd_printmem
You might need to do some conversion in using the ascii character in expressions. I'm not sure if ca65 will treat it as a value.


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 12, 2023 11:00 pm 
Offline
User avatar

Joined: Fri Feb 17, 2023 11:59 pm
Posts: 163
Location: Lviv, Ukraine
tmr4 wrote:
Try using a macro that takes the ascii character and jump address as parameters. Have the macro keep track of the current position (using the set command) and use the repeat control command to fill in the empty entries up to that point.


This is cool! I assume the calls need to be sorted by "char" in ascending order, right?

Thanks for the tip, I'll try it out. I missed the .set command when I went through the documentation!

_________________
/Andrew

deck65 - 6502 slab with screen and keyboard | ПК-88 - SBC based on KM1810VM88 (Ukrainian i8088 clone) | leo80 - simple Z80 SBC
nice65 - 6502 assembly linter | My parts, footprints & 3D models for KiCad/FreeCAD


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 12, 2023 11:12 pm 
Offline
User avatar

Joined: Fri Feb 17, 2023 11:59 pm
Posts: 163
Location: Lviv, Ukraine
With a minor tweak it worked - thanks!

Code:
.macro jmptbl char, addr
    .repeat char - jt_count  ; removed -1
        .word 0
    .endrep
    .word addr
    jt_count .set char + 1  ; added +1
.endmacro

_________________
/Andrew

deck65 - 6502 slab with screen and keyboard | ПК-88 - SBC based on KM1810VM88 (Ukrainian i8088 clone) | leo80 - simple Z80 SBC
nice65 - 6502 assembly linter | My parts, footprints & 3D models for KiCad/FreeCAD


Top
 Profile  
Reply with quote  
PostPosted: Sun Mar 12, 2023 11:53 pm 
Offline

Joined: Sat Feb 19, 2022 10:14 pm
Posts: 147
and3rson wrote:
I assume the calls need to be sorted by "char" in ascending order, right?
Yes, that's important.
and3rson wrote:
With a minor tweak it worked - thanks!
Ha, ha. Off by one has always been my bane!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: