amiga_imgv

annotate src/amiga/gfx.c @ 3:663471a80c21

broken + sdl emu
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Oct 2017 15:49:56 +0300
parents a4788c959918
children 0fd37effde29
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@3 60 logmsg("starting DMA\n");
nuclear@0 61 REG_DMACON = SETBITS(DMA_BPL | DMA_COPPER | DMA_MASTER);
nuclear@0 62 return 0;
nuclear@0 63 }
nuclear@0 64
nuclear@0 65 void gfx_shutdown(void)
nuclear@0 66 {
nuclear@0 67 REG_DMACON = CLRBITS(DMA_ALL);
nuclear@0 68 REG_DMACON = SETBITS(prev_dmacon);
nuclear@0 69
nuclear@0 70 REG_INTREQ = CLRBITS(0x7fff);
nuclear@0 71 REG_INTREQ = SETBITS(prev_intreq);
nuclear@0 72
nuclear@0 73 REG_ADKCON = CLRBITS(0x7fff);
nuclear@0 74 REG_ADKCON = SETBITS(prev_adkcon);
nuclear@0 75
nuclear@0 76 REG_INTENA = CLRBITS(INTEN_ALL);
nuclear@0 77 REG_INTENA = SETBITS(prev_intena);
nuclear@0 78
nuclear@0 79 if(framebuf && own_framebuf) {
nuclear@0 80 FreeMem(framebuf, fbsize);
nuclear@0 81 }
nuclear@0 82 framebuf = 0;
nuclear@0 83
nuclear@0 84 cleanup_copper();
nuclear@0 85 Permit();
nuclear@0 86 }
nuclear@0 87
nuclear@0 88 int gfx_screen_width(void)
nuclear@0 89 {
nuclear@0 90 return scr_width;
nuclear@0 91 }
nuclear@0 92
nuclear@0 93 int gfx_screen_height(void)
nuclear@0 94 {
nuclear@0 95 return scr_height;
nuclear@0 96 }
nuclear@0 97
nuclear@0 98 void *gfx_set_framebuffer(void *fb, int width, int height)
nuclear@0 99 {
nuclear@0 100 /*unsigned int bpl_scanline_sz = width / 8;*/
nuclear@0 101 unsigned long sz = width * height / 8 * num_bitplanes;
nuclear@0 102 uint32_t bpladdr;
nuclear@0 103 uint16_t bplmod;
nuclear@0 104
nuclear@0 105 if(framebuf && own_framebuf) {
nuclear@0 106 FreeMem(framebuf, fbsize);
nuclear@0 107 }
nuclear@0 108
nuclear@0 109 if(fb) {
nuclear@0 110 framebuf = fb;
nuclear@0 111 own_framebuf = 0;
nuclear@0 112 } else {
nuclear@0 113 if(!(framebuf = AllocMem(sz, MEMF_CHIP))) {
nuclear@0 114 logmsg("gfx_set_framebuffer failed to allocate %lu bytes of chip mem for framebuffer\n", sz);
nuclear@0 115 return 0;
nuclear@0 116 }
nuclear@0 117 own_framebuf = 1;
nuclear@0 118 }
nuclear@0 119
nuclear@0 120 fb_width = width;
nuclear@0 121 fb_height = height;
nuclear@0 122 fbsize = sz;
nuclear@0 123
nuclear@0 124 bpladdr = (uint32_t)framebuf;
nuclear@0 125 logmsg("bitplane address: %lx\n", (unsigned long)bpladdr);
nuclear@0 126
nuclear@0 127 bplmod = (fb_width - scr_width) / 8 + fb_width / 8 * num_bitplanes;
nuclear@0 128 REG_BPL1MOD = bplmod;
nuclear@0 129 REG_BPL2MOD = bplmod;
nuclear@0 130
nuclear@0 131 /*
nuclear@0 132 REG32_BPL1PT = bpladdr; bpladdr += bpl_scanline_sz;
nuclear@0 133 REG32_BPL2PT = bpladdr; bpladdr += bpl_scanline_sz;
nuclear@0 134 REG32_BPL3PT = bpladdr; bpladdr += bpl_scanline_sz;
nuclear@0 135 REG32_BPL4PT = bpladdr; bpladdr += bpl_scanline_sz;
nuclear@0 136 REG32_BPL5PT = bpladdr; bpladdr += bpl_scanline_sz;
nuclear@0 137 REG32_BPL6PT = bpladdr;
nuclear@0 138 */
nuclear@0 139 return framebuf;
nuclear@0 140 }
nuclear@0 141
nuclear@0 142 void *gfx_get_framebuffer(void)
nuclear@0 143 {
nuclear@0 144 return framebuf;
nuclear@0 145 }
nuclear@0 146
nuclear@0 147 int gfx_framebuffer_width(void)
nuclear@0 148 {
nuclear@0 149 return fb_width;
nuclear@0 150 }
nuclear@0 151
nuclear@0 152 int gfx_framebuffer_height(void)
nuclear@0 153 {
nuclear@0 154 return fb_height;
nuclear@0 155 }
nuclear@0 156
nuclear@0 157 void gfx_begin_copperlist(void)
nuclear@0 158 {
nuclear@0 159 static const uint16_t bplptr[] = {
nuclear@0 160 REGN_BPL1PTH, REGN_BPL2PTH, REGN_BPL3PTH, REGN_BPL4PTH, REGN_BPL5PTH, REGN_BPL6PTH
nuclear@0 161 };
nuclear@0 162 int i;
nuclear@0 163 uint32_t bpladdr;
nuclear@0 164 bpladdr = (uint32_t)framebuf;
nuclear@0 165
nuclear@0 166 for(i=0; i<num_bitplanes; i++) {
nuclear@0 167 add_copper(COPPER_MOVE(bplptr[i], bpladdr >> 16));
nuclear@0 168 add_copper(COPPER_MOVE(bplptr[i] + 2, bpladdr));
nuclear@0 169 bpladdr += fb_width / 8 * num_bitplanes;
nuclear@0 170 }
nuclear@0 171 }
nuclear@0 172
nuclear@0 173 static int mouse_bnstate(void)
nuclear@0 174 {
nuclear@0 175 return (REG_CIAA_PORTA & CIAA_PA_FIR0) ? 0 : 1;
nuclear@0 176 }
nuclear@0 177
nuclear@0 178 int gfx_next_event(union gfx_event *ev, int block)
nuclear@0 179 {
nuclear@0 180 /* TODO */
nuclear@0 181 if(block) {
nuclear@0 182 while(!mouse_bnstate());
nuclear@0 183 ev->type = GFX_EV_QUIT;
nuclear@0 184 return 1;
nuclear@0 185 }
nuclear@0 186
nuclear@0 187 if(mouse_bnstate()) {
nuclear@0 188 ev->type = GFX_EV_QUIT;
nuclear@0 189 return 1;
nuclear@0 190 }
nuclear@0 191
nuclear@0 192 return 0;
nuclear@0 193 }
nuclear@0 194
nuclear@0 195 void gfx_wait_vpos(int x)
nuclear@0 196 {
nuclear@0 197 x <<= 8;
nuclear@0 198 while((REG32_VPOSR & 0x1ff00) < x);
nuclear@0 199 }
nuclear@0 200
nuclear@0 201 void gfx_wait_vblank(void)
nuclear@0 202 {
nuclear@0 203 gfx_wait_vpos(300);
nuclear@0 204 }
nuclear@3 205
nuclear@3 206 void gfx_show_image(struct ham_image *img)
nuclear@3 207 {
nuclear@3 208 }