Mercurial > repos > blastem
annotate transz80.c @ 340:58a085cfc6bd
Set window title based on ROM header name
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Wed, 15 May 2013 23:32:21 -0700 |
parents | fd7c24b97ebf |
children | 140af5509ce7 |
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 |
316
fd7c24b97ebf
Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents:
250
diff
changeset
|
17 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
|
18 { |
fd7c24b97ebf
Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents:
250
diff
changeset
|
19 return 0xFF; |
fd7c24b97ebf
Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents:
250
diff
changeset
|
20 } |
fd7c24b97ebf
Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents:
250
diff
changeset
|
21 |
fd7c24b97ebf
Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents:
250
diff
changeset
|
22 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
|
23 { |
fd7c24b97ebf
Add YM2612 stubs to transz80
Mike Pavone <pavone@retrodev.com>
parents:
250
diff
changeset
|
24 return context; |
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 |
235
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 int main(int argc, char ** argv) |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 { |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 long filesize; |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 uint8_t *filebuf; |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 x86_z80_options opts; |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 z80_context context; |
245
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
33 if (argc < 2) { |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
34 fputs("usage: transz80 zrom [cartrom]\n", stderr); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
35 exit(1); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
36 } |
235
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 FILE * f = fopen(argv[1], "rb"); |
245
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
38 if (!f) { |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
39 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
|
40 exit(1); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
41 } |
235
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 fseek(f, 0, SEEK_END); |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 filesize = ftell(f); |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 fseek(f, 0, SEEK_SET); |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 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
|
46 fclose(f); |
245
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
47 if (argc > 2) { |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
48 f = fopen(argv[2], "rb"); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
49 if (!f) { |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
50 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
|
51 exit(1); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
52 } |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
53 fseek(f, 0, SEEK_END); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
54 filesize = ftell(f); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
55 fseek(f, 0, SEEK_SET); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
56 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
|
57 fclose(f); |
250
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
58 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
|
59 { |
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
60 *cur = (*cur >> 8) | (*cur << 8); |
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
61 } |
245
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
62 } |
235
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 init_x86_z80_opts(&opts); |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 init_z80_context(&context, &opts); |
245
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
65 //Z80 RAM |
235
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 context.mem_pointers[0] = z80_ram; |
250
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
67 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
|
68 context.int_cycle = CYCLE_NEVER; |
245
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
69 //cartridge/bank |
250
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
70 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
|
71 z80_reset(&context); |
244
df8a36bf5e1d
Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
235
diff
changeset
|
72 for(;;) |
df8a36bf5e1d
Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
235
diff
changeset
|
73 { |
df8a36bf5e1d
Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
235
diff
changeset
|
74 z80_run(&context); |
250
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
75 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
|
76 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
|
77 } |
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
78 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
|
79 context.int_cycle = VINT_CYCLE; |
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
80 } |
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
81 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
|
82 } |
235
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 return 0; |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 } |