Skip to content

Commit

Permalink
Merge pull request #1370 from pau-tomas/rpn_r_ref_set
Browse files Browse the repository at this point in the history
Use R_REF_SET commands in RPN when generating code
  • Loading branch information
chrismaltby authored Apr 5, 2024
2 parents b68418b + 8af28d1 commit 06d96fc
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 159 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Updated Polish localisation. [@ReptiIe](https://github.com/ReptiIe)
- Updated to latest [GBVM](https://github.com/chrismaltby/gbvm)
- Updated code generation to reduce access to stack [@pau-tomas](https://github.com/pau-tomas)
- Update Variable Uses sidebar to include any uses within Scripts [@pau-tomas](https://github.com/pau-tomas)

### Fixed
Expand Down
8 changes: 0 additions & 8 deletions appData/src/gb/include/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,7 @@ void vm_join(SCRIPT_CTX * THIS, INT16 idx) OLDCALL BANKED;
void vm_terminate(SCRIPT_CTX * THIS, INT16 idx) OLDCALL BANKED;
void vm_idle(SCRIPT_CTX * THIS) OLDCALL BANKED;
void vm_get_tlocal(SCRIPT_CTX * THIS, INT16 idxA, INT16 idxB) OLDCALL BANKED;
void vm_get_uint8(SCRIPT_CTX * THIS, INT16 idxA, UINT8 * addr) OLDCALL BANKED;
void vm_get_int8(SCRIPT_CTX * THIS, INT16 idxA, INT8 * addr) OLDCALL BANKED;
void vm_get_int16(SCRIPT_CTX * THIS, INT16 idxA, INT16 * addr) OLDCALL BANKED;
void vm_get_far(DUMMY0_t dummy0, DUMMY1_t dummy1, SCRIPT_CTX * THIS, INT16 idxA, UBYTE size, UBYTE bank, UBYTE * addr) OLDCALL NONBANKED;
void vm_set_uint8(SCRIPT_CTX * THIS, UINT8 * addr, INT16 idxA) OLDCALL BANKED;
void vm_set_int8(SCRIPT_CTX * THIS, INT8 * addr, INT16 idxA) OLDCALL BANKED;
void vm_set_int16(SCRIPT_CTX * THIS, INT16 * addr, INT16 idxA) OLDCALL BANKED;
void vm_set_const_int8(SCRIPT_CTX * THIS, UINT8 * addr, UINT8 v) OLDCALL BANKED;
void vm_set_const_int16(SCRIPT_CTX * THIS, INT16 * addr, INT16 v) OLDCALL BANKED;
void vm_init_rng(SCRIPT_CTX * THIS, INT16 idx) OLDCALL BANKED;
void vm_rand(SCRIPT_CTX * THIS, INT16 idx, UINT16 min, UINT16 limit, UINT16 mask) OLDCALL BANKED;
void vm_lock(SCRIPT_CTX * THIS) OLDCALL BANKED;
Expand Down
97 changes: 69 additions & 28 deletions appData/src/gb/include/vm.i
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ OP_VM_RPN = 0x15
.B_OR = '|'
.B_XOR = '^'
.B_NOT = '~'
.SHL = 'L'
.SHR = 'R'
.ABS = '@'
.MIN = 'm'
.MAX = 'M'
Expand All @@ -263,7 +265,8 @@ OP_VM_RPN = 0x15
.db OP_VM_RPN
.endm
.macro .R_INT8 ARG0
.db -1, #<ARG0
.db -1
.db #<ARG0
.endm
.macro .R_INT16 ARG0
.db -2
Expand All @@ -281,6 +284,23 @@ OP_VM_RPN = 0x15
.db -5
.dw #ARG0
.endm
.macro .R_REF_SET_IND ARG0
.db -6
.dw #ARG0
.endm
.MEM_I8 = 'i'
.MEM_U8 = 'u'
.MEM_I16 = 'I'
.macro .R_REF_MEM TYP, ADDR
.db -7
.db #<TYP
.dw #ADDR
.endm
.macro .R_REF_MEM_SET TYP, ADDR
.db -8
.db #<TYP
.dw #ADDR
.endm
.macro .R_OPERATOR ARG0
.db ARG0
.endm
Expand Down Expand Up @@ -333,75 +353,94 @@ OP_VM_IF_CONST = 0x1A
.db OP_VM_IF_CONST, #<N, #>LABEL, #<LABEL, #>B, #<B, #>IDXA, #<IDXA, #<CONDITION
.endm

OP_VM_GET_UINT8 = 0x1B
;-- Gets unsigned int8 from WRAM
; @param IDXA Target variable
; @param ADDR Address of the unsigned 8-bit value in WRAM
.macro VM_GET_UINT8 IDXA, ADDR
.db OP_VM_GET_UINT8, #>ADDR, #<ADDR, #>IDXA, #<IDXA
VM_RPN
.R_REF_MEM .MEM_U8, ^!ADDR!
.R_REF_SET ^!IDXA!
.R_STOP
.endm

OP_VM_GET_INT8 = 0x1C
;-- Gets signed int8 from WRAM
; @param IDXA Target variable
; @param ADDR Address of the signed 8-bit value in WRAM
.macro VM_GET_INT8 IDXA, ADDR
.db OP_VM_GET_INT8, #>ADDR, #<ADDR, #>IDXA, #<IDXA
VM_RPN
.R_REF_MEM .MEM_I8, ^!ADDR!
.R_REF_SET ^!IDXA!
.R_STOP
.endm

OP_VM_GET_INT16 = 0x1D
;-- Gets signed int16 from WRAM
; @param IDXA Target variable
; @param ADDR Address of the signed 16-bit value in WRAM
.macro VM_GET_INT16 IDXA, ADDR
.db OP_VM_GET_INT16, #>ADDR, #<ADDR, #>IDXA, #<IDXA
VM_RPN
.R_REF_MEM .MEM_I16, ^!ADDR!
.R_REF_SET ^!IDXA!
.R_STOP
.endm

OP_VM_SET_UINT8 = 0x1E
;-- Sets unsigned int8 in WRAM from variable
; @param ADDR Address of the unsigned 8-bit value in WRAM
; @param IDXA Source variable
.macro VM_SET_UINT8 ADDR, IDXA
.db OP_VM_SET_UINT8, #>IDXA, #<IDXA, #>ADDR, #<ADDR
VM_RPN
.R_REF ^!IDXA!
.R_REF_MEM_SET .MEM_U8, ^!ADDR!
.R_STOP
.endm

OP_VM_SET_INT8 = 0x1F
;-- Sets signed int8 in WRAM from variable
; @param ADDR Address of the signed 8-bit value in WRAM
; @param IDXA Source variable
.macro VM_SET_INT8 ADDR, IDXA
.db OP_VM_SET_INT8, #>IDXA, #<IDXA, #>ADDR, #<ADDR
VM_RPN
.R_REF ^!IDXA!
.R_REF_MEM_SET .MEM_I8, ^!ADDR!
.R_STOP
.endm

OP_VM_SET_INT16 = 0x20
;-- Sets signed int16 in WRAM from variable
; @param ADDR Address of the signed 16-bit value in WRAM
; @param IDXA Source variable
.macro VM_SET_INT16 ADDR, IDXA
.db OP_VM_SET_INT16, #>IDXA, #<IDXA, #>ADDR, #<ADDR
VM_RPN
.R_REF ^!IDXA!
.R_REF_MEM_SET .MEM_I16, ^!ADDR!
.R_STOP
.endm

OP_VM_SET_CONST_INT8 = 0x21
;-- Sets signed int8 in WRAM to the immediate value
; @param ADDR Address of the signed 8-bit value in WRAM
; @param V Immediate value
.macro VM_SET_CONST_INT8 ADDR, V
.db OP_VM_SET_CONST_INT8, #<V, #>ADDR, #<ADDR
; @param VAL Immediate value
.macro VM_SET_CONST_INT8 ADDR, VAL
VM_RPN
.R_INT8 ^!VAL!
.R_REF_MEM_SET .MEM_I8, ^!ADDR!
.R_STOP
.endm

;-- Sets unsigned int8 in WRAM to the immediate value
; @param ADDR Address of the unsigned 8-bit value in WRAM
; @param V Immediate value
.macro VM_SET_CONST_UINT8 ADDR, V
.db OP_VM_SET_CONST_INT8, #<V, #>ADDR, #<ADDR
; @param VAL Immediate value
.macro VM_SET_CONST_UINT8 ADDR, VAL
VM_RPN
.R_INT8 ^!VAL!
.R_REF_MEM_SET .MEM_U8, ^!ADDR!
.R_STOP
.endm

OP_VM_SET_CONST_INT16 = 0x22
;-- Sets signed int16 in WRAM to the immediate value
; @param ADDR Address of the signed 16-bit value in WRAM
; @param V Immediate value
.macro VM_SET_CONST_INT16 ADDR, V
.db OP_VM_SET_CONST_INT16, #>V, #<V, #>ADDR, #<ADDR
; @param VAL Immediate value
.macro VM_SET_CONST_INT16 ADDR, VAL
VM_RPN
.R_INT16 ^!VAL!
.R_REF_MEM_SET .MEM_I16, ^!ADDR!
.R_STOP
.endm

OP_VM_INIT_RNG = 0x23
Expand All @@ -413,10 +452,9 @@ OP_VM_INIT_RNG = 0x23

;-- Initializes RNG seed
.macro VM_RANDOMIZE
VM_RESERVE 2
VM_GET_UINT8 .ARG0, _DIV_REG
VM_GET_UINT8 .ARG1, _game_time
VM_RPN
.R_REF_MEM .MEM_U8, _DIV_REG
.R_REF_MEM .MEM_U8, _game_time
.R_INT16 256
.R_OPERATOR .MUL
.R_OPERATOR .ADD
Expand Down Expand Up @@ -830,10 +868,13 @@ OP_VM_ACTOR_SET_ANIM_SET = 0x84
; * `\002\x` Sets the text font
; * `\003\x\y` Sets the position for the next character
; * `\004\x\y` Sets the position for the next character relative to the last character
; * `\005\` TBD
; * `\005` Escape the next character
; * `\006\mask` Wait for input to continue to the next character.
; * `\007\n` Inverts the colors of the following characters.
; * `\010\x` Switch left to right / right to left printing
; * `\011` Zero width symbol
; * `\n` Next line
; * `\013\x` Set UI palette number to x
; * `\r` Scroll text one line up
OP_VM_LOAD_TEXT = 0x40
.macro VM_LOAD_TEXT NARGS
Expand Down
18 changes: 11 additions & 7 deletions appData/src/gb/src/core/actor.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ actor_t * actors_active_head;
actor_t * actors_active_tail;
actor_t * actors_inactive_head;

UINT8 screen_x, screen_y;
UBYTE screen_x, screen_y;
actor_t * invalid;
UBYTE player_moving;
UBYTE player_iframes;
Expand Down Expand Up @@ -110,12 +110,14 @@ void actors_update(void) NONBANKED {
);
}

static bool window_hide_actors;
window_hide_actors = (NO_OVERLAY_PRIORITY && (!show_actors_on_overlay) && (WX_REG > DEVICE_WINDOW_PX_OFFSET_X));

actor = actors_active_tail;
while (actor) {
if (actor->pinned) {
screen_x = (actor->pos.x >> 4) + 8, screen_y = (actor->pos.y >> 4) + 8;
} else {
screen_x = (actor->pos.x >> 4) - draw_scroll_x + 8, screen_y = (actor->pos.y >> 4) - draw_scroll_y + 8;
// Bottom right coordinate of actor in 16px tile coordinates
// Subtract bounding box estimate width/height
// and offset by 64 to allow signed comparisons with screen tiles
Expand All @@ -126,11 +128,11 @@ void actors_update(void) NONBANKED {
// Actor right edge < screen left edge
(actor_tile16_x < screen_tile16_x) ||
// Actor left edge > screen right edge
(actor_tile16_x - ACTOR_BOUNDS_TILE16 - SCREEN_TILE16_W > screen_tile16_x) ||
((actor_tile16_x - (ACTOR_BOUNDS_TILE16 + SCREEN_TILE16_W)) > screen_tile16_x) ||
// Actor bottom edge < screen top edge
(actor_tile16_y < screen_tile16_y) ||
// Actor top edge > screen bottom edge
(actor_tile16_y - ACTOR_BOUNDS_TILE16 - SCREEN_TILE16_H > screen_tile16_y)
((actor_tile16_y - (ACTOR_BOUNDS_TILE16 + SCREEN_TILE16_H)) > screen_tile16_y)
) {
if (actor->persistent) {
actor = actor->prev;
Expand All @@ -142,12 +144,14 @@ void actors_update(void) NONBANKED {
actor = prev;
continue;
}
// calculate screen coordinates
screen_x = ((actor->pos.x >> 4) + 8) - draw_scroll_x, screen_y = ((actor->pos.y >> 4) + 8) - draw_scroll_y;
}
if (NO_OVERLAY_PRIORITY && (!show_actors_on_overlay) && (WX_REG != MINWNDPOSX) && (WX_REG < (UINT8)screen_x + 8) && (WY_REG < (UINT8)(screen_y) - 8)) {
// Hide if under window (don't deactivate)
if (actor->hidden) {
actor = actor->prev;
continue;
} else if (actor->hidden) {
} else if ((window_hide_actors) && (((screen_x + 8) > WX_REG) && ((screen_y - 8) > WY_REG))) {
// Hide if under window (don't deactivate)
actor = actor->prev;
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion appData/src/gb/src/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void core_run(void) BANKED {

LCDC_REG = LCDCF_OFF | LCDCF_WIN9C00 | LCDCF_WINON | LCDCF_BG8800 | LCDCF_BG9800 | LCDCF_OBJ16 | LCDCF_OBJON | LCDCF_BGON;

WX_REG = MINWNDPOSX;
WX_REG = DEVICE_WINDOW_PX_OFFSET_X;
WY_REG = MENU_CLOSED_Y;

initrand(DIV_REG);
Expand Down
6 changes: 3 additions & 3 deletions appData/src/gb/src/core/interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ void simple_LCD_isr(void) NONBANKED {
if (WY_REG) {
if (WY_REG < MENU_CLOSED_Y) LYC_REG = WY_REG - 1;
} else {
if ((WX_REG == MINWNDPOSX) && (show_actors_on_overlay == FALSE)) HIDE_SPRITES;
if ((WX_REG == DEVICE_WINDOW_PX_OFFSET_X) && (show_actors_on_overlay == FALSE)) HIDE_SPRITES;
LYC_REG = overlay_cut_scanline;
}
} else {
if (LYC_REG < overlay_cut_scanline) {
if ((WX_REG == MINWNDPOSX) && (show_actors_on_overlay == FALSE)) {
if ((WX_REG == DEVICE_WINDOW_PX_OFFSET_X) && (show_actors_on_overlay == FALSE)) {
while (STAT_REG & STATF_BUSY) ;
HIDE_SPRITES;
}
Expand Down Expand Up @@ -64,7 +64,7 @@ void fullscreen_LCD_isr(void) NONBANKED {
}

void VBL_isr(void) NONBANKED {
if ((WY_REG = win_pos_y) < MENU_CLOSED_Y) WX_REG = (win_pos_x + MINWNDPOSX), SHOW_WIN; else WX_REG = 0, HIDE_WIN;
if ((WY_REG = win_pos_y) < MENU_CLOSED_Y) WX_REG = (win_pos_x + DEVICE_WINDOW_PX_OFFSET_X), SHOW_WIN; else WX_REG = 0, HIDE_WIN;
if (hide_sprites) HIDE_SPRITES; else SHOW_SPRITES;
scroll_shadow_update();
}
10 changes: 5 additions & 5 deletions appData/src/gb/src/core/projectiles.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ void projectiles_update(void) NONBANKED {
}
}

UINT8 screen_x = (projectile->pos.x >> 4) - draw_scroll_x + 8,
UBYTE screen_x = (projectile->pos.x >> 4) - draw_scroll_x + 8,
screen_y = (projectile->pos.y >> 4) - draw_scroll_y + 8;

if (screen_x > 160 || screen_y > 144) {
if ((screen_x > DEVICE_SCREEN_PX_WIDTH) || (screen_y > DEVICE_SCREEN_PX_HEIGHT)) {
// Remove projectile
projectile_t *next = projectile->next;
LL_REMOVE_ITEM(projectiles_active_head, projectile, prev_projectile);
Expand Down Expand Up @@ -119,10 +119,10 @@ void projectiles_render(void) NONBANKED {
_save_bank = _current_bank;

while (projectile) {
UINT8 screen_x = (projectile->pos.x >> 4) - draw_scroll_x + 8,
screen_y = (projectile->pos.y >> 4) - draw_scroll_y + 8;
UINT8 screen_x = ((projectile->pos.x >> 4) + 8) - draw_scroll_x,
screen_y = ((projectile->pos.y >> 4) + 8) - draw_scroll_y;

if (screen_x > 160 || screen_y > 144) {
if ((screen_x > DEVICE_SCREEN_PX_WIDTH) || (screen_y > DEVICE_SCREEN_PX_HEIGHT)) {
// Remove projectile
projectile_t *next = projectile->next;
LL_REMOVE_ITEM(projectiles_active_head, projectile, prev_projectile);
Expand Down
Loading

0 comments on commit 06d96fc

Please sign in to comment.