summaryrefslogtreecommitdiffstats
path: root/data/sysrom/sysstart.c
blob: 02ad8475239d46238707c574cf7d0743c34e43ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <ioreg.h>
#include <sys/cpu.h>
#include "sysrom.h"
#include "keyboard.h"

/* We don't trust printf yet for conversion to ASCII */
static char *tohex(uint32_t v)
{
  static const char hexdigits[16] = "0123456789abcdef";
  static char outbuf[9];
  int i;

  for (i = 0; i < 8; i++) {
    v = (v << 4) + (v >> 28);	/* Rotate */
    outbuf[i] = hexdigits[v & 15];
  }

  /* outbuf[8] is always NUL */
  return outbuf;
}

__noreturn __unknown_trap(uint32_t trap_status, uint32_t pc)
{
  fputs(">>> Trap ", stdout);
  fputs(tohex(trap_status), stdout);
  fputs(" received @ ", stdout);
  fputs(tohex(pc), stdout);
  fputs("\n", stdout);
  die();
}

__noreturn die(void)
{
  for (;;)
    ;
}

uint32_t spurious_ctr, vsync_ctr, sec_ctr;
uint16_t subsec_ctr;

static void __attribute__((interrupt)) vsync_irq(void)
{
  static uint8_t flash_ctr = 28; /* 400 ms * 70 Hz */
  vsync_ctr++;

  if (!--flash_ctr) {
    *IO_VIDEO_DACXOR_HI ^= 1;
    flash_ctr = 28;
  }
}

static const uint8_t ascii2led[] = {
  [' '] = 0b00000000,
  ['\"']= 0b00010001,
  ['\'']= 0b00000001,
  ['('] = 0b00111001,
  [')'] = 0b00001111,
  [','] = 0b00000010,
  ['-'] = 0b01000000,
  ['/'] = 0b01001001,
  ['0'] = 0b00111111,
  ['1'] = 0b00000110,
  ['2'] = 0b01011011,
  ['3'] = 0b01001111,
  ['4'] = 0b01100110,
  ['5'] = 0b01101101,
  ['6'] = 0b01111101,
  ['7'] = 0b00000111,
  ['8'] = 0b01111111,
  ['9'] = 0b01101111,
  ['='] = 0b01000100,
  ['A'] = 0b01110111,
  ['C'] = 0b00111001,
  ['E'] = 0b01111001,
  ['F'] = 0b01110001,
  ['G'] = 0b01111101,
  ['H'] = 0b01110110,
  ['I'] = 0b00110000,
  ['J'] = 0b00011110,
  ['L'] = 0b00111000,
  ['O'] = 0b00111111,
  ['P'] = 0b01110011,
  ['S'] = 0b01101101,
  ['U'] = 0b00111110,
  ['V'] = 0b00111110,
  ['X'] = 0b01110110,
  ['Z'] = 0b01011011,
  ['\\']= 0b01010010,
  ['^'] = 0b00110001,
  ['_'] = 0b00000100,
  ['`'] = 0b00100000,
  ['a'] = 0b01011111,
  ['b'] = 0b01111100,
  ['c'] = 0b01011000,
  ['d'] = 0b01011110,
  ['e'] = 0b01111011,
  ['f'] = 0b01110001,
  ['g'] = 0b01101111,
  ['h'] = 0b01110100,
  ['i'] = 0b00000100,
  ['j'] = 0b00001110,
  ['l'] = 0b00110000,
  ['n'] = 0b01010100,
  ['o'] = 0b01011100,
  ['p'] = 0b01110011,
  ['q'] = 0b01100111,
  ['r'] = 0b01010000,
  ['s'] = 0b01101101,
  ['t'] = 0b01111000,
  ['u'] = 0b00011100,
  ['y'] = 0b01101110,
  ['z'] = 0b01011011,
  ['|'] = 0b00110000,
};

static void __attribute__((interrupt)) timer_irq(void)
{
  static uint16_t sec_ctr_10k;
  static union {
    char buf[8];
    uint32_t val;
  } led;
  char *p;
  int i;

  subsec_ctr++;

  if (subsec_ctr >= 64) {
    subsec_ctr = 0;
    sec_ctr++;
    sec_ctr_10k++;

    if (sec_ctr_10k >= 10000)
      sec_ctr_10k = 0;

    snprintf(led.buf, 5, "%4u", sec_ctr_10k);

    p = led.buf;
    for (i = 0; i < 4; i++) {
      *p = ascii2led[(uint8_t)*p];
      p++;
    }

    writel(led.val, IO_SYS_7SEG);
  }

  sdcard_timer_tick();
}

