comparison ztestgen.c @ 505:b7b7a1cab44a

The local clone on my laptop got messed up and some changes had not been pushed. This commit represents the status of the working copy from that clone. It unfortunately contains some changes that I did not intend to commit yet, but this seems like the best option at the moment.
author Michael Pavone <pavone@retrodev.com>
date Mon, 06 Jan 2014 22:54:05 -0800
parents 140af5509ce7
children 33ff0171301b
comparison
equal deleted inserted replaced
504:7b0df1aaf384 505:b7b7a1cab44a
1 /* 1 /*
2 Copyright 2013 Michael Pavone 2 Copyright 2013 Michael Pavone
3 This file is part of BlastEm. 3 This file is part of BlastEm.
4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text. 4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
5 */ 5 */
6 #include "z80inst.h" 6 #include "z80inst.h"
7 #include <stdlib.h> 7 #include <stdlib.h>
8 #include <string.h> 8 #include <string.h>
156 if (inst->reg == Z80_USE_IMMED || addr_mode == Z80_IMMED || addr_mode == Z80_IMMED_INDIRECT 156 if (inst->reg == Z80_USE_IMMED || addr_mode == Z80_IMMED || addr_mode == Z80_IMMED_INDIRECT
157 || addr_mode == Z80_IX_DISPLACE || addr_mode == Z80_IY_DISPLACE) 157 || addr_mode == Z80_IX_DISPLACE || addr_mode == Z80_IY_DISPLACE)
158 { 158 {
159 memcpy(&copy, inst, sizeof(copy)); 159 memcpy(&copy, inst, sizeof(copy));
160 inst = &copy; 160 inst = &copy;
161 if ((inst->reg == Z80_USE_IMMED && inst->op != Z80_BIT && inst->op != Z80_RES && inst->op != Z80_SET) 161 if ((inst->reg == Z80_USE_IMMED && inst->op != Z80_BIT && inst->op != Z80_RES && inst->op != Z80_SET)
162 || (addr_mode == Z80_IMMED && inst->op != Z80_IM)) 162 || (addr_mode == Z80_IMMED && inst->op != Z80_IM))
163 { 163 {
164 copy.immed = rand() % (word_sized ? 65536 : 256); 164 copy.immed = rand() % (word_sized ? 65536 : 256);
165 } 165 }
166 if (addr_mode == Z80_IX_DISPLACE || addr_mode == Z80_IY_DISPLACE) { 166 if (addr_mode == Z80_IX_DISPLACE || addr_mode == Z80_IY_DISPLACE) {
234 } 234 }
235 address = 0x1000 + offset; 235 address = 0x1000 + offset;
236 break; 236 break;
237 } 237 }
238 if (inst->reg != Z80_UNUSED && inst->reg != Z80_USE_IMMED) { 238 if (inst->reg != Z80_UNUSED && inst->reg != Z80_USE_IMMED) {
239 reg_usage[inst->reg] = 1; 239
240 if (word_sized) { 240 if (word_sized) {
241 reg_values[inst->reg] = rand() % 65536; 241 reg_values[inst->reg] = rand() % 65536;
242 reg_usage[z80_high_reg(inst->reg)] = 1; 242 reg_usage[z80_high_reg(inst->reg)] = 1;
243 reg_values[z80_high_reg(inst->reg)] = reg_values[inst->reg] >> 8; 243 reg_values[z80_high_reg(inst->reg)] = reg_values[inst->reg] >> 8;
244 reg_usage[z80_low_reg(inst->reg)] = 1; 244 reg_usage[z80_low_reg(inst->reg)] = 1;
245 reg_values[z80_low_reg(inst->reg)] = reg_values[inst->reg] & 0xFF; 245 reg_values[z80_low_reg(inst->reg)] = reg_values[inst->reg] & 0xFF;
246 } else { 246 } else {
247 reg_values[inst->reg] = rand() % 255; 247 if (!reg_usage[inst->reg]) {
248 uint8_t word_reg = z80_word_reg(inst->reg); 248 reg_values[inst->reg] = rand() % 255;
249 if (word_reg != Z80_UNUSED) { 249 uint8_t word_reg = z80_word_reg(inst->reg);
250 reg_usage[word_reg] = 1; 250 if (word_reg != Z80_UNUSED) {
251 reg_values[word_reg] = (reg_values[z80_high_reg(word_reg)] << 8) | (reg_values[z80_low_reg(word_reg)] & 0xFF); 251 reg_usage[word_reg] = 1;
252 } 252 reg_values[word_reg] = (reg_values[z80_high_reg(word_reg)] << 8) | (reg_values[z80_low_reg(word_reg)] & 0xFF);
253 } 253 }
254 }
255 }
256 reg_usage[inst->reg] = 1;
254 } 257 }
255 puts("--------------"); 258 puts("--------------");
256 for (uint8_t reg = 0; reg < Z80_UNUSED; reg++) { 259 for (uint8_t reg = 0; reg < Z80_UNUSED; reg++) {
257 if (reg_values[reg]) { 260 if (reg_values[reg]) {
258 printf("%s: %X\n", z80_regs[reg], reg_values[reg]); 261 printf("%s: %X\n", z80_regs[reg], reg_values[reg]);
285 } 288 }
286 //setup AF 289 //setup AF
287 cur = ld_ir16(cur, Z80_BC, reg_values[Z80_A] << 8 | (i ? 0xFF : 0)); 290 cur = ld_ir16(cur, Z80_BC, reg_values[Z80_A] << 8 | (i ? 0xFF : 0));
288 cur = push(cur, Z80_BC); 291 cur = push(cur, Z80_BC);
289 cur = pop(cur, Z80_AF); 292 cur = pop(cur, Z80_AF);
290 293
291 //setup other regs 294 //setup other regs
292 for (uint8_t reg = Z80_BC; reg <= Z80_IY; reg++) { 295 for (uint8_t reg = Z80_BC; reg <= Z80_IY; reg++) {
293 if (reg != Z80_AF && reg != Z80_SP) { 296 if (reg != Z80_AF && reg != Z80_SP) {
294 cur = ld_ir16(cur, reg, reg_values[reg]); 297 cur = ld_ir16(cur, reg, reg_values[reg]);
295 } 298 }
296 } 299 }
297 300
298 //copy instruction 301 //copy instruction
299 if (instlen == 3) { 302 if (instlen == 3) {
300 memcpy(cur, instbuf, 2); 303 memcpy(cur, instbuf, 2);
301 cur += 2; 304 cur += 2;
302 } else { 305 } else {
303 memcpy(cur, instbuf, instlen); 306 memcpy(cur, instbuf, instlen);
304 cur += instlen; 307 cur += instlen;
305 } 308 }
306 309
307 //immed/displacement byte(s) 310 //immed/displacement byte(s)
308 if (addr_mode == Z80_IX_DISPLACE || addr_mode == Z80_IY_DISPLACE) { 311 if (addr_mode == Z80_IX_DISPLACE || addr_mode == Z80_IY_DISPLACE) {
309 *(cur++) = inst->ea_reg; 312 *(cur++) = inst->ea_reg;
310 } else if (addr_mode == Z80_IMMED & inst->op != Z80_IM) { 313 } else if (addr_mode == Z80_IMMED & inst->op != Z80_IM) {
311 *(cur++) = inst->immed & 0xFF; 314 *(cur++) = inst->immed & 0xFF;
323 *(cur++) = instbuf[2]; 326 *(cur++) = instbuf[2];
324 } 327 }
325 if (!i) { 328 if (!i) {
326 //Save AF from first run 329 //Save AF from first run
327 cur = push(cur, Z80_AF); 330 cur = push(cur, Z80_AF);
331 if (is_mem) {
332 //Save memory location from frist run
333 cur = ld_mema(cur, address);
334 cur = push(cur, Z80_AF);
335 }
328 } else { 336 } else {
329 //Pop AF from first run for final result 337 //Pop AF from first run for final result
330 for (int reg = Z80_BC; reg <= Z80_IY; reg++) { 338 for (int reg = Z80_BC; reg <= Z80_IY; reg++) {
331 if (reg != Z80_AF && !reg_usage[reg]) { 339 if (reg != Z80_AF && !reg_usage[reg]) {
332 cur = pop(cur, reg); 340 cur = pop(cur, reg);
338 reg_usage[reg] = 1; 346 reg_usage[reg] = 1;
339 reg_usage[z80_low_reg(reg)] = 1; 347 reg_usage[z80_low_reg(reg)] = 1;
340 break; 348 break;
341 } 349 }
342 } 350 }
351 if (is_mem) {
352 //Pop memory location from frist run
353 for (int reg = Z80_BC; reg <= Z80_IY; reg++) {
354 if (reg != Z80_AF && !reg_usage[reg]) {
355 cur = pop(cur, reg);
356 cur = push(cur, Z80_AF);
357 cur = ld_mema(cur, address);
358 cur = ld_rr8(cur, Z80_A, z80_low_reg(reg));
359 cur = pop(cur, Z80_AF);
360 reg_usage[reg] = 1;
361 reg_usage[z80_low_reg(reg)] = 1;
362 break;
363 }
364 }
365 }
343 } 366 }
344 } 367 }
345 368
346 for (char * cur = disbuf; *cur != 0; cur++) { 369 for (char * cur = disbuf; *cur != 0; cur++) {
347 if (*cur == ',' || *cur == ' ') { 370 if (*cur == ',' || *cur == ' ') {
362 } 385 }
363 } 386 }
364 cur = pop(cur, Z80_AF); 387 cur = pop(cur, Z80_AF);
365 } 388 }
366 } 389 }
367 390
368 //halt 391 //halt
369 *(cur++) = 0x76; 392 *(cur++) = 0x76;
370 sprintf(pathbuf + strlen(pathbuf), "/%s.bin", disbuf); 393 sprintf(pathbuf + strlen(pathbuf), "/%s.bin", disbuf);
371 FILE * progfile = fopen(pathbuf, "wb"); 394 FILE * progfile = fopen(pathbuf, "wb");
372 fwrite(prog, 1, cur - prog, progfile); 395 fwrite(prog, 1, cur - prog, progfile);
374 } 397 }
375 398
376 399
377 uint8_t should_skip(z80inst * inst) 400 uint8_t should_skip(z80inst * inst)
378 { 401 {
379 return inst->op >= Z80_JP || (inst->op >= Z80_LDI && inst->op <= Z80_CPDR) || inst->op == Z80_HALT 402 return inst->op >= Z80_JP || (inst->op >= Z80_LDI && inst->op <= Z80_CPDR) || inst->op == Z80_HALT
380 || inst->op == Z80_DAA || inst->op == Z80_RLD || inst->op == Z80_RRD || inst->op == Z80_NOP 403 || inst->op == Z80_DAA || inst->op == Z80_RLD || inst->op == Z80_RRD || inst->op == Z80_NOP
381 || inst->op == Z80_DI || inst->op == Z80_EI; 404 || inst->op == Z80_DI || inst->op == Z80_EI;
382 } 405 }
383 406
384 void z80_gen_all() 407 void z80_gen_all()