Skip to content

Commit

Permalink
Issue #2544: First attempt at far static conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
suborb committed May 23, 2024
1 parent 0e0421c commit 54d36d2
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 33 deletions.
1 change: 1 addition & 0 deletions lib/z80_crt0.hdr
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@
EXTERN lp_pptr

EXTERN l_farcall ; Long call
EXTERN l_far_mapaddr ; Map __banked to __far

;--------------------------------------------------
; Routines for handling 16 bit fixed point for sccz80
Expand Down
6 changes: 5 additions & 1 deletion libsrc/target/test/far/__far_page.asm
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ __far_page:
rl d
rla
rl d
; e is now which bank we should look at
inc d
inc d
inc d
inc d
; d is now which bank we should look at
ld a,b
and @00111111 ;Take mod 16384
or @10000000 ;Map to 0x8000 page
Expand Down
22 changes: 22 additions & 0 deletions libsrc/target/test/far/l_far_mapaddr.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@




PUBLIC l_far_mapaddr

; Map between a __banked address to a far pointer
; Entry: ehl = banked address
; Exit: ehl = far pointer
l_far_mapaddr:
ld d,0
srl e
rr d
srl e
rr d
ld a,h
and @00111111
or d
ld h,a
ld d,0
ret
16 changes: 10 additions & 6 deletions libsrc/target/zx/far/__far_page.asm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ SECTION code_l_sccz80_far
PUBLIC __far_page

EXTERN __far_end
EXTERN __msx_bank_mappings

; Support up to 96k of heap, but...
; last 32k conflict with other things
pages:
defb 1,3,4,6,0,7
PUBLIC __zx_bank_mappings

; Support up to 128k of heap, but...
; last 64k conflict with other things
; Last 32k really conflict
;
; Mapping from bank -> far in l_far_mapaddr.asm
__zx_bank_mappings:
defb 1,3,4,6,0,7,2,5

; Entry: ebc = logical address
; a' = local memory page
Expand All @@ -34,7 +38,7 @@ __far_page:
; hl = offset within bank
push hl
push de
ld hl,pages
ld hl,__zx_bank_mappings
ld e,d
ld d,0
add hl,de
Expand Down
68 changes: 68 additions & 0 deletions libsrc/target/zx/far/l_far_mapaddr.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
;
; Map a banked address to a far pointer
;



SECTION code_l_sccz80_far
PUBLIC l_far_mapaddr


; ZX Bank mapping is a bit funky, so we have to use a table to
; do the mapping
;
; Entry: ehl = banked address
; Exit: ehl = far address
l_far_mapaddr:
push hl
ld a,e
add a
ld e,a
ld d,0
ld hl,__zx_bank_to_far
add hl,de
ld d,(hl) ;High 2 bits of address
inc hl
ld e,(hl) ;Segment number
pop hl
ld a,h
and @00111111
or d
ld h,a
ld d,0
ret


; Support up to 128k of heap, but...
; last 64k conflict with other things
; Last 32k really conflict
; defb 1,3,4,6,0,7,2,5
__zx_bank_to_far:
; Bank 0
defb %00000000 ;High word bit
defb 2 ;Far segment
; Bank 1
defb %00000000 ;High word bit
defb 1 ;Far segment
; Bank 2
defb %10000000 ;High word bit
defb 2 ;Far segment
; Bank 3
defb %01000000 ;High word bit
defb 1 ;Far segment
; Bank 3
defb %01000000 ;High word bit
defb 1 ;Far segment
; Bank 4
defb %10000000 ;High word bit
defb 1 ;Far segment
; Bank 5
defb %11000000 ;High word bit
defb 2 ;Far segment
; Bank 6
defb %11000000 ;High word bit
defb 1 ;Far segment
; Bank 7
defb %01000000 ;High word bit
defb 2 ;Far segment

1 change: 1 addition & 0 deletions libsrc/target/zx/zx.lst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target/zx/games/obj/${TARGET}/bit_close_ei

