Debian Codegraph @

terps/taylor/loadtotpicture.c

raw symbols SCIP
1 //
2 // loadtotpicture.c
3 //
4 // Part of TaylorMade, an interpreter for Adventure Soft UK games
5 //
6 // This code is for de-protecting the ZX Spectrum text-only version of Temple of Terror
7 //
8 // Created by Petter Sjölund on 2022-04-19.
9 //
10
11 #include <stdlib.h>
12
13 #include "extracttape.h"
14 #include "loadtotpicture.h"
15 #include "utility.h"
16
17 static uint8_t *mem;
18
19 static uint16_t push(uint16_t reg, uint16_t SP)
20 {
21 SP -= 2;
22 mem[SP] = reg & 0xff;
23 mem[SP + 1] = reg >> 8;
24 return SP;
25 }
26
27 static uint16_t pop(uint16_t *SP)
28 {
29 uint16_t reg = mem[*SP] + mem[*SP + 1] * 256;
30 *SP += 2;
31 return reg;
32 }
33
34 static int parity(uint8_t x)
35 {
36 int bitsset = 0;
37 for (int i = 0; i < 8; i++)
38 if (x & (1 << i))
39 bitsset++;
40 return ((bitsset & 1) == 0);
41 }
42
43 static uint16_t *DrawAdresses = NULL;
44 static int AddressIndex = 0;
45
46 /* The image loader reads a byte from tape, decrypts it and writes the result */
47 /* to a screen adress pulled from the list we create here */
48
49 /* Here we add another screen address to the address list array */
50 static void addtoarray(uint16_t val)
51 {
52 DrawAdresses[AddressIndex++] = val;
53 }
54
55 /* Here we add another attribute address to the address list array */
56 static void addcolour(uint16_t addr, uint8_t IYh)
57 { // COLOUR_LOAD
58 uint8_t msb = addr >> 8;
59 if (!parity(IYh))
60 msb = msb ^ 0xff; // CPL
61 if (msb & 7)
62 return;
63 msb = addr >> 11;
64 msb = (msb & 3) | 0x58; // 0101 1000
65 addtoarray((addr & 0xff) + msb * 256);
66 }
67
68 /* Here we get the address in screen memory representing one line down */
69 /* or up from addr, depending on the parity of the IYh variable */
70 static uint16_t nextlineaddr(uint16_t addr, uint8_t IYh)
71 {
72 uint8_t msb = addr >> 8;
73 uint8_t lsb = addr & 0xff;
74 // byte down
75 if (parity(IYh)) {
76 msb++;
77 if (msb & 7) // 0000 0111
78 return msb * 256 + lsb;
79 lsb += 32;
80 if ((lsb & 0xe0) == 0) // 1110 0000
81 return (msb * 256) + lsb;
82 return (msb - 8) * 256 + lsb;
83 }
84 // byte up
85 msb--;
86 if ((msb ^ 0xff) & 7) // 0000 0111
87 return msb * 256 + lsb;
88 lsb -= 32;
89 if ((lsb ^ 0xff) & 0xe0) { // 1110 0000
90 msb += 8;
91 }
92 return msb * 256 + lsb;
93 }
94
95 uint16_t globalIY, globalBC, globalDE, globalHL;
96 uint8_t globalA;
97
98 static void registers_on_stack(uint8_t ack, uint16_t IY, uint16_t BC, uint16_t DE, uint16_t HL, uint8_t D)
99 {
100 uint16_t SP = D * 11 + 0xee57;
101 // D can be 1, 2 or 3, selecting a different "slot" at 0xee62, 0xee6d or 0xee78 respectively
102 SP = push(IY, SP);
103 SP = push(BC, SP);
104 SP = push(DE, SP);
105 SP = push(HL, SP);
106 uint16_t AF = ack * 256;
107 push(AF, SP);
108 }
109
110 /* Here we determine in which direction to go for the next screen byte */
111 static uint8_t styler(uint8_t ack, uint16_t *IY, uint16_t *HL, uint16_t *DE, uint16_t *BC)
112 { // DIRECTION_OF_LOAD
113 uint16_t SP = ack * 11 + 0xee4d;
114 // A can be 1, 2 or 3, selecting a different "slot" at 0xee58, 0xee63 or 0xee6e, respectively
115 uint16_t AF = pop(&SP);
116 uint8_t H, L;
117 *HL = pop(&SP);
118 *DE = pop(&SP);
119 *BC = pop(&SP);
120 *IY = pop(&SP);
121 if ((AF >> 8) == 0)
122 return 0;
123 uint8_t IYh = *IY >> 8;
124
125 addcolour(*HL, IYh);
126 addtoarray(*HL);
127 *BC -= 256;
128
129 if (IYh < 2) {
130 if ((*BC >> 8) != 0) {
131 *HL = nextlineaddr(*HL, IYh); // find address of next line
132 return 0xff;
133 }
134 H = *DE >> 8;
135 L = *BC & 0xff;
136 if (!parity(IYh)) {
137 L--;
138 } else {
139 L++;
140 }
141 *BC = (*IY & 0xff) * 256 + L;
142 *HL = H * 256 + L;
143 *DE = (*DE & 0xff) - 1;
144 if (*DE == 0)
145 return 0;
146 return 0xff;
147 }
148 // STYLE34
149 H = *HL >> 8;
150 L = *HL & 0xff;
151 if (!parity(IYh)) {
152 L--;
153 } else {
154 L++;
155 }
156 if ((*BC >> 8) != 0) {
157 *HL = H * 256 + L;
158 return 0xff;
159 }
160 *HL = nextlineaddr(H * 256 + (*BC & 0xff), IYh); // find address of next line
161 *BC = (*DE & 0xff) * 256 + (*HL & 0xff);
162 *DE -= 256;
163 if ((*DE >> 8) == 0)
164 return 0;
165 return 0xff;
166 }
167
168 static void address_table(void)
169 {
170 uint16_t IY = 0x032d;
171 uint16_t IX = 0xeeab;
172 uint16_t DE2, BC2, HL2;
173 uint16_t counter = mem[0xeea9] + mem[0xeeaa] * 256;
174 uint8_t ack, B, C;
175 do { // next line
176 ack = mem[IX + 3] >> 5;
177 C = ack + 1;
178 for (int i = C; i > 0; i--) {
179 HL2 = mem[IX] + mem[IX + 1] * 256;
180 ack = mem[IX + 3] & 0x1f; // 0001 1111
181 DE2 = (mem[IX + 2] & 0x3f) + ack * 2048; // 0011 1111
182 ack = mem[IX + 2] >> 6;
183 IY = (IY & 0xff) + ack * 256;
184 BC2 = (HL2 & 0xff) + (DE2 & 0xff) * 256;
185 if (ack < 2) {
186 IY = (DE2 >> 8) + ack * 256;
187 BC2 = mem[IX] + (DE2 >> 8) * 256;
188 DE2 = (DE2 & 0xff) + mem[IX + 1] * 256;
189 }
190 ack++;
191 registers_on_stack(ack, IY, BC2, DE2, HL2, i);
192 IX += 4;
193 counter--;
194 }
195
196 do { // NXTP1
197 B = 0;
198 for (int i = C; i > 0; i--) {
199 ack = styler(i, &IY, &HL2, &DE2, &BC2);
200 registers_on_stack(ack, IY, BC2, DE2, HL2, i);
201 B = ack | B;
202 }
203 } while (B != 0);
204 } while (counter != 0);
205 }
206
207 uint8_t *LoadAlkatrazPicture(uint8_t *memimage, uint8_t *file)
208 {
209 mem = memimage;
210 DrawAdresses = MemAlloc(6912 * sizeof(uint16_t));
211 address_table();
212 uint8_t loacon = 0xf6;
213 DeAlkatraz(file, mem, 0, 0xea7d, 2, &loacon, 0xc1, 0xcb, 1);
214 uint16_t fileoffset = 2;
215 for (int i = 0; i < 6912; i++)
216 DeAlkatraz(file, mem, fileoffset++, DrawAdresses[i], 1, &loacon, 0xc1, 0xcb, 1);
217 free(DrawAdresses);
218 return mem;
219 }