Skip to content

Commit

Permalink
bios: bank: add support for ROM wraparound emulation
Browse files Browse the repository at this point in the history
  • Loading branch information
asiekierka committed Jul 28, 2024
1 parent f148892 commit 58547c6
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 77 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ SRC_OS := os
# Defines
# -------

SRC_BIOS += bios/bank/$(BIOS_BANK_MAPPER)
SRC_BIOS += bios/timer/$(BIOS_TIMER_RTC)
DEFINES += -DBIOS_BANK_MAPPER_$(BIOS_BANK_MAPPER)
DEFINES += -DBIOS_BANK_ROM_FORCE_COUNT=$(BIOS_BANK_ROM_FORCE_COUNT)

# Tool paths
# ----------
Expand Down
46 changes: 0 additions & 46 deletions bios/bank/2003/bank_get_map.s

This file was deleted.

10 changes: 5 additions & 5 deletions bios/bank/2001/bank_get_map.s → bios/bank/bank_get_map.s
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
.intel_syntax noprefix

#include "common.inc"
#include "bank/bank_macros.inc"

/**
* INT 18h AH=01h - bank_get_map
Expand All @@ -37,10 +38,9 @@
bank_get_map:
xor ax, ax
mov dx, bx
xor dh, dh
add dx, 0x00C1
cmp dx, 0x00C3
ja 1f
in al, dx
bank_map_dx
bank_map_read_ax_dx
bank_adjust_wraparound_map bl, ax
1:
ret

64 changes: 45 additions & 19 deletions bios/bank/2003/bank_set_map.s → bios/bank/bank_macros.inc
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,56 @@
* SOFTWARE.
*/

.arch i186
.code16
.intel_syntax noprefix
// Wrap around ROM indexes to support larger cartridges
// (bank_set_map variant)
.macro bank_adjust_wraparound_map reg_bl, reg_cx
#if BIOS_BANK_ROM_FORCE_COUNT > 0
test \reg_bl, \reg_bl
jz 1f // SRAM - skip

#include "common.inc"
or \reg_cx, (0xFFFF - BIOS_BANK_ROM_FORCE_COUNT)
1:
#endif
.endm

/**
* INT 18h AH=00h - bank_set_map
* Input:
* - BX = Bank
* - CX = Bank number
* Output:
*/
.global bank_set_map
bank_set_map:
push ax
mov ax, cx
mov dx, bx
// Wrap around ROM indexes to support larger cartridges
// (bank_read variant)
.macro bank_adjust_wraparound_rw reg_bh, reg_bx
#if BIOS_BANK_ROM_FORCE_COUNT > 0
test \reg_bh, 0x80
jz 1f // SRAM - skip

or \reg_bx, (0xFFFF - BIOS_BANK_ROM_FORCE_COUNT)
1:
#endif
.endm

// bank_get_map/bank_set_map helpers
#ifdef BIOS_BANK_MAPPER_2003
.macro bank_map_dx
xor dh, dh
add dx, dx
add dx, 0x00D0
cmp dx, 0x00D6
ja 1f
.endm
.macro bank_map_read_ax_dx
in ax, dx
.endm
.macro bank_map_write_ax_dx
out dx, ax
1:
pop ax
ret
.endm
#else
.macro bank_map_dx
xor dh, dh
add dx, 0x00C1
cmp dx, 0x00C3
ja 1f
.endm
.macro bank_map_read_ax_dx
in al, dx
.endm
.macro bank_map_write_ax_dx
out dx, al
.endm
#endif
9 changes: 4 additions & 5 deletions bios/bank/2001/bank_set_map.s → bios/bank/bank_set_map.s
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
.intel_syntax noprefix

#include "common.inc"
#include "bank/bank_macros.inc"

/**
* INT 18h AH=00h - bank_set_map
Expand All @@ -37,12 +38,10 @@
bank_set_map:
push ax
mov ax, cx
bank_adjust_wraparound_map bl, ax
mov dx, bx
xor dh, dh
add dx, 0x00C1
cmp dx, 0x00C3
ja 1f
out dx, al
bank_map_dx
bank_map_write_ax_dx
1:
pop ax
ret
29 changes: 28 additions & 1 deletion bios/header.s
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,34 @@ footer:
.byte 1 // Color
.byte 0 // Game ID
.byte 0x80 // Game version
.byte 2 // ROM size

// ROM size (derived from BIOS_BANK_ROM_FORCE_COUNT)
#if BIOS_BANK_ROM_FORCE_COUNT == 1024
.byte 11
#elif BIOS_BANK_ROM_FORCE_COUNT == 512
.byte 10
#elif BIOS_BANK_ROM_FORCE_COUNT == 256
.byte 9
#elif BIOS_BANK_ROM_FORCE_COUNT == 128
.byte 8
#elif BIOS_BANK_ROM_FORCE_COUNT == 64
.byte 6
#elif BIOS_BANK_ROM_FORCE_COUNT == 32
.byte 4
#elif BIOS_BANK_ROM_FORCE_COUNT == 16
.byte 3
#elif BIOS_BANK_ROM_FORCE_COUNT == 8
.byte 2
#elif BIOS_BANK_ROM_FORCE_COUNT == 4
.byte 1
#elif BIOS_BANK_ROM_FORCE_COUNT == 1
.byte 0
#elif BIOS_BANK_ROM_FORCE_COUNT == 0
.byte 2 // Default - 512 KB
#else
# error Unsupported ROM bank count!
#endif

.byte 4 // RAM size
.byte 4 // Flags
.byte 1 // Mapper
Expand Down
1 change: 1 addition & 0 deletions config.defaults.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Do not change this file! Change config.mk instead.

BIOS_BANK_MAPPER := 2001
BIOS_BANK_ROM_FORCE_COUNT := 0
BIOS_TIMER_RTC := none

NAME := Athena
Expand Down
5 changes: 5 additions & 0 deletions config.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

# BIOS_BANK_MAPPER := 2001

# Force ROM banking to emulate a specified number of banks.
# For WonderWitch compatibility on >512KB cartridges, set this value to 8.

BIOS_BANK_ROM_FORCE_COUNT := 8

# Select the RTC provided by the cartridge.
#
# Available options:
Expand Down

0 comments on commit 58547c6

Please sign in to comment.