Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use real symbols instead of binary + N #31

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 deletions src/binary.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
.EXPORT binary

##########
# start of the binary to execute
#
Expand All @@ -15,27 +13,32 @@
# also used by heap.s in libxib. The VM without a binary must not use the heap unless it takes
# care the overlap between the heap and the appended binary image.

binary:
# Header of the binary to execute. In this case there is no built-in binary,
# so we just define the symbols to make the VM compile

# After this we expect:
#
# binary_start_address:
# db 0x12345 # start at address 0x12345; or
# db -1 # start at the reset vector (default 6502 behavior)
#
# binary_load_address:
# db 0xc000 # load address of the binary image in 6502 memory
#
# tracing:
# db 1 # 0 - disable tracing, -1 - trace always, >0 - start tracing after passing that address
#
# vm_callback:
# db 0xabcde # optional callback function to call before each instruction
#
# binary_length:
# db 0x4000 # size of the binary image
#
# binary_data:
# ds 0x4000, 0 # binary image data
.EXPORT binary_start_address
.EXPORT binary_load_address
.EXPORT binary_enable_tracing
.EXPORT binary_vm_callback
.EXPORT binary_length
.EXPORT binary_data

# Initial pc value, or -1 to use the reset vector
+0 = binary_start_address:

# Load address of the binary image in 6502 memory
+1 = binary_load_address:

# Tracing (0 - disable tracing, -1 - trace always, >0 - tracing past given address)
+2 = binary_enable_tracing:

# Optional callback function to call before each instruction, zero if not used
+3 = binary_vm_callback:

# Size of the binary image
+4 = binary_length:

# Binary image data
+5 = binary_data:

.EOF
19 changes: 10 additions & 9 deletions src/exec.s
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
.EXPORT execute_nop
.EXPORT invalid_opcode

# From binary.s
.IMPORT binary
# From the linked 6502 binary
.IMPORT binary_enable_tracing
.IMPORT binary_vm_callback

# From error.s
.IMPORT report_error
Expand All @@ -30,28 +31,28 @@ execute:

execute_loop:
# Skip tracing if disabled
jz [binary + 2], execute_tracing_done
jz [binary_enable_tracing], execute_tracing_done

# If the [binary + 2] flag is positive, we use it as an address
# If the [binary_enable_tracing] flag is positive, we use it as an address
# starting from where we should turn on tracing
eq [binary + 2], [reg_pc], [rb + tmp]
eq [binary_enable_tracing], [reg_pc], [rb + tmp]
jz [rb + tmp], execute_tracing_different_address

# Address match, turn on tracing
add -1, 0, [binary + 2]
add -1, 0, [binary_enable_tracing]

execute_tracing_different_address:
# Print trace if enabled
eq [binary + 2], -1, [rb + tmp]
eq [binary_enable_tracing], -1, [rb + tmp]
jz [rb + tmp], execute_tracing_done

call print_trace

execute_tracing_done:
# Call the callback if enabled
jz [binary + 3], execute_callback_done
jz [binary_vm_callback], execute_callback_done

call [binary + 3]
call [binary_vm_callback]
jnz [rb - 2], execute_callback_done

# Callback returned 0, halt
Expand Down
24 changes: 14 additions & 10 deletions src/func_test_header.s
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@

# The binary is available in git repository https://github.com/Klaus2m5/6502_65C02_functional_tests

.EXPORT binary
.EXPORT binary_start_address
.EXPORT binary_load_address
.EXPORT binary_enable_tracing
.EXPORT binary_vm_callback

.IMPORT func_test_callback

binary:
# Start address for the functional test binary
# Can be found using bin_files/6502_functional_test.lst, search for "Program start address is at"
# Initial pc value; can be found using bin_files/6502_functional_test.lst, search for "Program start address is at"
binary_start_address:
db 0x0400

# Load address for the functional test binary
# Load address for the functional test binary
binary_load_address:
db 0

