Tilemap code example

Do you live and breathe hexadecimal? Do you like speaking to hardware directly?

Moderator: Programming Moderators

Tau Ceti
Posts: 11
Joined: Fri Jul 07, 2017 3:00 pm

Tilemap code example

Post by Tau Ceti » Mon Jul 01, 2019 5:38 am

I've been struggling with the Tilemap documentation, loading tile definitions and 40x32 tile map into memory nothing seems to happen, despite the data being present in memory; along with the ports set with seemingly correct values.

I'd love to see a z80 sjasmplus based code example of say loading a Manic Miner level into a Zx Spectrum Next Tile Map.

I've managed basic sprite animation so far, but the tilemap seems unbeatable at the moment :(

Ped7g
Posts: 112
Joined: Mon Jul 16, 2018 7:11 pm

Re: Tilemap code example

Post by Ped7g » Mon Jul 01, 2019 7:00 pm

How about the skeleton of zxnext editor: https://github.com/next-dev/ed
It's pretty much *only* tilemap mode and keyboard handler currently (as Mike is seriously considering to write it rather in C), i.e. should be small enough to trace through it and see if you can find what is different with your approach.

And if you will figure it out, and feel like you mastered the tilemap, you may check my fork with work-in progress 80x42.6 mode (8x6px tiles) ... which reminds I need to reserve some spare time to finally move ahead on that one, and make it nice standalone piece of asm, component-like, so it will also work as tilemap asm example (although sort of "advanced" one).

(but of course you can also push your current code to github and put here link, so we can check yours source and eventually salvage it or gave you some pointers (pun intended) how to rewrite it)

EDIT2: OMG... I did it myself.... who's Mike? Matt should finally explain that... :D

Tau Ceti
Posts: 11
Joined: Fri Jul 07, 2017 3:00 pm

Re: Tilemap code example

Post by Tau Ceti » Tue Jul 02, 2019 1:55 pm

Thanks for the code Ped7G - I'll spend some time looking through and see if I'm making any obvious mistakes with my approach.

User avatar
SevenFFF
Posts: 222
Joined: Mon Jun 05, 2017 5:30 pm
Location: USA

Re: Tilemap code example

Post by SevenFFF » Tue Jul 02, 2019 5:15 pm

This is my tilemap setup code from Spectron 2084. It's not a runnable example, and not written for sjasmplus, but it's small and self-contained enough that you might be able to spot something different from your code:

Code: Select all

SetupTilemap            proc
                        ld sp, $0000
                        MMU2(10, false)                 ; Page in ULA/tilemap bank
                        nextreg $4C, 3                  ; (R/W) 0x4C (76) => Transparency index for the tilemap
                        nextreg $43, %0 011 0000        ; (R/W) 0x43 (67) => Palette Control
                        nextreg $40, 0                  ; (R/W) 0x40 (64) => Palette Index
                        ld b, 0                         ; Write 256 9-bit palette entries
                        ld hl, TilePalette              ; R/W) 0x44 (68) => Palette Value (9 bit colour)
PalLoop:                ld a, (hl)
                        nextreg $44, a
                        inc hl
                        ld a, (hl)
                        nextreg $44, a
                        inc hl
                        djnz PalLoop
                        nextreg $6F, $65                ; Copy tile definitions
                        ld hl, Tiles.Source             ; (R/W) 0x6F (111) => Tile Definitions Base Address
                        ld de, TilesAddr
                        ld bc, Tiles.Size
                        ldir
                        nextreg $6E, $5B                ; Copy map definitions
                        ld hl, PlayField.Source         ; (R/W) 0x6E (110) => Tilemap Base Address
                        ld de, ArenaAddr
                        ld bc, PlayField.Size
                        ldir
                        nextreg $15, %000 101 10        ; (R/W) 0x15 (21) => Sprite and Layers system
                        nextreg $1C, %xxxx 1111         ; (W) 0x1C (28) => Clip Window control
                        nextreg $1B, 0                  ; (R/W) 0x1B (27) => Clip Window Tilemap
                        nextreg $1B, 159                ; 0,159,0,255
                        nextreg $1B, 0
                        nextreg $1B, 255
                        nextreg $2F, 0                  ; (R/W) 0x2F (47) => Tilemap Offset X MSB
                        nextreg $30, 0                  ; (R/W) 0x30 (48) => Tilemap Offset X LSB)
                        nextreg $31, 0                  ; (R/W) 0x31 (49) => Tilemap Offset Y
                        FillLDIR($4000, $1800, $00)     ; Clear ULA pixels
                        FillLDIR($5800, $0300, $47)     ; Clear ULA attributes
                        nextreg $6B, %1000 xx 00        ; (R/W) 0x6B (107) => Tilemap Control

                        FarJump(6, 56, ScreenAdjust)

