annotate transz80.c @ 647:5d58dcd94733

Fix the HV counter and adjust the slots of certain VDP events
author Michael Pavone <pavone@retrodev.com>
date Sun, 14 Dec 2014 18:14:50 -0800
parents b7b7a1cab44a
children 759c38bf97f8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
467
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 316
diff changeset
1 /*
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 316
diff changeset
2 Copyright 2013 Michael Pavone
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.
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
3 This file is part of BlastEm.
467
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 316
diff changeset
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.
140af5509ce7 Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents: 316
diff changeset
5 */
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #include "z80inst.h"
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7 #include "z80_to_x86.h"
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
8 #include "mem.h"
250
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
9 #include "vdp.h"
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10 #include <stdio.h>
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
11 #include <stdlib.h>
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
12
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
13 uint8_t z80_ram[0x2000];
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
14 uint16_t cart[0x200000];
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
15
250
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
16 #define MCLKS_PER_Z80 15
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
17 //TODO: Figure out the exact value for this
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
18 #define MCLKS_PER_FRAME (MCLKS_LINE*262)
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
19 #define VINT_CYCLE ((MCLKS_LINE * 226)/MCLKS_PER_Z80)
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
20 #define CYCLE_NEVER 0xFFFFFFFF
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
21
316
fd7c24b97ebf Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents: 250
diff changeset
22 uint8_t z80_read_ym(uint16_t location, z80_context * context)
fd7c24b97ebf Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents: 250
diff changeset
23 {
fd7c24b97ebf Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents: 250
diff changeset
24 return 0xFF;
fd7c24b97ebf Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents: 250
diff changeset
25 }
fd7c24b97ebf Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents: 250
diff changeset
26
fd7c24b97ebf Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents: 250
diff changeset
27 z80_context * z80_write_ym(uint16_t location, z80_context * context, uint8_t value)
fd7c24b97ebf Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents: 250
diff changeset
28 {
fd7c24b97ebf Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents: 250
diff changeset
29 return context;
fd7c24b97ebf Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents: 250
diff changeset
30 }
fd7c24b97ebf Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents: 250
diff changeset
31
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.
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
32 z80_context * z80_vdp_port_write(uint16_t location, z80_context * context, uint8_t value)
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.
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
33 {
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.
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
34 return context;
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.
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
35 }
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.
Michael Pavone <pavone@retrodev.com>
parents: 467
diff changeset
36
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
37 int main(int argc, char ** argv)
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
38 {
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
39 long filesize;
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
40 uint8_t *filebuf;
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
41 x86_z80_options opts;
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
42 z80_context context;
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
43 if (argc < 2) {
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
44 fputs("usage: transz80 zrom [cartrom]\n", stderr);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
45 exit(1);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
46 }
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
47 FILE * f = fopen(argv[1], "rb");
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
48 if (!f) {
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
49 fprintf(stderr, "unable to open file %s\n", argv[2]);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
50 exit(1);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
51 }
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
52 fseek(f, 0, SEEK_END);
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 filesize = ftell(f);
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 fseek(f, 0, SEEK_SET);
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
55 fread(z80_ram, 1, filesize < sizeof(z80_ram) ? filesize : sizeof(z80_ram), f);
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 fclose(f);
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
57 if (argc > 2) {
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
58 f = fopen(argv[2], "rb");
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
59 if (!f) {
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
60 fprintf(stderr, "unable to open file %s\n", argv[2]);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
61 exit(1);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
62 }
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
63 fseek(f, 0, SEEK_END);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
64 filesize = ftell(f);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
65 fseek(f, 0, SEEK_SET);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
66 fread(cart, 1, filesize < sizeof(cart) ? filesize : sizeof(cart), f);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
67 fclose(f);
250
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
68 for(unsigned short * cur = cart; cur - cart < (filesize/2); ++cur)
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
69 {
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
70 *cur = (*cur >> 8) | (*cur << 8);
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
71 }
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
72 }
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 init_x86_z80_opts(&opts);
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 init_z80_context(&context, &opts);
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
75 //Z80 RAM
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
76 context.mem_pointers[0] = z80_ram;
250
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
77 context.sync_cycle = context.target_cycle = MCLKS_PER_FRAME/MCLKS_PER_Z80;
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
78 context.int_cycle = CYCLE_NEVER;
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
79 //cartridge/bank
250
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
80 context.mem_pointers[1] = context.mem_pointers[2] = (uint8_t *)cart;
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
81 z80_reset(&context);
244
df8a36bf5e1d Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 235
diff changeset
82 for(;;)
df8a36bf5e1d Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 235
diff changeset
83 {
df8a36bf5e1d Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 235
diff changeset
84 z80_run(&context);
250
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
85 if (context.current_cycle >= MCLKS_PER_FRAME/MCLKS_PER_Z80) {
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
86 context.current_cycle -= MCLKS_PER_FRAME/MCLKS_PER_Z80;
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
87 }
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
88 if (context.current_cycle < VINT_CYCLE && context.iff1) {
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
89 context.int_cycle = VINT_CYCLE;
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
90 }
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
91 context.target_cycle = context.sync_cycle < context.int_cycle ? context.sync_cycle : context.int_cycle;
244
df8a36bf5e1d Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 235
diff changeset
92 }
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
93 return 0;
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
94 }