void __attribute__((interrupt)) __spurious_irq(void)
{
  spurious_ctr++;
}

static void irq_init(void)
{
  __ivt[0x4e] = vsync_irq;	/* GPIP6 = vertical retrace */
  __ivt[0x4d] = timer_irq;	/* Timer A = clock */

  /* Disable and mask all interrupts */
  writeb(0x00, IO_MFP_IERA);
  writeb(0x00, IO_MFP_IERB);
  writeb(0x00, IO_MFP_IMRA);
  writeb(0x00, IO_MFP_IMRB);
  writeb(0x00, IO_MFP_ISRA);
  writeb(0x00, IO_MFP_ISRB);

  /* Vector register: base 0x40 */
  writeb(0x40, IO_MFP_VR);

  /* GPIOs configured as inputs, trigger on the negative edge */
  writeb(0x00, IO_MFP_AER);
  writeb(0x00, IO_MFP_DDR);

  /*
   * Use Timer A to generate a 64 Hz timer interrupt;
   * the input clock is 2.457600 MHz; the total frequency is
   * 2457600/(prescale*counter)
   */
  writeb(0x07, IO_MFP_TACR);	/* Prescaler divide by 200 */
  writeb(0xc0, IO_MFP_TADR);	/* Main counter divide by 192 */

  /* Actually enable and unmask interrupts */
  writeb(0x60, IO_MFP_IERA);
  writeb(0x60, IO_MFP_IMRA);

  /* Enable interrupts in the CPU */
  cpu_set_spl(0);
}

uint8_t __framebuf[2*480*640] __attribute__((aligned(32)));

static void picture(void)
{
  volatile uint8_t *ptr, *aptr, *bptr;
  unsigned int x, y, i, j;
  size_t p;

  ptr = __framebuf;
  printf("Initializing frame buffer image @ %p\n", ptr);
  for (y = 0; y < 480; y++)
    for (x = 0; x < 640; x++)
      *ptr++ = x+y;

  ptr = __framebuf;
  printf("Reading back framebuffer image @ %p\n", ptr);
  for (y = 0; y < 480; y++) {
    for (x = 0; x < 640; x++) {
      uint8_t z = x+y;
      uint8_t v = *ptr;
      uint8_t w = *ptr;
      if (v != z || v != w)
	printf("Error @ %p wrote %02x read %02x %02x\n", ptr, z, v, w);
      ptr++;
    }
  }

  printf("Drawing test frame picture at %p\n", ptr);
  writel(((size_t)__framebuf + 640*480) << 3, IO_VIDEO_ROW0START);
  memset((void *)ptr, 0, 256000);	/* Clear picture 1 */
  for (i = 0; i < 120; i++) {
    unsigned int ofs = i*2;
    unsigned int col = i+63;
    aptr = &ptr[ofs*641];
    bptr = &ptr[(479*640)-ofs*639];
    memset((void *)aptr, col, 640-2*ofs);
    memset((void *)bptr, col, 640-2*ofs);

    aptr = &ptr[ofs*641+640];
    bptr = &ptr[639+ofs*639+640];
    for (y = ofs+1; y <= 478-ofs; y++) {
      *aptr = col;  aptr += 640;
      *bptr = col;  bptr += 640;
    }
  }

  printf("Soft-scrolling the picture...\n");
  /* Soft-scroll the new picture in */
  p = ((size_t)__framebuf) << 3;
  for (i = 0; i <= 480; i++) {
    writel(p, IO_VIDEO_ROW0START);
    p += (640 << 3);

    for (j = 0; j < 10000; j++)
      asm volatile("" : : "r" (j));
  }
}

void sys_start(void)
{
  printf("Hello C code!\n");
  printf("Hello C code again, 2^8 = %d\n", 256);

  irq_init();

  picture();

  //disk_boot();

  syscon_init();
  keyboard_init();

  while (1) {
    uint32_t c;
    uint8_t cb;

    do {
      c = get_key();
    } while (c == -1U);
    cb = c;

    syscon_write(1, &cb, 1);
  }

  monitor();
  die();
}