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

(sccz80) Gameboy z80 support #1275

Closed
suborb opened this issue Sep 10, 2019 · 6 comments
Closed

(sccz80) Gameboy z80 support #1275

suborb opened this issue Sep 10, 2019 · 6 comments

Comments

@suborb
Copy link
Member

@suborb suborb commented Sep 10, 2019

Just leaving this here for thoughts. We've managed to support the 8080 without too much trouble, so lets turn to the gbz80:

Missing:

  • No index registers
  • No alternate registers
  • No block instructions
  • No in/out (memory mapped io instead)
  • No 0xed instruction page (so ld rr,(nnnn), ld (nnnn),rr, sbc hl,rr, adc hl,rr are absent)
  • No ld hl,(nnnn), ld (nnnn),hl
  • No ex (sp),hl, ex de,hl
  • No djnz
  • No sign or parity flags
  • POP AF doesn't preserve low 4 bits of F (see above)

Extra (useful) instructions:

  • ld hl,sp+dd
  • add sp,dd
  • ld a,(hl+)
  • ld (hl+),a
  • ld a,(hl-)
  • ld (hl-),a

Movements:

  • Opcodes moved: reti, ld a,(nnnn), ld (nnnn),a

So it's mostly 8080, the main issues at first glance are the missing ex instructions and ld (nnnn),hl. ld hl,(nnnn) is fairly easy to emulate if we can trash a.

Library support should be from gbdk, there's an up-to-date version here: https://github.com/andreasjhkarlsson/gbdk-n

@pauloscustodio pauloscustodio self-assigned this Sep 10, 2019
suborb added a commit that referenced this issue Sep 10, 2019
Function pointer calls will be horrendously broken..
@pauloscustodio pauloscustodio removed their assignment Sep 10, 2019
@pauloscustodio
Copy link
Member

@pauloscustodio pauloscustodio commented Sep 10, 2019

Shall we use -mgbz80 as the machine specifier option? (to be the same in z80asm and sccz80)

@suborb
Copy link
Member Author

@suborb suborb commented Sep 11, 2019

Thanks for picking up the z80asm side Paulo.

That’s the value that I’ve chosen - matches the one used by sdcc.

pauloscustodio added a commit that referenced this issue Sep 15, 2019
On a GameBoy, call automatically the emulation for ex(sp),hl.
Remove IF/ELSE in library code.
pauloscustodio added a commit that referenced this issue Sep 15, 2019
Emulate ldi, ldir, ldd, lddr on GameBoy
@suborb
Copy link
Member Author

@suborb suborb commented Sep 16, 2019

@pauloscustodio I think djnz has an off by one issue:

Source:

flags_again:
        push    hl              ;save fmt
        ld      b,5
        ld      hl,flags
flag_loop:
        cp      (hl)
        inc     hl
        jr      nz,no_flag
; We've found a flag
        ld      a,(hl)          ;pick up flags
        or      c
        ld      c,a
        pop     hl              ;get fmt back
        ld      a,(hl)          ;pick up next character
        inc     hl
        jr      flags_again
flags:
        defb    '-', 0x01
        defb    '+', 0x02
        defb    ' ', 0x08
        defb    '#', 0x10
        defb    '0', 0x04
no_flag:
        inc     hl
        djnz    flag_loop

Assembled to:

flags_again:
                    push    hl                              ;[0155] e5
                    ld      b,$05                           ;[0156] 06 05
                    ld      hl,$0167                        ;[0158] 21 67 01
flag_loop:
                    cp      (hl)                            ;[015b] be
                    inc     hl                              ;[015c] 23
                    jr      nz,no_flag                      ;[015d] 20 12
                    ld      a,(hl)                          ;[015f] 7e
                    or      c                               ;[0160] b1
                    ld      c,a                             ;[0161] 4f
                    pop     hl                              ;[0162] e1
                    ld      a,(hl)                          ;[0163] 7e
                    inc     hl                              ;[0164] 23
                    jr      flags_again                     ;[0165] 18 ee
flags:
                    dec     l                               ;[0167] 2d
                    ld      bc,$022b                        ;[0168] 01 2b 02
                    jr      nz,$0175                        ;[016b] 20 08
                    inc     hl                              ;[016d] 23
                    stop                                    ;[016e] 10
                    jr      nc,$0175                        ;[016f] 30 04
no_flag:
                    inc     hl                              ;[0171] 23
                    dec     b                               ;[0172] 05
                    jr      nz,$015c                        ;[0173] 20 e7
@suborb
Copy link
Member Author

@suborb suborb commented Sep 16, 2019

Status update:

The basics are working:

  • sccz80 generates the correct I/O code for 8 bit __sfr
  • sccz80 uses the new stack routines
  • +test has a -clib=gbz80 target with pretty much the same library as an 8080 port.
  • A fair amount of the test-suite is running
  • z80asm does emulation for ldir and friends

Remaining:

  • Half + carry flags for add hl,sp, ld hl,sp+nnn
  • A Gameboy crt0
  • Import the standard GBDK library
  • Example
  • Figure out sdcc interop will work (sdcc uses reversed de+hl)
  • sdcc crt0 routines
pauloscustodio added a commit that referenced this issue Sep 19, 2019
On a GameBoy, call automatically the emulation for ex(sp),hl.
Remove IF/ELSE in library code.
pauloscustodio added a commit that referenced this issue Sep 19, 2019
On a GameBoy, call automatically the emulation for ex(sp),hl.
Remove IF/ELSE in library code.
pauloscustodio added a commit that referenced this issue Sep 19, 2019
Emulate ldi, ldir, ldd, lddr on GameBoy
@suborb
Copy link
Member Author

@suborb suborb commented Sep 21, 2019

I've now got galaxy.c from gbdk compiling and mostly working.

I think sdcc interop goes away at this stage, getting a whole bunch of errors along the lines of:

Function canAssign not initialized in port structure
Function notUsed not initialized in port structure
Caught signal 11: SIGSEGV

Which will need to be investigated later.

  • The not-initialised messages are due to the z80 rules being applied in gbz80 mode.
  • Seg fault is probably due to #1279
@suborb
Copy link
Member Author

@suborb suborb commented Sep 28, 2019

Closing, will reopen as a separate issue with remaining items

@suborb suborb closed this Sep 28, 2019
pauloscustodio added a commit that referenced this issue Oct 29, 2019
On a GameBoy, call automatically the emulation for ex(sp),hl.
Remove IF/ELSE in library code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants