main: attribute naked not working, use .s instead
[swan-dev] / src / main.c
1 /**
2  * File              : main.c
3  * Author            : Robin Krens <robin@robinkrens.nl>
4  * Date              : 09.06.2022
5  * Last Modified Date: 09.06.2022
6  * Last Modified By  : Robin Krens <robin@robinkrens.nl>
7  */
8 #include <memory.h>
9 #include <video.h>
10 #include <config.h>
11 #include <interrupt.h>
12
13 extern void * dummyS_isr;
14
15 /* sprite data */
16 unsigned char bgtile_gfx[] = {
17   0x31, 0x22, 0x31, 0x32, 0x22, 0x31, 0x23, 0x21, 0x13, 0x13, 0x31, 0x32,
18   0x32, 0x31, 0x23, 0x12, 0x21, 0x32, 0x13, 0x23, 0x23, 0x13, 0x31, 0x31,
19   0x12, 0x32, 0x13, 0x22, 0x23, 0x13, 0x22, 0x13
20 };
21
22 /* palette data */
23 unsigned char bgtile_pal[] = {
24   0x65, 0x06, 0x67, 0x06, 0x79, 0x07, 0x8c, 0x08, 0xff, 0x0f, 0xff, 0x0f,
25   0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f,
26   0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f
27 };
28
29
30 /* data */
31 unsigned char lol[5] = { 0xAA, 0x2, 0x3, 0x4, 0x5};
32 unsigned char lol2[5] = { 0x1, 0x2, 0x3, 0x4, 0x5};
33 unsigned char lol3[5] = { 0x1, 0x2, 0x3, 0x4, 0x5};
34 #define PALETTE_T (volatile unsigned *)0xFE00
35 #define SPRITE_T (volatile unsigned *)0x4000
36
37 /* bss */
38 static unsigned char blaat[100];
39
40 void outport(unsigned char portnr, unsigned char val)
41 {
42         __asm__(
43                 "mov %1, %%dl\n\t"
44                 "out %0, (%%dx)"
45                 :
46                 :"r"(val), "r"(portnr)
47                 :);
48 }
49
50 unsigned char inport(unsigned char portnr)
51 {
52         unsigned char val;
53         __asm__(
54                 "mov %1, %%dl\n\t"
55                 "in (%%dx), %%al\n\t"
56                 "mov %%al, %0"
57                 : "=r"(val)
58                 : "r"(portnr)
59                 :);
60
61         return val;
62 }
63
64 static void __attribute__((used)) dummy() 
65 {
66         __asm__("iret");
67 }
68
69 //__attribute__((naked)) void dummy_isr(void)
70 //{
71 //      __asm__("push %ax\n\t"
72 //              "push %bx\n\t"
73 //              "popw %bp\n\t" /* HACK: directly pop bp again */
74 //             );
75 //
76 //      //unsigned char val = inport(IO_BG_X);
77 //      //val++;
78 //
79 //      //outport(IO_BG_X, val);
80 //      //outport(IO_INT_ACK, INT_VBLANK_START);
81 //      __asm__("pop %ax\n\t"
82 //              "pop %bx\n\t"
83 //             );
84 //
85 //      __asm__("iret");
86 //}
87
88 void setup_ivec(void)
89 {
90         outport(IO_INT_BASE, INT_BASE);
91
92         unsigned short * test_vec = (INT_BASE + INTVEC_VBLANK_START) << 2;
93         *test_vec++ = (&dummyS_isr);
94         *test_vec = (0x2000);
95
96         unsigned short * keyboard_intr = (INT_BASE + INTVEC_KEY_PRESS) << 2;
97         *keyboard_intr++ = (&dummyS_isr);
98         *keyboard_intr = (0x2000);
99
100         outport(IO_INT_ENABLE, INT_VBLANK_START);
101         //outport(IO_INT_ENABLE, INT_KEY_PRESS);
102         __asm__("sti");
103 }
104
105 static unsigned cnt = 0;
106
107 void scroll_video(void)
108 {
109         unsigned char val = inport(IO_FG_X);
110
111         cnt++;
112         if (cnt > 10) {
113                 cnt = 0;
114                 val++;
115         }
116         
117         outport(IO_FG_X, val);
118         outport(IO_INT_ACK, INT_VBLANK_START);
119         
120         outport(IO_FG_WIN_X0, cnt);
121         outport(IO_FG_WIN_Y0, cnt);
122         outport(IO_FG_WIN_X1, SCREEN_WIDTH - cnt);
123         outport(IO_FG_WIN_Y1, SCREEN_HEIGHT - cnt);
124
125 }
126
127 void init_video(void)
128 {
129 }
130
131 int main(void)
132 {
133         outport(0x15, 0x99);
134         unsigned char ret = inport(0x15);
135         
136         lol[0] = 0xAA;
137         lol[1] = ret;
138
139         blaat[0] = 0xEE;
140         blaat[1] = 0xEE;
141         blaat[32] = 0xEE;
142
143
144         setup_ivec();
145         init_video();
146         outport(IO_VIDEO_MODE, VMODE_16C_CHK | VMODE_CLEANINIT);
147         unsigned short base = 0x3000; /* screens pos */
148         unsigned short sprite_pos = base - (MAP_SIZE*2) - SPR_TABLE_SIZE;
149         unsigned short scr1_pos = base - (MAP_SIZE*2); /* 0x2000 */
150         unsigned short scr2_pos = base - MAP_SIZE; /* 0x2800 */
151         outport(IO_MAP_BASE, FG_MAP(scr2_pos) | BG_MAP(scr1_pos));
152         outport(IO_SPR_TABLE, SPR_TABLE(sprite_pos));
153
154         outport(IO_FG_WIN_X0, 10);
155         outport(IO_FG_WIN_Y0, 10);
156         outport(IO_FG_WIN_X1, SCREEN_WIDTH - 10);
157         outport(IO_FG_WIN_Y1, SCREEN_HEIGHT - 10);
158
159         /* icons */
160         outport(0x15, 0xEE);
161
162         unsigned char * ptr = PALETTE_T;
163         for (int i = 0; i < sizeof(bgtile_pal); ++i) {
164                 *ptr++ = bgtile_pal[i];
165         }
166         
167         ptr = SPRITE_T;
168         for (int i = 0; i < sizeof(bgtile_gfx); ++i) {
169                 *ptr++ = bgtile_gfx[i];
170         }
171         
172         outport(IO_DISPLAY_CTRL, FG_OUT_WIN | FG_ON);
173
174         int a = 0;
175         a++;
176
177         while(1) {
178         }
179
180         return 0;
181 }