amiga_imgv

view src/amiga/gfx.c @ 0:a4788c959918

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 25 Oct 2017 19:34:53 +0300
parents
children 663471a80c21
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"
8 static int scr_width, scr_height;
9 static int fb_width, fb_height;
10 static int num_bitplanes;
11 static uint16_t prev_intena, prev_intreq, prev_adkcon, prev_dmacon;
13 static unsigned char *framebuf;
14 static unsigned long fbsize;
15 static int own_framebuf;
17 int gfx_init(int nbpl, unsigned int flags)
18 {
19 uint16_t bplcon0;
21 num_bitplanes = nbpl;
22 scr_width = fb_width = (flags & GFX_HIRES) ? 640 : 320;
23 scr_height = fb_height = (flags & GFX_ILACE) ? 512 : 256;
25 Forbid();
27 prev_dmacon = REG_DMACONR;
28 REG_DMACON = CLRBITS(DMA_ALL);
30 prev_intena = REG_INTENAR;
31 prev_intreq = REG_INTREQR;
32 prev_adkcon = REG_ADKCONR;
34 if(init_copper(0, 0) == -1) {
35 return -1;
36 }
38 if(!gfx_set_framebuffer(0, scr_width, scr_height)) {
39 gfx_shutdown();
40 return -1;
41 }
43 bplcon0 = BPLCON0_COUNT(nbpl) | BPLCON0_COLOR;
44 if(flags & GFX_HIRES) bplcon0 |= BPLCON0_HIRES;
45 if(flags & GFX_ILACE) bplcon0 |= BPLCON0_LACE;
46 if(flags & GFX_HAM) bplcon0 |= BPLCON0_HOMOD;
47 if(flags & GFX_DBLPF) bplcon0 |= BPLCON0_DBLPF;
49 REG_BPLCON0 = bplcon0;
50 REG_BPLCON1 = 0; /* h-scroll */
51 REG_DIWSTART = 0x2c81; /* 81h horiz start, 2ch vertical start */
52 REG_DIWSTOP = 0x2cc1;
53 REG_DDFSTART = (flags & GFX_HIRES) ? 0x3c : 0x38;
54 REG_DDFSTOP = (flags & GFX_HIRES) ? 0xd4 : 0xd0;
56 gfx_wait_vblank();
57 gfx_begin_copperlist();
58 add_copper(COPPER_END);
60 REG_DMACON = SETBITS(DMA_BPL | DMA_COPPER | DMA_MASTER);
61 return 0;
62 }
64 void gfx_shutdown(void)
65 {
66 REG_DMACON = CLRBITS(DMA_ALL);
67 REG_DMACON = SETBITS(prev_dmacon);
69 REG_INTREQ = CLRBITS(0x7fff);
70 REG_INTREQ = SETBITS(prev_intreq);
72 REG_ADKCON = CLRBITS(0x7fff);
73 REG_ADKCON = SETBITS(prev_adkcon);
75 REG_INTENA = CLRBITS(INTEN_ALL);
76 REG_INTENA = SETBITS(prev_intena);
78 if(framebuf && own_framebuf) {
79 FreeMem(framebuf, fbsize);
80 }
81 framebuf = 0;
83 cleanup_copper();
84 Permit();
85 }
87 int gfx_screen_width(void)
88 {
89 return scr_width;
90 }
92 int gfx_screen_height(void)
93 {
94 return scr_height;
95 }
97 void *gfx_set_framebuffer(void *fb, int width, int height)
98 {
99 /*unsigned int bpl_scanline_sz = width / 8;*/
100 unsigned long sz = width * height / 8 * num_bitplanes;
101 uint32_t bpladdr;
102 uint16_t bplmod;
104 if(framebuf && own_framebuf) {
105 FreeMem(framebuf, fbsize);
106 }
108 if(fb) {
109 framebuf = fb;
110 own_framebuf = 0;
111 } else {
112 if(!(framebuf = AllocMem(sz, MEMF_CHIP))) {
113 logmsg("gfx_set_framebuffer failed to allocate %lu bytes of chip mem for framebuffer\n", sz);
114 return 0;
115 }
116 own_framebuf = 1;
117 }
119 fb_width = width;
120 fb_height = height;
121 fbsize = sz;
123 bpladdr = (uint32_t)framebuf;
124 logmsg("bitplane address: %lx\n", (unsigned long)bpladdr);
126 bplmod = (fb_width - scr_width) / 8 + fb_width / 8 * num_bitplanes;
127 REG_BPL1MOD = bplmod;
128 REG_BPL2MOD = bplmod;
130 /*
131 REG32_BPL1PT = bpladdr; bpladdr += bpl_scanline_sz;
132 REG32_BPL2PT = bpladdr; bpladdr += bpl_scanline_sz;
133 REG32_BPL3PT = bpladdr; bpladdr += bpl_scanline_sz;
134 REG32_BPL4PT = bpladdr; bpladdr += bpl_scanline_sz;
135 REG32_BPL5PT = bpladdr; bpladdr += bpl_scanline_sz;
136 REG32_BPL6PT = bpladdr;
137 */
138 return framebuf;
139 }
141 void *gfx_get_framebuffer(void)
142 {
143 return framebuf;
144 }
146 int gfx_framebuffer_width(void)
147 {
148 return fb_width;
149 }
151 int gfx_framebuffer_height(void)
152 {
153 return fb_height;
154 }
156 void gfx_begin_copperlist(void)
157 {
158 static const uint16_t bplptr[] = {
159 REGN_BPL1PTH, REGN_BPL2PTH, REGN_BPL3PTH, REGN_BPL4PTH, REGN_BPL5PTH, REGN_BPL6PTH
160 };
161 int i;
162 uint32_t bpladdr;
163 bpladdr = (uint32_t)framebuf;
165 for(i=0; i<num_bitplanes; i++) {
166 add_copper(COPPER_MOVE(bplptr[i], bpladdr >> 16));
167 add_copper(COPPER_MOVE(bplptr[i] + 2, bpladdr));
168 bpladdr += fb_width / 8 * num_bitplanes;
169 }
170 }
172 static int mouse_bnstate(void)
173 {
174 return (REG_CIAA_PORTA & CIAA_PA_FIR0) ? 0 : 1;
175 }
177 int gfx_next_event(union gfx_event *ev, int block)
178 {
179 /* TODO */
180 if(block) {
181 while(!mouse_bnstate());
182 ev->type = GFX_EV_QUIT;
183 return 1;
184 }
186 if(mouse_bnstate()) {
187 ev->type = GFX_EV_QUIT;
188 return 1;
189 }
191 return 0;
192 }
194 void gfx_wait_vpos(int x)
195 {
196 x <<= 8;
197 while((REG32_VPOSR & 0x1ff00) < x);
198 }
200 void gfx_wait_vblank(void)
201 {
202 gfx_wait_vpos(300);
203 }