What instead of indexed addressing modes?

Discussion of programming and development for the original Game Boy and Game Boy Color.
tepples
Posts: 22055
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: What instead of indexed addressing modes?

Post by tepples » Mon Mar 19, 2018 8:30 pm

That gets complicated when each actor ends up with dozens of different Uses flags, one for each possible line that could be in or not in a particular actor's script, that need to get copied from the actor's prototype. It gets doubly complicated when a line in the Grand Unified Script needs to turn a bunch of Uses flags on and off when an actor goes in or out of a particular state.

Today I decided to look at how commercial games solve the SOA vs. AOS dilemma. But I discovered that very few Game Boy games listed on Data Crystal actually have their RAM map substantially filled in. The first one I found was that of Wario Land, which has an 8-entry actor table at $A200, where the actors occupy $A200-$A213, $A220-$A233, $A240-$A253, ..., $A2E0-$A2F3. I guess this validates the array of 32-byte-aligned actor structures.

Oziphantom
Posts: 916
Joined: Tue Feb 07, 2017 2:03 am

Re: What instead of indexed addressing modes?

Post by Oziphantom » Mon Mar 19, 2018 9:19 pm

I've recently started tumbling down the Z80 rabbit hole.. Only I have a full Z80 at my disposal and 6502 that I can jump back to when the going gets too hot for the Z80 ;)

I'm starting to think that the dispatch is the way to solve the issue.
We are use to doing

Get thing,x
and state
bne _next
lda otherData,x
adc moreData,x
sta otherData,x
and state2
bne _next2
...thing2

while for Z80 it might be better to use the "use bits" as a dispatch.
ld use
<<2
add Base Pointer
call

this way you put a function that knows what the use cases are and can handle the bits without having to look them up. Saving the need to index into multiple tables.

Not done too much experimentation yet, as I'm still trying to find an assembler that isn't circa 1982. Or one that is 1987 spec but isn't trapped on a machine that is slow...

tepples
Posts: 22055
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: What instead of indexed addressing modes?

Post by tepples » Fri May 11, 2018 11:52 am

My bad luck continues. I searched Google for 8080 record field access and 8080 struct field access, hoping to stumble on some idiom that has become common practice, but most results were some website hosted on port 8080, not Intel 8080. I tried 8080 assembly struct field access, and Google tried to second guess me with "Missing: 8080"

adam_smasher
Posts: 271
Joined: Sun Mar 27, 2011 10:49 am
Location: Seattle

Re: What instead of indexed addressing modes?

Post by adam_smasher » Fri May 11, 2018 12:43 pm

If you put a search term in quotes Google won't show you results without it.

You might try looking at the output of 8080 or GBZ80 C compilers when interacting with structs to see how they handle the problem.

tepples
Posts: 22055
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: What instead of indexed addressing modes?

Post by tepples » Fri May 11, 2018 5:46 pm

adam_smasher wrote:You might try looking at the output of 8080 or GBZ80 C compilers
In other words, the godbolt solution. I'd've tried that if SDCC were any good. See "To C or not to C?" by ISSOtm.
ISSOtm wrote:[GBDK is] built on an ancient build of SDCC, which is known to generate poor (bloated) and often straight up wrong code.
What C compilers targeting 8080 are any good? Any luck with, say, BDS C?

adam_smasher
Posts: 271
Joined: Sun Mar 27, 2011 10:49 am
Location: Seattle

Re: What instead of indexed addressing modes?

Post by adam_smasher » Fri May 11, 2018 7:14 pm

It might be worth giving SDCC a shot anyway - it's still under active development, and the aforementioned "ancient build of SDCC, which is known to generate poor (bloated) and often straight up wrong code" that GBDK is based on is 17 years old.

tepples
Posts: 22055
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: What instead of indexed addressing modes?

Post by tepples » Thu May 24, 2018 8:55 pm

In GBDev Discord, ISSOtm announced an RGBDS macro pack to define structs and is trying to figure out how to best distribute it. Alongside this came some practical idioms for struct field access.

Code: Select all

  ; Prep: 3 mcycles each
  ld de,self
  ld bc,other_actor

  ; Random field load/store: 7 mcycles, BC preserved
  ld hl,offsetof(Actor, xsub)  ; 3
  add hl,de                    ; 2
  ld a,[hl]                    ; 2
  ; Compare 6502: 5 cycles (minus 1 for load not crossing page)
  lda actor_xsub,x             ; 4

  ; Random field arithmetic: 8 mcycles, BC preserved
  ld hl,offsetof(Actor, xsub)  ; 3
  add hl,de                    ; 2
  ld l,[hl]                    ; 2
  add a,l                      ; 1
  ; Compare 6502: 4 cycles (plus 1 for crossing page)
  add actor_xsub,x             ; 4

  ; Store constant in field: 8 mcycles, ABC preserved
  ld hl,offsetof(Actor, frame)  ; 3
  add hl,de                     ; 2
  ld [hl],FRAME_JUMP            ; 3
  ; Compare 6502: 7 cycles and A is clobbered
  lda #FRAME_JUMP               ; 2
  sta actor_xsub,x              ; 5
Roughly: 8080 is faster for sequential access, and 6502 is faster for random access. Try counting aaaa,X vs. (dd),Y accesses in your NES program to estimate what parts might become slower or faster respectively.

tepples
Posts: 22055
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: What instead of indexed addressing modes?

Post by tepples » Tue Jan 22, 2019 10:11 am

I've since collected a lot of this into a wiki article.

Post Reply