amiga_imgv

annotate src/amiga/gfx.c @ 1:6320a20f17c5

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