# Set up tracing
# 0 - disable tracing
# -1 - trace always
# >0 - start tracing after passing that address
# Tracing (0 - disable tracing, -1 - trace always, >0 - tracing past given address)
binary_enable_tracing:
db 0

# Callback address
# Optional callback function to call before each instruction, zero if not used
binary_vm_callback:
db func_test_callback

# Symbols binary_length and binary data are provided by func_test_data.o

.EOF
20 changes: 11 additions & 9 deletions src/memory.s
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
.EXPORT push
.EXPORT pull

# From binary.s
.IMPORT binary
# From the linked 6502 binary
.IMPORT binary_load_address
.IMPORT binary_length
.IMPORT binary_data

# From error.s
.IMPORT report_error
Expand All @@ -28,13 +30,13 @@ init_memory:
# Initialize memory space for the 6502.

# Validate the load address is a valid 16-bit number
add [binary + 1], 0, [rb - 1]
add [binary_load_address], 0, [rb - 1]
add 0xffff, 0, [rb - 2]
arb -2
call check_range

# Validate the image will fit to 16-bits when loaded there
add [binary + 1], [binary + 4], [rb + tgt]
add [binary_load_address], [binary_length], [rb + tgt]
lt 0x10000, [rb + tgt], [rb + tmp]
jz [rb + tmp], init_memory_load_address_ok

Expand All @@ -44,19 +46,19 @@ init_memory:

init_memory_load_address_ok:
# The 6502 memory space will start where the binary starts now
add binary + 5, 0, [mem]
add binary_data, 0, [mem]

# Do we need to move the binary to a different load address?
jz [binary + 1], init_memory_done
jz [binary_load_address], init_memory_done

# Yes, calculate beginning address of the source (binary),
add binary + 5, 0, [rb + src]
add binary_data, 0, [rb + src]

# Calculate the beginning address of the target ([mem] + [load])
add [mem], [binary + 1], [rb + tgt]
add [mem], [binary_load_address], [rb + tgt]

# Number of bytes to copy
add [binary + 4], 0, [rb + cnt]
add [binary_length], 0, [rb + cnt]

init_memory_loop:
# Move the image from src to tgt (iterating in reverse direction)
Expand Down
20 changes: 14 additions & 6 deletions src/msbasic_header.s
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
# This the header for the MS Basic binary.
# It needs to be linked immediately after binary.o and immediately before the MS Basic binary itself.

.EXPORT binary
.EXPORT binary_start_address
.EXPORT binary_load_address
.EXPORT binary_enable_tracing
.EXPORT binary_vm_callback

binary:
# Start address for the MS Basic binary, set to -1 to use the reset vector.
# Initial pc value; use the reset vector
binary_start_address:
db -1

# Load address for the MS Basic binary, needs to match the BASROM memory region in $(MSBASICDIR)/vm6502.cfg.
# Load address for the MS Basic binary; needs to match the BASROM memory region in $(MSBASICDIR)/vm6502.cfg.
binary_load_address:
db 0xc000

# Disable tracing
# Tracing (0 - disable tracing, -1 - trace always, >0 - tracing past given address)
binary_enable_tracing:
db 0

# No callback
# Optional callback function to call before each instruction, zero if not used
binary_vm_callback:
db 0

# Symbols binary_length and binary data are provided by msbasic_data.o

.EOF
6 changes: 3 additions & 3 deletions src/state.s
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
.EXPORT pack_sr
.EXPORT unpack_sr

# From binary.s
.IMPORT binary
# From the linked 6502 binary
.IMPORT binary_start_address

# From error.s
.IMPORT report_error
Expand Down Expand Up @@ -62,7 +62,7 @@ init_state:
arb -1

# Load the start address to pc
add [binary + 0], 0, [reg_pc]
add [binary_start_address], 0, [reg_pc]

# If it is -1, use the reset vector
eq [reg_pc], -1, [rb + tmp]
Expand Down