Forums

Sega Master System / Mark III / Game Gear
SG-1000 / SC-3000 / SF-7000 / OMV
Home - Forums - Games - Scans - Maps - Cheats - Credits
Music - Videos - Development - Hacks - Translations - Homebrew

View topic - Problem with rewriting indexed code 6502 to Z80 (Indirect zero-page post-indexed Y)

Reply to topic
Author Message
  • Joined: 23 Sep 2021
  • Posts: 96
  • Location: Poland
Reply with quote
Problem with rewriting indexed code 6502 to Z80 (Indirect zero-page post-indexed Y)
Post Posted: Sat Oct 23, 2021 11:25 am
I am having trouble rewriting functions in code 6502 to Z80. The function uses indexing, which has no equivalent in the Z80, and Indirect zero-page post-indexed Y. I would like to keep exactly the same functionality but for the Z80 code.
First, the accumulator will have an 8-bit value (0-FF), which is transferred to the Y register, then it will use it to read the VALUE from the address indicated in addrLO increased by Y. Next step VALUE is transferred back to the Y register to read what value of CONSTANT is in the appropriate place of the table increased by the Y index value. The result will be in accumulator A at the end.

The address set in the addrLO variable (and addrHI because 6502 does not have 16-bit registers, the given addressing mode reads only the addrLO variable because it automatically reads the next byte as HI)) in 6502 will be different and does not matter (it can be, for example, $ C000 for clarity) because it is only about reading the value from the indexed table.



;-------------------------------------------------

   LDA #$20                  ; loads to A $20 value (for example, because any 0-FF value can be calculated before)
   TAY                     ; transfers A to the Y index register
                        ; (this type of addressing can only be read with the register Y not X in 6502)
   LDA [addrLO],Y                  ; reads the address in the ram incremented by Y
   TAY                     ; the value read goes back to the Y register to be used as the next index
   LDA TileBits,Y                  ; now reads what Tile TYPE is at this address (the value of the constant Air (00) Solid (01) will be read
(...)
; now I have the value 00 or 01 in the battery because only such types of tiles are set as constant




; a table containing the types of tiles listed in the pattern table (0-255) :



;-------------------------------------------------

TileBits:                     ; tile table 256 bytes with constants

   .db AirTile      ;00            ; air tile
   .db SolidTile      ;01            ; solid tile
   .db SolidTile      ;02            ; other tiles 02-FF...
   .db SolidTile      ;03
   .db SolidTile      ;04
   .db SolidTile      ;05
   .db SolidTile      ;06
   .db SolidTile      ;07
   .db SolidTile      ;08
   .db SolidTile      ;09
   .db SolidTile      ;0A
   .db SolidTile      ;0B
   .db SolidTile      ;0C
   .db SolidTile      ;0D
   .db SolidTile      ;0E
   .db SolidTile      ;0F

   .db AirTile      ;10
   .db AirTile      ;11
   .db AirTile      ;12
   .db AirTile      ;13
   .db SolidTile      ;14
   .db SolidTile      ;15
   .db SolidTile      ;16
   .db SolidTile      ;17
   .db SolidTile      ;18
   .db SolidTile      ;19
   .db SolidTile      ;1A
   .db SolidTile      ;1B
   .db SolidTile      ;1C
   .db SolidTile      ;1D
   .db SolidTile      ;1E
   .db SolidTile      ;1F

   .db AirTile      ;20
   .db AirTile      ;21
   .db AirTile      ;22
   .db AirTile      ;23
   .db SolidTile      ;24
   .db SolidTile      ;25
   .db SolidTile      ;26
   .db SolidTile      ;27
   .db SolidTile      ;28
   .db SolidTile      ;29
   .db SolidTile      ;2A
   .db SolidTile      ;2B
   .db SolidTile      ;2C
   .db SolidTile      ;2D
   .db SolidTile      ;2E
   .db SolidTile      ;2F

(...) etc etc (30,40,50,60,70,80,90,A0,B0,C0,D0....)

   .db SolidTile      ;E0
   .db SolidTile      ;E1
   .db SolidTile      ;E2
   .db SolidTile      ;E3
   .db SolidTile      ;E4
   .db SolidTile      ;E5
   .db SolidTile      ;E6
   .db SolidTile      ;E7
   .db SolidTile      ;E8
   .db SolidTile      ;E9
   .db SolidTile      ;EA
   .db SolidTile      ;EB
   .db SolidTile      ;EC
   .db SolidTile      ;ED
   .db SolidTile      ;EE
   .db SolidTile      ;EF

   .db SolidTile      ;F0
   .db SolidTile      ;F1
   .db SolidTile      ;F2
   .db SolidTile      ;F3
   .db SolidTile      ;F4
   .db SolidTile      ;F5
   .db SolidTile      ;F6
   .db SolidTile      ;F7
   .db SolidTile      ;F8
   .db SolidTile      ;F9
   .db SolidTile      ;FA
   .db SolidTile      ;FB
   .db SolidTile      ;FC
   .db SolidTile      ;FD
   .db SolidTile      ;FE
   .db SolidTile      ;FF            ; tile $FF (#255)

;-------------------------------------------------

AirTile      equ 0                  ; constants specifying the types of tiles, you can add more types (02.03 ...)
SolidTile   equ 1

  View user's profile Send private message Visit poster's website
  • Site Admin
  • Joined: 19 Oct 1999
  • Posts: 14690
  • Location: London
Reply with quote
Post Posted: Sat Oct 23, 2021 12:42 pm
If I understand correctly, you want to read one value from a pointer plus an offset, then read from another pointer offset by this value. On Z80 we might do:
ld a, $20 ; initial offset for example
ld e, a
ld d,0
ld hl,table1
add hl,de
ld e,(hl)
ld hl,table2
add hl,de
ld a,(hl)

You can optimise a little if your tables are aligned to multiples of 256 bytes.
  View user's profile Send private message Visit poster's website
  • Joined: 23 Sep 2021
  • Posts: 96
  • Location: Poland
Reply with quote
Post Posted: Sat Oct 23, 2021 1:56 pm
Possible not very accurately described everything:

For example, if the address register is C000 and $20 is in the Y register, then the indexed address will come out C020 and the byte from this address will be read, e.g. there will be $ 10 there. This value of $ 10 will be transferred to the Y register. Then the type of tiles will be read from the address TABLE + $ 10, i.e. #AirTile is there, so 00 will be loaded into the acummulator (and if there was SolidTile then 01 would be read into Acu)

Edit:

I also have another, not tested:


   LD A,$20
   LD D,0
   LD E,A         ; $20 in DE
   ADD HL,DE
   LD A,(HL)
   LD D,E
   LD HL,TileBits
   ADD HL,DE
   LD A,(HL)


The question is how to prepare the table with the constants set for the Z80 (WLA-DX)?
I don't know if the constants in the tables are specified without "#"?
  View user's profile Send private message Visit poster's website
  • Site Admin
  • Joined: 19 Oct 1999
  • Posts: 14690
  • Location: London
Reply with quote
Post Posted: Sat Oct 23, 2021 8:03 pm
WLA DX uses .define to create value definitions and .db to make data.
  View user's profile Send private message Visit poster's website
Reply to topic



Back to the top of this page

Back to SMS Power!