pend
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel Spectron 2084blog

Tau Ceti
Posts: 11
Joined: Fri Jul 07, 2017 3:00 pm

Re: Tilemap code example

Post by Tau Ceti » Fri Jul 05, 2019 4:59 pm

I managed to fill the screen!

The tilemap seems to be working after compiling a .nex file with DEVICE ZXSPECTRUMNEXT and using nextreg to set the tilemap ports with some of your values in your example code. I'd previously set the ports using e.g. ld bc, $4C : out (c), a

Thanks for the help - time to find out how to convert a manic miner level into ZX Spectrum Next tile map definitions!

Ped7g
Posts: 112
Joined: Mon Jul 16, 2018 7:11 pm

Re: Tilemap code example

Post by Ped7g » Fri Jul 05, 2019 6:36 pm

you can modify "next-regs" also with OUT instruction, but it works differently.

First you must output nextreg number $4C to I/O port $243B ("TBBlue Register Select" I/O port), then you can output value $xx to I/O port $253B ("TBBlue Register Access"), or you can read from the selected nextreg by IN instruction. I.e.

Code: Select all

    next    $4C, $04    ; tilemap transparency set to 4
  ; can be achieved by using Z80 OUT instruction like this
    ld      bc,$243B
    ld      a,$4C
    out     (c),a       ; select nextreg $4C (tilemap transparency)
    inc     b
    ld      a,4
    out     (c),a       ; write value 4 into selected nextreg
  ; the OUT+IN is actually only possible way to *READ* nextregs:
    ld      bc,$243B
    ld      a,$56
    out     (c),a       ; select nextreg $56 (MMU6)
    inc     b
    in      a,(c)       ; read currently mapped 8k page at $C000..$DFFF memory range
If you are setting the same nextregister to multiple values in successive way (palette values, etc), it may be faster to use the OUTs, for single-value-set the nextreg $xx,$yy is much more convenient and faster.

Tau Ceti
Posts: 11
Joined: Fri Jul 07, 2017 3:00 pm

Re: Tilemap code example

Post by Tau Ceti » Tue Jul 16, 2019 1:05 pm

tilemap.png
tilemap.png (73.45 KiB) Viewed 852 times
:D

Finally, control over the tiles and tilemap.

Now to find out how to get some decent colours - guess I'll have to look into info about palettes.

Anyone know if there are any guides on turning a photoshop file into data for tile definitions?

User avatar
SevenFFF
Posts: 222
Joined: Mon Jun 05, 2017 5:30 pm
Location: USA

Re: Tilemap code example

Post by SevenFFF » Tue Jul 16, 2019 1:33 pm

Everyone seems to have their own way to do this. I wrote my own C# code to take a png with transparency, and create the tiles, palette and map all together.

The advantage of doing this is that you can dedupe the tiles by comparing all eight flip/rotate combinations. It also lets you have more control over deduping the colours and assigning them into the desired 16 colour subpalette.

I like doing it this way because, once I write the routines, I can build up the tile set gradually line by line like a script, that can be run over and over. I can also name the tiles so they end up with asm labels, letting me refer to them in code later.
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel Spectron 2084blog

User avatar
varmfskii
Posts: 189
Joined: Fri Jun 23, 2017 1:13 pm
Location: Albuquerque, NM USA

Re: Tilemap code example

Post by varmfskii » Wed Jul 17, 2019 11:31 am

I have a set of tools that I wrote and use for these tasks as well. They are command line tools.
Backer #2741 - TS2068, Byte, ZX Evolution

Tau Ceti
Posts: 11
Joined: Fri Jul 07, 2017 3:00 pm

Re: Tilemap code example

Post by Tau Ceti » Wed Jul 17, 2019 5:06 pm

I found your tools on Github and they work great.

Thanks!

Post Reply