target/zx/far/__far_misc
target/zx/far/__far_page
target/zx/far/l_far_mapaddr
@games/games.lst
@target/zx/spectrum.lst
@target/zx/fzx/fzx.lst
Expand Down
2 changes: 1 addition & 1 deletion src/sccz80/ccdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ extern void gen_builtin_strcpy(void);
extern void gen_builtin_strchr(int32_t c);
extern void gen_builtin_memset(int32_t c, int32_t s);
extern void gen_builtin_memcpy(int32_t src, int32_t n);
extern void gen_address(SYMBOL *ptr);

extern void zconvert_to_long(unsigned char tounsigned, Kind from, unsigned char fromunsigned);
extern void zconvert_to_llong(unsigned char tounsigned, Kind from, unsigned char fromunsigned);
Expand All @@ -165,7 +166,6 @@ extern void zconvert_to_llong(unsigned char tounsigned, Kind from, unsigned char
/* const.c */
extern int constant(LVALUE *lval);

extern void address(SYMBOL *ptr);

extern int storeq(int length, unsigned char *queue,int32_t *val);
extern int qstr(double *val);
Expand Down
36 changes: 33 additions & 3 deletions src/sccz80/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,18 @@ static void switch_namespace(char *name)
*/
void gen_load_static(SYMBOL* sym)
{
if ( sym->ctype->flags & FARACC ) {
LVALUE lval={0};

// Load the address and convert it
gen_address(sym);

lval.ltype = sym->ctype;
lval.flags = FARACC;
lval.indirect_kind = sym->ctype->kind;
gen_load_indirect(&lval);
return;
}
switch_namespace(sym->ctype->namespace);
if (sym->ctype->kind == KIND_CHAR) {
if ( (sym->ctype->isunsigned) == 0 ) {
Expand Down Expand Up @@ -323,10 +335,10 @@ void gen_load_static(SYMBOL* sym)

#endif
} else if (sym->ctype->kind == KIND_DOUBLE && c_fp_size > 4 ) {
address(sym);
gen_address(sym);
dcallrts("dload", KIND_DOUBLE);
} else if (sym->ctype->kind == KIND_LONGLONG ) {
address(sym);
gen_address(sym);
callrts("l_i64_load");
} else if (sym->ctype->kind == KIND_LONG || (sym->ctype->kind == KIND_DOUBLE && c_fp_size == 4) || sym->ctype->kind == KIND_CPTR ) { // 4 byte doubles only
if ( IS_GBZ80() ) {
Expand Down Expand Up @@ -378,13 +390,31 @@ int getloc(SYMBOL* sym, int off)
return (offs);
}


void gen_address(SYMBOL* ptr)
{
if ( ptr->ctype->flags & FARACC ) {
outfmt("\tld\thl,+(%s%s %% 65536)\n", dopref(ptr) ? Z80ASM_PREFIX : "", ptr->name);
outfmt("\tld\tde,+(%s%s / 65536)\n", dopref(ptr) ? Z80ASM_PREFIX : "", ptr->name);
callrts("l_far_mapaddr");
} else {
immed();
outname(ptr->name, dopref(ptr));
nl();
if ( ptr->ctype->kind == KIND_CPTR ) {
const2(0);
}
}
}


/* Store the primary register into the specified */
/* static memory cell */
void gen_store_static(SYMBOL* sym)
{
switch_namespace(sym->ctype->namespace);
if (sym->ctype->kind == KIND_DOUBLE && c_fp_size > 4 ) {
address(sym);
gen_address(sym);
dcallrts("dstore", KIND_DOUBLE);
} else if (sym->ctype->kind == KIND_CHAR) {
ol("ld\ta,l");
Expand Down
15 changes: 2 additions & 13 deletions src/sccz80/const.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,17 +326,6 @@ int hex(char c)
return ((c1 >= '0' && c1 <= '9') || (c1 >= 'A' && c1 <= 'F'));
}

/* djm, seems to load up literal address? */

void address(SYMBOL* ptr)
{
immed();
outname(ptr->name, dopref(ptr));
nl();
if ( ptr->ctype->kind == KIND_CPTR ) {
const2(0);
}
}

int pstr(LVALUE *lval)
{
Expand Down Expand Up @@ -561,7 +550,7 @@ void offset_of(LVALUE *lval)
} else if ( ptr->ctype->kind == KIND_STRUCT ) {
tag = ptr->ctype->tag;
} else {
printf("%d\n",ptr->type);
// printf("%d\n",ptr->type);
}
}
}
Expand Down Expand Up @@ -667,7 +656,7 @@ void size_of(LVALUE* lval)
do {
if ( (mptr = get_member(type->kind == KIND_STRUCT ? type->tag : type->ptr->tag) ) != NULL ) {
type = mptr;
if ( (mptr->kind == KIND_PTR || mptr->kind == KIND_CPTR) && deref ) {
if ( ( mptr->kind == KIND_PTR || mptr->kind == KIND_CPTR) && deref ) {
// Do nothing
} else {
// tag_sym->size = numner of elements
Expand Down
4 changes: 4 additions & 0 deletions src/sccz80/declparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1259,11 +1259,13 @@ Type *dodeclare2(Type **base_type, decl_mode mode)
char namebuf[NAMESIZE];
Type *type;
int flags = 0;
int isfar = 0;

if ( base_type != NULL && *base_type != NULL ) {
type = CALLOC(1,sizeof(*type));
*type = **base_type;
} else {
if ( amatch("__far") ) isfar = 1;
if ( (type = parse_type()) == NULL ) {
return NULL;
}
Expand Down Expand Up @@ -1318,6 +1320,8 @@ Type *dodeclare2(Type **base_type, decl_mode mode)

if ( type->kind == KIND_FUNC ) {
type->flags |= flags;
} else if ( isfar ) {
type->flags |= FARACC;
}

// Validate that structs are not weak if we have an instance
Expand Down
6 changes: 4 additions & 2 deletions src/sccz80/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ int heira(LVALUE *lval)
if (lval->indirect_kind)
return 0;
/* global & non-array */
address(lval->symbol);
gen_address(lval->symbol);
lval->indirect_kind = lval->symbol->ctype->kind;
return 0;
}
Expand Down Expand Up @@ -850,11 +850,13 @@ int heirb(LVALUE* lval)
return k;
}
if (ptr && ptr->ctype->kind == KIND_FUNC) {
address(ptr);
gen_address(ptr);
lval->symbol = NULL; // TODO: Can we actually set it correctly here? - Needed for verification of func ptr arguments
lval->ltype = ptr->ctype;
lval->flags = ptr->flags;
return 0;
} else if ( ptr && ptr->ctype->flags & FARACC ) {
lval->flags = FARACC;
}
return k;
}
2 changes: 1 addition & 1 deletion src/sccz80/primary.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ int primary(LVALUE* lval)
return (1);
}
/* Handle arrays... */
address(ptr);
gen_address(ptr);
/* djm sommat here about pointer types? */
lval->indirect_kind = lval->ptr_type = ptr->type;
if ( ispointer(lval->ltype) || lval->ltype->kind == KIND_ARRAY )
Expand Down
11 changes: 5 additions & 6 deletions src/ticks/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,11 @@ static void msx_init(void)
for ( i = 0; i < 256; i++ ) {
msx_banks[i] = calloc(16384,1);
}
msx_mmu[0] = 0x00;
msx_mmu[1] = 0x01;
msx_mmu[2] = 0x02;
msx_mmu[3] = 0x03;


standard_init();
get_mem_addr = msx_get_memory_addr;
handle_out = msx_handle_out;
handle_in = msx_handle_in;
Expand All @@ -367,10 +369,7 @@ static uint8_t *msx_get_memory_addr(uint32_t pc, memtype type)
{
int segment = pc / 16384;

if ( msx_mmu[segment] != 0xff ) {
return &msx_banks[msx_mmu[segment]][pc % 16384];
}
return &mem[pc & 65535];
return &msx_banks[msx_mmu[segment]][pc % 16384];
}

static int msx_handle_in(int port)
Expand Down

0 comments on commit 54d36d2

Please sign in to comment.