amiga_imgv

view src/amiga/gfx.c @ 4:0fd37effde29

progress
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Oct 2017 11:36:18 +0300
parents 663471a80c21
children 0d3d7b020e6a
line source
1 #include <stdio.h>
2 #include <proto/exec.h>
3 #include "gfx.h"
4 #include "copper.h"
5 #include "hwregs.h"
6 #include "logger.h"
7 #include "image.h"
9 static int scr_width, scr_height;
10 static int fb_width, fb_height;
11 static int num_bitplanes;
12 static uint16_t prev_intena, prev_intreq, prev_adkcon, prev_dmacon;
14 static unsigned char *framebuf;
15 static unsigned long fbsize;
16 static int own_framebuf;
18 int gfx_init(int nbpl, unsigned int flags)
19 {
20 uint16_t bplcon0;
22 num_bitplanes = nbpl;
23 scr_width = fb_width = (flags & GFX_HIRES) ? 640 : 320;
24 scr_height = fb_height = (flags & GFX_ILACE) ? 512 : 256;
26 Forbid();
28 prev_dmacon = REG_DMACONR;
29 REG_DMACON = CLRBITS(DMA_ALL);
31 prev_intena = REG_INTENAR;
32 prev_intreq = REG_INTREQR;
33 prev_adkcon = REG_ADKCONR;
35 if(init_copper(0, 0) == -1) {
36 return -1;
37 }
39 if(!gfx_set_framebuffer(0, scr_width, scr_height)) {
40 gfx_shutdown();
41 return -1;
42 }
44 bplcon0 = BPLCON0_COUNT(nbpl) | BPLCON0_COLOR;
45 if(flags & GFX_HIRES) bplcon0 |= BPLCON0_HIRES;
46 if(flags & GFX_ILACE) bplcon0 |= BPLCON0_LACE;
47 if(flags & GFX_HAM) bplcon0 |= BPLCON0_HOMOD;
48 if(flags & GFX_DBLPF) bplcon0 |= BPLCON0_DBLPF;
50 REG_BPLCON0 = bplcon0;
51 REG_BPLCON1 = 0; /* h-scroll */
52 REG_DIWSTART = 0x2c81; /* 81h horiz start, 2ch vertical start */
53 REG_DIWSTOP = 0x2cc1;
54 REG_DDFSTART = (flags & GFX_HIRES) ? 0x3c : 0x38;
55 REG_DDFSTOP = (flags & GFX_HIRES) ? 0xd4 : 0xd0;
57 gfx_wait_vblank();
58 gfx_begin_copperlist();
59 add_copper(COPPER_END);
61 logmsg("starting DMA\n");
62 REG_DMACON = SETBITS(DMA_BPL | DMA_COPPER | DMA_MASTER);
63 return 0;
64 }
66 void gfx_shutdown(void)
67 {
68 REG_DMACON = CLRBITS(DMA_ALL);
69 REG_DMACON = SETBITS(prev_dmacon);
71 REG_INTREQ = CLRBITS(0x7fff);
72 REG_INTREQ = SETBITS(prev_intreq);
74 REG_ADKCON = CLRBITS(0x7fff);
75 REG_ADKCON = SETBITS(prev_adkcon);
77 REG_INTENA = CLRBITS(INTEN_ALL);
78 REG_INTENA = SETBITS(prev_intena);
80 if(framebuf && own_framebuf) {
81 FreeMem(framebuf, fbsize);
82 }
83 framebuf = 0;
85 cleanup_copper();
86 Permit();
87 }
89 int gfx_screen_width(void)
90 {
91 return scr_width;
92 }
94 int gfx_screen_height(void)
95 {
96 return scr_height;
97 }
99 void *gfx_set_framebuffer(void *fb, int width, int height)
100 {
101 /*unsigned int bpl_scanline_sz = width / 8;*/
102 unsigned long sz = width * height / 8 * num_bitplanes;
103 uint32_t bpladdr;
104 uint16_t bplmod;
106 if(framebuf && own_framebuf) {
107 FreeMem(framebuf, fbsize);
108 }
110 if(fb) {
111 framebuf = fb;
112 own_framebuf = 0;
113 } else {
114 if(!(framebuf = AllocMem(sz, MEMF_CHIP))) {
115 logmsg("gfx_set_framebuffer failed to allocate %lu bytes of chip mem for framebuffer\n", sz);
116 return 0;
117 }
118 own_framebuf = 1;
119 }
121 fb_width = width;
122 fb_height = height;
123 fbsize = sz;
125 bpladdr = (uint32_t)framebuf;
126 logmsg("bitplane address: %lx\n", (unsigned long)bpladdr);
128 bplmod = (fb_width - scr_width) / 8 + fb_width / 8 * (num_bitplanes - 1);
129 REG_BPL1MOD = bplmod;
130 REG_BPL2MOD = bplmod;
132 /*
133 REG32_BPL1PT = bpladdr; bpladdr += bpl_scanline_sz;
134 REG32_BPL2PT = bpladdr; bpladdr += bpl_scanline_sz;
135 REG32_BPL3PT = bpladdr; bpladdr += bpl_scanline_sz;
136 REG32_BPL4PT = bpladdr; bpladdr += bpl_scanline_sz;
137 REG32_BPL5PT = bpladdr; bpladdr += bpl_scanline_sz;
138 REG32_BPL6PT = bpladdr;
139 */
140 return framebuf;
141 }
143 void *gfx_get_framebuffer(void)
144 {
145 return framebuf;
146 }
148 int gfx_framebuffer_width(void)
149 {
150 return fb_width;
151 }
153 int gfx_framebuffer_height(void)
154 {
155 return fb_height;
156 }
158 void gfx_begin_copperlist(void)
159 {
160 static const uint16_t bplptr[] = {
161 REGN_BPL1PTH, REGN_BPL2PTH, REGN_BPL3PTH, REGN_BPL4PTH, REGN_BPL5PTH, REGN_BPL6PTH
162 };
163 int i;
164 uint32_t bpladdr;
165 bpladdr = (uint32_t)framebuf;
167 for(i=0; i<num_bitplanes; i++) {
168 add_copper(COPPER_MOVE(bplptr[i], bpladdr >> 16));
169 add_copper(COPPER_MOVE(bplptr[i] + 2, bpladdr));
170 bpladdr += fb_width / 8 * num_bitplanes;
171 }
172 }
174 static int mouse_bnstate(void)
175 {
176 return (REG_CIAA_PORTA & CIAA_PA_FIR0) ? 0 : 1;
177 }
179 int gfx_next_event(union gfx_event *ev, int block)
180 {
181 /* TODO */
182 if(block) {
183 while(!mouse_bnstate());
184 ev->type = GFX_EV_QUIT;
185 return 1;
186 }
188 if(mouse_bnstate()) {
189 ev->type = GFX_EV_QUIT;
190 return 1;
191 }
193 return 0;
194 }
196 void gfx_wait_vpos(int x)
197 {
198 x <<= 8;
199 while((REG32_VPOSR & 0x1ff00) < x);
200 }
202 void gfx_wait_vblank(void)
203 {
204 gfx_wait_vpos(300);
205 }
207 void gfx_show_image(struct ham_image *img)
208 {
209 int i, j, k, fbwidth, fbheight, ncolors, prev_line;
210 unsigned char *fbptr = gfx_get_framebuffer();
211 struct palchange *chg = img->chglist;
213 fbwidth = gfx_framebuffer_width();
214 fbheight = gfx_framebuffer_height();
216 logmsg("showing ham image %dx%d\n", fbwidth, fbheight);
218 memcpy(fbptr, img->pixels, fbwidth * fbheight / 8 * num_bitplanes);
220 /* create copper list that handles the palette */
221 clear_copper();
222 gfx_begin_copperlist();
223 /* initial palette at the start of frame */
224 for(i=0; i<16; i++) {
225 add_copper(COPPER_MOVE(REGN_COLOR(i), img->palette[i]));
226 logmsg("copper palette[%d]: %x\n", i, (unsigned int)img->palette[i]);
227 }
228 #if 0
229 /* add copper instructions for palette changes according to the image changelist */
230 prev_line = -1;
231 while(chg) {
232 assert(chg->line >= prev_line);
233 if(chg->line != prev_line) {
234 prev_line = chg->line;
235 add_copper(COPPER_VWAIT(chg->line));
236 }
237 add_copper(COPPER_MOVE(REGN_COLOR(chg->entry >> 12), chg->entry & 0xfff));
238 }
239 #endif
240 add_copper(COPPER_END);
241 }