aboutsummaryrefslogtreecommitdiff
path: root/splib.c
blob: 400ca16e967ce0fe8133b74586dc0b3e798dcb1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
   The Return of Traxtor (Amstrad CPC)
   Copyright (C) 2015 Juan J. Martinez <jjm@usebox.net>

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include <stdlib.h>

#include "splib.h"

static struct st_tile tiles[TMW * TMH];
struct st_tile *dirty, *last_dirty;

#define T_CLEAN	0
#define T_DIRTY 1

// faster
struct st_tile *tile_p;

void
pad_numbers(uint8_t *s, uint8_t limit, uint16_t number)
{
    s += limit;
    *s = 0;

    while (limit--)
    {
        *--s = (number % 10) + '0';
        number /= 10;
    }
}

#pragma save
#pragma disable_warning 85
void
set_hw_border(uint8_t c)
{
    __asm;
    ld bc, #0x7f10
    out (c), c
    ld hl, #2
    add hl, sp
    ld c, (hl)
    out (c), c
    __endasm;
}
#pragma restore

#pragma save
#pragma disable_warning 85
void
set_hw_ink(uint8_t ink, uint8_t c)
{
    __asm;
    ld hl, #2
    add hl, sp
    ld a, (hl)
    inc hl
    ld e, (hl)
    ld bc, #0x7f00
    out (c), a
    ld a, #0x40
    or e
    out (c), a
    __endasm;
}
#pragma restore

#pragma save
#pragma disable_warning 85
void
set_hw_mode(uint8_t m)
{
    __asm;
    ld hl, #2
    add hl, sp
    ld e, (hl)
    out (c), a
    ld a, #0x8c
    or e
    ld bc, #0x7f00
    out (c), a
    __endasm;
}
#pragma restore


void
wait_vsync()
{
    __asm;
    ld b, #0xf5
    keep_waiting:
    in a, (c)
    rra
    jr nc, keep_waiting
    __endasm;
}

// this is quite slow, use it only where speed is not an issue
uint16_t
screen_addr(uint16_t x, uint16_t y)
{
    // up to 160 x 200
    return (0xc000 + x + ((y / 8) * 80) + ((y % 8) * 2048));
}

void
init_tiles()
{
    uint8_t i, j;

    for (j = 0; j < TMH; j++)
        for (i = 0; i < TMW; i++)
        {
            tiles[i + j * TMW].t = NULL;
            tiles[i + j * TMW].saddr = screen_addr((uint16_t)(20 + i * TW / 2), (uint16_t)(8 + j * TH));
            tiles[i + j * TMW].baddr = BUFF_ADDR + (i * TW / 2) + (j * TH * TMW * TW / 2);
            tiles[i + j * TMW].n = NULL;
            tiles[i + j * TMW].dirty = T_CLEAN;
        }

    dirty = NULL;
    last_dirty = NULL;
}

void
update_screen()
{
    // tiles are expected to be 12x9 pixels
    __asm;
    ld b, #0xf5
    update_keep_waiting:
    in a, (c)
    rra
    jr nc, update_keep_waiting

    ld bc, (_dirty)

    update_loop:
    ld a, b
    or c
    jr z, update_done

    ld hl, #2
    add hl, bc
    ld e, (hl)
    inc hl
    ld d, (hl)
    inc hl
    ld a, (hl)
    inc hl
    ld h, (hl)
    ld l, a

    push bc
    ld a, #9
    put_tile0:
    ldi
    ldi
    ldi
    ldi
    ldi
    ldi

    dec a
    jp z, put_tile2

    ld bc, #0x24
    add hl, bc

    ex de, hl
    ld bc, #0x07fa
    add hl, bc
    jp nc, put_tile1
    ld bc, #0xc050
    add hl, bc
    put_tile1:
    ex de, hl
    jr put_tile0

    put_tile2:
    pop bc

    xor a
    ld hl, #6
    add hl, bc
    ld (hl), a

    inc hl
    ld c, (hl)
    inc hl
    ld b, (hl)

    jp update_loop

    update_done:
    __endasm;

    dirty = NULL;
    last_dirty = NULL;
}

void
validate_screen()
{
    for (; dirty; dirty = dirty->n)
        dirty->dirty = T_CLEAN;

    dirty = NULL;
    last_dirty = NULL;
}

void
invalidate_screen()
{
    uint8_t i;

    for (i = 0; i < (TMW * TMH) - 1; i++)
    {
        tiles[i].dirty = T_DIRTY;
        tiles[i].n = &tiles[i + 1];
    }

    tiles[i].dirty = T_DIRTY;
    tiles[i].n = NULL;

    dirty = tiles;
    last_dirty = &tiles[i];
}

void
invalidate_tile(struct st_tile *st)
{
    if (st->dirty)
        return;

    st->dirty = T_DIRTY;
    st->n = NULL;

    if (!dirty)
    {
        dirty = st;
        last_dirty = st;
    }
    else
    {
        last_dirty->n = st;
        last_dirty = st;
    }
}

inline void
invalidate_tile_xy(uint8_t x, uint8_t y)
{
    // x and y in tilemap coordinates

    invalidate_tile(&tiles[x + y * TMW]);
}

void
erase_tile(struct st_tile *st)
{
    if (!st || !st->t)
        return;

    invalidate_tile(st);

    tile_p = st;
    __asm;
    ld ix, (_tile_p)

    ld e, 4(ix)
    ld d, 5(ix)
    ld l, 0(ix)
    ld h, 1(ix)

    ld a, #9
    blit_tile0:
    ldi
    ldi
    ldi
    ldi
    ldi
    ldi

    ex de, hl
    ld bc, #0x24
    add hl, bc
    ex de, hl

    dec a
    jp nz, blit_tile0
    __endasm;
}

inline void
erase_tile_xy(uint8_t x, uint8_t y)
{
    // x and y in tilemap coordinates
    erase_tile(&tiles[x + y * TMW]);
}

void
put_tile(uint8_t *t, uint8_t x, uint8_t y)
{
    // x and y in tilemap coordinates
    tile_p = &tiles[x + y * TMW];
    tile_p->t = t;

    erase_tile(tile_p);
}

// EOF