annotate transz80.c @ 280:9ee64039ddeb

Fix bank register update
author Mike Pavone <pavone@retrodev.com>
date Fri, 03 May 2013 21:09:48 -0700
parents 5f1b68cecfc7
children fd7c24b97ebf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include "z80inst.h"
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include "z80_to_x86.h"
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include "mem.h"
250
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
4 #include "vdp.h"
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
5 #include <stdio.h>
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #include <stdlib.h>
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
7
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
8 uint8_t z80_ram[0x2000];
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
9 uint16_t cart[0x200000];
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
10
250
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
11 #define MCLKS_PER_Z80 15
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
12 //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
13 #define MCLKS_PER_FRAME (MCLKS_LINE*262)
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
14 #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
15 #define CYCLE_NEVER 0xFFFFFFFF
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
16
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
17 int main(int argc, char ** argv)
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
18 {
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
19 long filesize;
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
20 uint8_t *filebuf;
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
21 x86_z80_options opts;
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
22 z80_context context;
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
23 if (argc < 2) {
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
24 fputs("usage: transz80 zrom [cartrom]\n", stderr);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
25 exit(1);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
26 }
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
27 FILE * f = fopen(argv[1], "rb");
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
28 if (!f) {
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
29 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
30 exit(1);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
31 }
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
32 fseek(f, 0, SEEK_END);
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
33 filesize = ftell(f);
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
34 fseek(f, 0, SEEK_SET);
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
35 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
36 fclose(f);
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
37 if (argc > 2) {
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
38 f = fopen(argv[2], "rb");
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
39 if (!f) {
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
40 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
41 exit(1);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
42 }
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
43 fseek(f, 0, SEEK_END);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
44 filesize = ftell(f);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
45 fseek(f, 0, SEEK_SET);
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
46 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
47 fclose(f);
250
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
48 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
49 {
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
50 *cur = (*cur >> 8) | (*cur << 8);
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
51 }
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
52 }
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
53 init_x86_z80_opts(&opts);
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
54 init_z80_context(&context, &opts);
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
55 //Z80 RAM
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
56 context.mem_pointers[0] = z80_ram;
250
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
57 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
58 context.int_cycle = CYCLE_NEVER;
245
ea3899e3e7ec Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents: 244
diff changeset
59 //cartridge/bank
250
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
60 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
61 z80_reset(&context);
244
df8a36bf5e1d Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 235
diff changeset
62 for(;;)
df8a36bf5e1d Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 235
diff changeset
63 {
df8a36bf5e1d Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 235
diff changeset
64 z80_run(&context);
250
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
65 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
66 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
67 }
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
68 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
69 context.int_cycle = VINT_CYCLE;
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
70 }
5f1b68cecfc7 Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents: 245
diff changeset
71 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
72 }
235
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
73 return 0;
d9bf8e61c33c Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff changeset
74 }