Mercurial > repos > simple16
comparison src/vdp.c @ 19:04fc17376999
Sort of working tile rendering and tile test ROM
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 28 Mar 2016 23:43:31 -0700 |
parents | 04d8efe7a1f0 |
children | 91ded3b12d96 |
comparison
equal
deleted
inserted
replaced
18:cc885122e9e3 | 19:04fc17376999 |
---|---|
1 #include <stdint.h> | 1 #include <stdint.h> |
2 #include <string.h> | 2 #include <string.h> |
3 #include <stdio.h> | |
3 #include "vdp.h" | 4 #include "vdp.h" |
4 #include "system.h" | 5 #include "system.h" |
5 | 6 |
6 void vdp_init(vdp *context, uint32_t clock_div) | 7 void vdp_init(vdp *context, uint32_t clock_div) |
7 { | 8 { |
23 context->vcounter++; | 24 context->vcounter++; |
24 if (context->vcounter == 262) { | 25 if (context->vcounter == 262) { |
25 context->vcounter = 0; | 26 context->vcounter = 0; |
26 } | 27 } |
27 } | 28 } |
28 //TODO: do rendering stuff here | 29 context->status &= ~(VDP_STATUS_VRAM|VDP_STATUS_SRAM); |
30 //Render to linebuffer | |
31 if (context->vcounter > 15 && context->vcounter < 240 && context->hcounter < 406) { | |
32 if (context->hcounter < 246) { | |
33 context->status |= VDP_STATUS_VRAM; | |
34 if (!context->hcounter) { | |
35 //flip linebuffers | |
36 if (context->drawbuffer == context->linebuffers) { | |
37 context->drawbuffer = context->linebuffers + 328; | |
38 context->readbuffer = context->linebuffers; | |
39 } else { | |
40 context->drawbuffer = context->linebuffers; | |
41 context->readbuffer = context->linebuffers + 328; | |
42 } | |
43 context->draw_dest = 0; | |
44 } | |
45 if (context->draw_counter) { | |
46 context->draw_counter--; | |
47 uint16_t pixels = context->vram[context->draw_source++]; | |
48 for (int i = 0; i < 4; i++) | |
49 { | |
50 uint8_t pixel = ((pixels >> ((3-i) * 4)) & 0xF) | context->palpriority; | |
51 context->drawbuffer[context->draw_dest++] = pixel; | |
52 } | |
53 } else { | |
54 //00VV VVVV VVHH HHHH | |
55 uint16_t vpos = (context->vscroll & 0x7FF) + context->vcounter - 16; | |
56 uint16_t vmask = (context->vscroll >> 2) & 0x3E00; | |
57 uint16_t vcoarse = (vpos << 3) & 0x3FC0; | |
58 uint16_t vfine = vpos & 7; | |
59 uint16_t hcoarse = ((context->hscroll >> 3) + context->hcounter/6) & 0x3F; | |
60 uint16_t tableaddress = hcoarse | (vcoarse & ~vmask) | ((context->vscroll << 3) & vmask); | |
61 //printf("VCounter: %X, VScroll: %X, HCounter: %X, Table: %X\n", context->vcounter, context->vscroll, context->hcounter, tableaddress); | |
62 uint16_t entry = context->vram[tableaddress]; | |
63 context->draw_source = (entry & 0x3FF) * 16; | |
64 if (entry & 0x1000) { | |
65 context->draw_source += 14 - vfine * 2; | |
66 } else { | |
67 context->draw_source += vfine * 2; | |
68 } | |
69 context->palpriority = entry >> 9 & 0x70; | |
70 context->draw_counter = 2; | |
71 //TODO: handle horizontal flip | |
72 } | |
73 //TODO: Scan sprite table | |
74 } else { | |
75 //TODO: Render sprites | |
76 } | |
77 } | |
29 //Draw to framebuffer | 78 //Draw to framebuffer |
30 if (context->vcounter > 8 && context->vcounter < 249 && context->hcounter < 320) { | 79 if (context->vcounter > 8 && context->vcounter < 249 && context->hcounter < 320) { |
31 if (!context->hcounter && context->vcounter == 9) { | 80 if (!context->hcounter && context->vcounter == 9) { |
32 context->framebuffer = system_get_framebuffer(&context->pitch); | 81 context->framebuffer = system_get_framebuffer(&context->pitch); |
33 //pitch is in terms of bytes, but we want it in terms of pixels | 82 //pitch is in terms of bytes, but we want it in terms of pixels |
94 void vdp_write_data(vdp *context, uint16_t value) | 143 void vdp_write_data(vdp *context, uint16_t value) |
95 { | 144 { |
96 context->fifo = value; | 145 context->fifo = value; |
97 context->status |= VDP_STATUS_FIFO; | 146 context->status |= VDP_STATUS_FIFO; |
98 } | 147 } |
148 | |
149 void vdp_write_hscroll(vdp *context, uint16_t value) | |
150 { | |
151 context->hscroll = value & 0x1FF; | |
152 if (value & 0x8000) { | |
153 context->status |= VDP_STATUS_ENABLED; | |
154 } else { | |
155 context->status &= ~VDP_STATUS_ENABLED; | |
156 } | |
157 } |