Mercurial > repos > simple16
annotate src/vdp.c @ 12:d4112095b93f
Allow negative decimal literals
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 27 Mar 2016 21:07:41 -0700 |
parents | 04d8efe7a1f0 |
children | 04fc17376999 |
rev | line source |
---|---|
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #include <stdint.h> |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 #include <string.h> |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 #include "vdp.h" |
11
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
4 #include "system.h" |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 void vdp_init(vdp *context, uint32_t clock_div) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 memset(context, 0, sizeof(vdp)); |
11
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
9 //clock div specifies the pixel clock divider |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
10 //but our emulation step is half that fast |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
11 context->clock_inc = clock_div*2; |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
12 context->drawbuffer = context->linebuffers; |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
13 context->readbuffer = context->linebuffers+320; |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 void vdp_run(vdp *context, uint32_t target) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 while (context->cycles < target) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 { |
11
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
20 context->hcounter+=2; |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 if (context->hcounter == 416) { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 context->hcounter = 0; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 context->vcounter++; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 if (context->vcounter == 262) { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 context->vcounter = 0; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 //TODO: do rendering stuff here |
11
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
29 //Draw to framebuffer |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
30 if (context->vcounter > 8 && context->vcounter < 249 && context->hcounter < 320) { |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
31 if (!context->hcounter && context->vcounter == 9) { |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
32 context->framebuffer = system_get_framebuffer(&context->pitch); |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
33 //pitch is in terms of bytes, but we want it in terms of pixels |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
34 context->pitch /= sizeof(uint16_t); |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
35 } |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
36 uint16_t *dest = context->framebuffer + (context->vcounter - 9) * context->pitch + context->hcounter; |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
37 if (context->status & VDP_STATUS_ENABLED && context->vcounter > 16 && context->vcounter < 241) { |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
38 *dest = context->cram[0x3F & context->readbuffer[context->hcounter]]; |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
39 dest++; |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
40 *dest = context->cram[0x3F & context->readbuffer[context->hcounter+1]]; |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
41 } else { |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
42 //Display is disabled or we're in the border area, draw the background color |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
43 *dest = *context->cram; |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
44 dest++; |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
45 *dest = *context->cram; |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
46 } |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
47 } else if(!context->hcounter && context->vcounter == 249) { |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
48 system_framebuffer_updated(); |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
49 context->framebuffer = NULL; |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
50 } |
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
51 //Handle the FIFO |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 if (context->status & VDP_STATUS_FIFO) { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 switch (context->fifo_dest) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 case FIFO_DEST_VRAM: |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 if (!(context->status & VDP_STATUS_VRAM)) { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 context->vram[context->dest_offset++] = context->fifo; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 context->dest_offset &= sizeof(context->vram)/2-1; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 context->status &= ~VDP_STATUS_FIFO; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 break; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 case FIFO_DEST_SRAM: |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 if (!(context->status & VDP_STATUS_SRAM)) { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 context->sram[context->dest_offset++] = context->fifo; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 context->dest_offset &= sizeof(context->sram)/2-1; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 context->status &= ~VDP_STATUS_FIFO; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 break; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 case FIFO_DEST_CRAM: |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 context->cram[context->dest_offset++] = context->fifo; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 context->dest_offset &= sizeof(context->cram)/2-1; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 context->status &= ~VDP_STATUS_FIFO; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 break; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 context->cycles += context->clock_inc; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 void vdp_write_address(vdp *context, uint16_t value) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 context->status &= ~VDP_STATUS_FIFO; |
11
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
82 if (!(value & 0x8000)) { |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 context->fifo_dest = FIFO_DEST_VRAM; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 context->dest_offset = (value & (sizeof(context->vram) -1))/2; |
11
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
85 } else if ((value & 0xFF00) == 0xFE00) { |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 context->fifo_dest = FIFO_DEST_SRAM; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
87 context->dest_offset = (value & (sizeof(context->sram) -1))/2; |
11
04d8efe7a1f0
Initial stab at video output and background color rendering. Fixed address decoding in address port write handler.
Michael Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
88 } else if ((value & 0xFF00) == 0xFF00) { |
8
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 context->fifo_dest = FIFO_DEST_CRAM; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 context->dest_offset = (value & (sizeof(context->cram) -1))/2; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
91 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
92 } |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
93 |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 void vdp_write_data(vdp *context, uint16_t value) |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
95 { |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 context->fifo = value; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 context->status |= VDP_STATUS_FIFO; |
5176efdda5ae
Initial work on VDP emulation
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
98 } |