annotate z80_util.c @ 1750:01236179fc71

Optimization to memory access in new Z80 core
author Michael Pavone <pavone@retrodev.com>
date Sat, 09 Feb 2019 11:34:31 -0800
parents 48a43dff4dc0
children d6d4c006a7b3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1748
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
1 #include <string.h>
1706
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 void z80_read_8(z80_context *context)
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 {
1715
4fd84c3efc72 Implement 16-bit addition in new Z80 core along with necessary CPU DSL fixes to make them work right
Michael Pavone <pavone@retrodev.com>
parents: 1706
diff changeset
5 context->cycles += 3 * context->opts->gen.clock_divider;
1750
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
6 uint8_t *fast = context->fastmem[context->scratch1 >> 10];
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
7 if (fast) {
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
8 context->scratch1 = fast[context->scratch1 & 0x3FF];
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
9 } else {
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
10 context->scratch1 = read_byte(context->scratch1, NULL, &context->opts->gen, context);
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
11 }
1706
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 }
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 void z80_write_8(z80_context *context)
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 {
1715
4fd84c3efc72 Implement 16-bit addition in new Z80 core along with necessary CPU DSL fixes to make them work right
Michael Pavone <pavone@retrodev.com>
parents: 1706
diff changeset
16 context->cycles += 3 * context->opts->gen.clock_divider;
1750
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
17 uint8_t *fast = context->fastmem[context->scratch2 >> 10];
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
18 if (fast) {
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
19 fast[context->scratch2 & 0x3FF] = context->scratch1;
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
20 } else {
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
21 write_byte(context->scratch2, context->scratch1, NULL, &context->opts->gen, context);
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
22 }
1706
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
23 }
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
24
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
25 void z80_io_read8(z80_context *context)
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
26 {
1735
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
27 uint32_t tmp_mask = context->opts->gen.address_mask;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
28 memmap_chunk const *tmp_map = context->opts->gen.memmap;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
29 uint32_t tmp_chunks = context->opts->gen.memmap_chunks;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
30
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
31 context->opts->gen.address_mask = context->io_mask;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
32 context->opts->gen.memmap = context->io_map;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
33 context->opts->gen.memmap_chunks = context->io_chunks;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
34
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
35 context->scratch1 = read_byte(context->scratch1, NULL, &context->opts->gen, context);
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
36
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
37 context->opts->gen.address_mask = tmp_mask;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
38 context->opts->gen.memmap = tmp_map;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
39 context->opts->gen.memmap_chunks = tmp_chunks;
1706
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 }
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 void z80_io_write8(z80_context *context)
c2324849a5e5 Initial checkin of new WIP Z80 core using CPU DSL
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 {
1735
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
44 uint32_t tmp_mask = context->opts->gen.address_mask;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
45 memmap_chunk const *tmp_map = context->opts->gen.memmap;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
46 uint32_t tmp_chunks = context->opts->gen.memmap_chunks;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
47
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
48 context->opts->gen.address_mask = context->io_mask;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
49 context->opts->gen.memmap = context->io_map;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
50 context->opts->gen.memmap_chunks = context->io_chunks;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
51
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
52 write_byte(context->scratch2, context->scratch1, NULL, &context->opts->gen, context);
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
53
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
54 context->opts->gen.address_mask = tmp_mask;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
55 context->opts->gen.memmap = tmp_map;
ca2336469397 Get new Z80 core running in CPM harness
Michael Pavone <pavone@retrodev.com>
parents: 1715
diff changeset
56 context->opts->gen.memmap_chunks = tmp_chunks;
1748
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
57 }
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
58
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
59 //quick hack until I get a chance to change which init method these get passed to
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
60 static memmap_chunk const * tmp_io_chunks;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
61 static uint32_t tmp_num_io_chunks, tmp_io_mask;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
62 void init_z80_opts(z80_options * options, memmap_chunk const * chunks, uint32_t num_chunks, memmap_chunk const * io_chunks, uint32_t num_io_chunks, uint32_t clock_divider, uint32_t io_address_mask)
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
63 {
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
64 memset(options, 0, sizeof(*options));
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
65 options->gen.memmap = chunks;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
66 options->gen.memmap_chunks = num_chunks;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
67 options->gen.address_mask = 0xFFFF;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
68 options->gen.max_address = 0xFFFF;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
69 options->gen.clock_divider = clock_divider;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
70 tmp_io_chunks = io_chunks;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
71 tmp_num_io_chunks = num_io_chunks;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
72 tmp_io_mask = io_address_mask;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
73 }
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
74
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
75 z80_context * init_z80_context(z80_options *options)
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
76 {
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
77 z80_context *context = calloc(1, sizeof(z80_context));
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
78 context->opts = options;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
79 context->io_map = (memmap_chunk *)tmp_io_chunks;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
80 context->io_chunks = tmp_num_io_chunks;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
81 context->io_mask = tmp_io_mask;
1750
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
82 for(uint32_t address = 0; address < 0x10000; address+=1024)
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
83 {
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
84 uint8_t *start = get_native_pointer(address, NULL, &options->gen);
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
85 if (start) {
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
86 uint8_t *end = get_native_pointer(address + 1023, NULL, &options->gen);
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
87 if (end && end - start == 1023) {
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
88 context->fastmem[address >> 10] = start;
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
89 }
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
90 }
01236179fc71 Optimization to memory access in new Z80 core
Michael Pavone <pavone@retrodev.com>
parents: 1748
diff changeset
91 }
1748
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
92 return context;
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
93 }
48a43dff4dc0 Added init functions to z80_util.c so new Z80 core is closer to a drop in replacement for the old one
Michael Pavone <pavone@retrodev.com>
parents: 1735
diff changeset
94