Mercurial > repos > blastem
annotate transz80.c @ 502:7fdfcced65b6
Fix allocation and memset size for field buffers when using OpenGL
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 29 Oct 2013 23:13:15 -0700 |
parents | 140af5509ce7 |
children | b7b7a1cab44a |
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 |
140af5509ce7
Added copyright notice to source files and added GPL license text in COPYING
Mike Pavone <pavone@retrodev.com>
parents:
316
diff
changeset
|
3 This file is part of BlastEm. |
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 |
235
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 int main(int argc, char ** argv) |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 { |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 long filesize; |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 uint8_t *filebuf; |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 x86_z80_options opts; |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 z80_context context; |
245
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
38 if (argc < 2) { |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
39 fputs("usage: transz80 zrom [cartrom]\n", stderr); |
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 FILE * f = fopen(argv[1], "rb"); |
245
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
43 if (!f) { |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
44 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
|
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 fseek(f, 0, SEEK_END); |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 filesize = ftell(f); |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 fseek(f, 0, SEEK_SET); |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 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
|
51 fclose(f); |
245
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
52 if (argc > 2) { |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
53 f = fopen(argv[2], "rb"); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
54 if (!f) { |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
55 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
|
56 exit(1); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
57 } |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
58 fseek(f, 0, SEEK_END); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
59 filesize = ftell(f); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
60 fseek(f, 0, SEEK_SET); |
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
61 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
|
62 fclose(f); |
250
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
63 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
|
64 { |
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
65 *cur = (*cur >> 8) | (*cur << 8); |
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
66 } |
245
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
67 } |
235
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 init_x86_z80_opts(&opts); |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 init_z80_context(&context, &opts); |
245
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
70 //Z80 RAM |
235
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 context.mem_pointers[0] = z80_ram; |
250
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
72 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
|
73 context.int_cycle = CYCLE_NEVER; |
245
ea3899e3e7ec
Implement cartridge rom loading in transz80
Mike Pavone <pavone@retrodev.com>
parents:
244
diff
changeset
|
74 //cartridge/bank |
250
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
75 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
|
76 z80_reset(&context); |
244
df8a36bf5e1d
Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
235
diff
changeset
|
77 for(;;) |
df8a36bf5e1d
Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
235
diff
changeset
|
78 { |
df8a36bf5e1d
Implement cycle limit in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
235
diff
changeset
|
79 z80_run(&context); |
250
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
80 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
|
81 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
|
82 } |
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
83 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
|
84 context.int_cycle = VINT_CYCLE; |
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
85 } |
5f1b68cecfc7
Implemented basic interrupt support in Z80 core.
Mike Pavone <pavone@retrodev.com>
parents:
245
diff
changeset
|
86 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
|
87 } |
235
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 return 0; |
d9bf8e61c33c
Get Z80 core working for simple programs
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 } |