amiga_cyberspace
view src/main.c @ 2:b98fa9b135ea
first version which looks good
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 27 Jul 2017 02:34:42 +0300 |
parents | b5d609c7161d |
children | 200e512488e4 |
line source
1 #include <stdio.h>
2 #include <proto/exec.h>
3 #include <exec/memory.h>
4 #include "inttypes.h"
5 #include "copper.h"
6 #include "mouse.h"
7 #include "hwregs.h"
9 #define WIDTH 480
10 #define HEIGHT 128
12 #define BPLSIZE (WIDTH / 8 * HEIGHT)
13 #define MAX_HSCROLL (WIDTH - 320)
14 #define HMOD (MAX_HSCROLL / 8)
16 static uint16_t prev_intena, prev_intreq, prev_adkcon, prev_dmacon;
18 extern uint32_t backdrop;
19 extern uint32_t grid;
21 static uint32_t backdrop_addr = (uint32_t)&backdrop;
22 static uint32_t grid_addr = (uint32_t)&grid;
24 static uint16_t *grid_pal;
26 #define MAX_GRID_SHADES 6
27 static uint32_t *cplist_pal[MAX_GRID_SHADES];
29 #define MAX_SKY_SHADES 32
30 static uint16_t sky_color[] = {
31 0x204,
32 0x306,
33 0x307,
34 0x408, 0x408,
35 0x509,
36 0x50a,
37 0x60b, 0x60b,
38 0x60c, 0x60c,
39 0x70c, 0x70c,
40 0x80c,
41 0x80d,
42 0x90d, 0x90d,
43 0xa0d,
44 0xb0c, 0xb0c, 0xb0c,
45 0xc0c, 0xc0c,
46 0xd0c, 0xd0c,
47 0xd0b, 0xd0b, 0xd0b, 0xd0b,
48 0xe0a, 0xe0a, 0xe0a
49 };
51 int init(void);
52 void cleanup(void);
53 void wait_vpos(int x);
54 void wait_vblank(void);
56 int main(void)
57 {
58 int i, j, x, y;
59 long frameno = 0;
60 uint16_t tmpcol;
61 uint16_t *cplptr;
63 if(init() == -1) {
64 return 1;
65 }
67 while(!mouse_state(&x, &y)) {
68 wait_vblank();
70 if((frameno++ & 1) == 0) {
71 tmpcol = grid_pal[7];
72 for(i=7; i>2; i--) {
73 grid_pal[i] = grid_pal[i - 1];
74 }
75 grid_pal[2] = tmpcol;
76 }
78 for(i=0; i<MAX_GRID_SHADES; i++) {
79 cplptr = (uint16_t*)cplist_pal[i];
80 for(j=1; j<8; j++) {
81 /*uint16_t foo = 15 * (i + 1) / MAX_GRID_SHADES;
82 cplptr[1] = (foo << 8) | (foo << 4) | foo;*/
83 uint16_t r = ((grid_pal[j] & 0xf00) >> 8) * (i + 1) / MAX_GRID_SHADES;
84 uint16_t g = ((grid_pal[j] & 0xf0) >> 4) * (i + 1) / MAX_GRID_SHADES;
85 uint16_t b = (grid_pal[j] & 0xf) * (i + 1) / MAX_GRID_SHADES;
87 cplptr[1] = (r << 8) | (g << 4) | b;
88 cplptr += 2;
89 }
90 }
91 }
93 cleanup();
94 return 0;
95 }
97 int init(void)
98 {
99 int i, j, x, y, bit;
100 unsigned char *fbptr;
101 unsigned char tmp;
102 uint32_t grid_bpl0_addr, grid_bpl1_addr, grid_bpl2_addr;
104 Forbid();
106 /* initialize copper */
107 if(init_copper(0, 0) == -1) {
108 return -1;
109 }
111 prev_dmacon = REG_DMACONR;
112 REG_DMACON = CLRBITS(DMA_ALL);
114 prev_intena = REG_INTENAR;
115 REG_INTENA = SETBITS(INTEN_ALL);
117 prev_intreq = REG_INTREQR;
118 prev_adkcon = REG_ADKCONR;
120 REG_BPLCON0 = BPLCON0_COUNT(1) | BPLCON0_COLOR;
121 REG_BPLCON1 = 0; /* h-scroll */
122 REG_BPL1MOD = HMOD;
123 REG_BPL2MOD = HMOD;
124 REG_DIWSTART = 0x2981;
125 REG_DIWSTOP = 0x29c1;
126 REG_DDFSTART = 0x38;
127 REG_DDFSTOP = 0xd0;
129 grid_pal = (uint16_t*)(grid_addr + BPLSIZE * 3);
131 grid_bpl0_addr = grid_addr;
132 grid_bpl1_addr = grid_addr + BPLSIZE;
133 grid_bpl2_addr = grid_addr + BPLSIZE * 2;
135 for(i=0; i<8; i++) {
136 REG_COLOR_PTR[i] = grid_pal[i];
137 }
139 wait_vblank();
140 add_copper(COPPER_MOVE(REGN_BPLCON0, BPLCON0_COUNT(1) | BPLCON0_COLOR));
141 add_copper(COPPER_MOVE(REGN_BPL1PTH, backdrop_addr >> 16));
142 add_copper(COPPER_MOVE(REGN_BPL1PTL, backdrop_addr));
144 add_copper(COPPER_MOVE(REGN_COLOR1, sky_color[0]));
146 for(i=0; i<MAX_SKY_SHADES; i++) {
147 add_copper(COPPER_VWAIT(i * 128 / MAX_SKY_SHADES));
148 add_copper(COPPER_MOVE(REGN_COLOR1, sky_color[i]));
149 }
151 add_copper(COPPER_VWAIT(125));
152 add_copper(COPPER_MOVE(REGN_BPLCON0, BPLCON0_COLOR));
154 add_copper(COPPER_MOVE(REGN_BPL1PTH, grid_bpl0_addr >> 16));
155 add_copper(COPPER_MOVE(REGN_BPL1PTL, grid_bpl0_addr));
156 add_copper(COPPER_MOVE(REGN_BPL2PTH, grid_bpl1_addr >> 16));
157 add_copper(COPPER_MOVE(REGN_BPL2PTL, grid_bpl1_addr));
158 add_copper(COPPER_MOVE(REGN_BPL3PTH, grid_bpl2_addr >> 16));
159 add_copper(COPPER_MOVE(REGN_BPL3PTL, grid_bpl2_addr));
161 add_copper(COPPER_VWAIT(128));
162 add_copper(COPPER_MOVE(REGN_BPLCON0, BPLCON0_COUNT(3) | BPLCON0_COLOR));
164 add_copper(COPPER_MOVE(REGN_COLOR0, grid_pal[0]));
165 for(i=0; i<MAX_GRID_SHADES; i++) {
166 add_copper(COPPER_VWAIT(128 + i * 64 / MAX_GRID_SHADES));
167 cplist_pal[i] = copperlist_end; /* save this copperlist pointer */
168 for(j=1; j<8; j++) {
169 add_copper(COPPER_MOVE(REGN_COLOR(j), 0));
170 }
171 }
172 add_copper(COPPER_END);
174 REG_DMACON = SETBITS(DMA_BPL | DMA_COPPER | DMA_MASTER);
175 return 0;
176 }
178 void cleanup(void)
179 {
180 REG_DMACON = CLRBITS(DMA_ALL);
181 REG_DMACON = SETBITS(prev_dmacon);
183 REG_INTREQ = CLRBITS(0x7fff);
184 REG_INTREQ = SETBITS(prev_intreq);
186 REG_ADKCON = CLRBITS(0x7fff);
187 REG_ADKCON = SETBITS(prev_adkcon);
189 REG_INTENA = CLRBITS(INTEN_ALL);
190 REG_INTENA = SETBITS(prev_intena);
192 cleanup_copper();
193 Permit();
194 }
196 void wait_vpos(int x)
197 {
198 x <<= 8;
199 while((REG32_VPOSR & 0x1ff00) < x);
200 }
202 void wait_vblank(void)
203 {
204 wait_vpos(300);
205 }