forked from eliben/pycparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathholygrail.py
158 lines (147 loc) · 7.62 KB
/
holygrail.py
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
VGA_S = r'''
# PCI is at 0x30000000
# VGA is at 00:01.0, using extended control regs (4096 bytes)
# TODO: Scan for Vendor ID / Product ID
la t0, 0x30000000|(1<<15)|(0<<12)
# Set up frame buffer
la t1, 0x50000008
sw t1, 0x10(t0)
# Set up I/O
la t2, 0x40000000
sw t2, 0x18(t0)
# Enable memory accesses for this device
lw a0, 0x04(t0)
ori a0, a0, 0x02
sw a0, 0x04(t0)
lw a0, 0x04(t0)
# Set up video mode somehow
li t3, 0x60 # Enable LFB, enable 8-bit DAC
sh t3, 0x508(t2)
# Set Mode 13h by hand
la a0, mode_13h_regs
addi a1, t2, 0x400-0xC0
la t3, 0xC0
1:
# Grab address
lbu a3, 0(a0)
beq a3, zero, 2f
add a2, a1, a3
# Grab index and data
lb a4, 1(a0)
lbu a5, 2(a0)
# Advance a0
addi a0, a0, 3
# If this is for the attribute controller, treat it specially.
blt a3, t3, 3f
# If this is an external register, also treat it specially.
blt a4, zero, 4f
# Normal case
sb a4, 0(a2)
sb a5, 1(a2)
j 1b
3:
# The attribute controller is a special case
lb zero, 0xDA(a1)
sb a4, 0(a2)
sb a5, 0(a2)
j 1b
4:
# External registers are also special but not as special as the attribute controller
sb a5, 0(a2)
j 1b
2:
# Set up a palette
li t3, 0
sb t3, 0x408(t2)
li t3, 0
li t4, 256*3
la a0, initial_palette
1:
lb t5, 0(a0)
sb t5, 0x409(t2)
addi a0, a0, 1
addi t3, t3, 1
bltu t3, t4, 1b
'''
START_VGA_S = f'''
.text
.global _start
_start:
bne a0, x0, _start # loop if hartid is not 0
li sp, 0x80200000 # setup stack pointer
{VGA_S}
j firmware_main # jump to c entry
'''
MODE_13H = '''
mode_13h_regs:
# Miscellaneous Output Register:
# Just a single port. But bit 0 determines whether we use 3Dx or 3Bx.
# So we need to set this early.
.byte 0xC2, 0xFF, 0x63
# Sequencer:
# Disable reset here.
.byte 0xC4, 0x00, 0x00
# Attributes:
# - Read 3DA to reset flip-flop
# - Write 3C0 for address
# - Write 3C0 for data
.byte 0xC0, 0x00, 0x00
.byte 0xC0, 0x01, 0x02
.byte 0xC0, 0x02, 0x08
.byte 0xC0, 0x03, 0x0A
.byte 0xC0, 0x04, 0x20
.byte 0xC0, 0x05, 0x22
.byte 0xC0, 0x06, 0x28
.byte 0xC0, 0x07, 0x2A
.byte 0xC0, 0x08, 0x15
.byte 0xC0, 0x09, 0x17
.byte 0xC0, 0x0A, 0x1D
.byte 0xC0, 0x0B, 0x1F
.byte 0xC0, 0x0C, 0x35
.byte 0xC0, 0x0D, 0x37
.byte 0xC0, 0x0E, 0x3D
.byte 0xC0, 0x0F, 0x3F
.byte 0xC0, 0x30, 0x41
.byte 0xC0, 0x31, 0x00
.byte 0xC0, 0x32, 0x0F
.byte 0xC0, 0x33, 0x00
.byte 0xC0, 0x34, 0x00
# Graphics Mode
.byte 0xCE, 0x00, 0x00
.byte 0xCE, 0x01, 0x00
.byte 0xCE, 0x02, 0x00
.byte 0xCE, 0x03, 0x00
.byte 0xCE, 0x04, 0x00
.byte 0xCE, 0x05, 0x40
.byte 0xCE, 0x06, 0x05
.byte 0xCE, 0x07, 0x00
.byte 0xCE, 0x08, 0xFF
# CRTC
.byte 0xD4, 0x11, 0x0E # Do this to unprotect the registers
.byte 0xD4, 0x00, 0x5F
.byte 0xD4, 0x01, 0x4F
.byte 0xD4, 0x02, 0x50
.byte 0xD4, 0x03, 0x82
.byte 0xD4, 0x04, 0x54
.byte 0xD4, 0x05, 0x80
.byte 0xD4, 0x06, 0xBF
.byte 0xD4, 0x07, 0x1F
.byte 0xD4, 0x08, 0x00
.byte 0xD4, 0x09, 0x41
.byte 0xD4, 0x0A, 0x20
.byte 0xD4, 0x0B, 0x1F
.byte 0xD4, 0x0C, 0x00
.byte 0xD4, 0x0D, 0x00
.byte 0xD4, 0x0E, 0xFF
.byte 0xD4, 0x0F, 0xFF
.byte 0xD4, 0x10, 0x9C
.byte 0xD4, 0x11, 0x8E # Registers are now reprotected
.byte 0xD4, 0x12, 0x8F, 0xD4, 0x13, 0x28, 0xD4, 0x14, 0x40, 0xD4, 0x15, 0x96, 0xD4, 0x16, 0xB9, 0xD4, 0x17, 0xA3
'''
vga_pal = [0x00, 0x00, 0x00,0x00, 0x00, 0x55,0x00, 0x00, 0xAA,0x00, 0x00, 0xFF,0x00, 0x55, 0x00,0x00, 0x55, 0x55,0x00, 0x55, 0xAA,0x00, 0x55, 0xFF,0x00, 0xAA, 0x00,0x00, 0xAA, 0x55,0x00, 0xAA, 0xAA,0x00, 0xAA, 0xFF,0x00, 0xFF, 0x00,0x00, 0xFF, 0x55,0x00, 0xFF, 0xAA,0x00, 0xFF, 0xFF,0x55, 0x00, 0x00,0x55, 0x00, 0x55,0x55, 0x00, 0xAA,0x55, 0x00, 0xFF,0x55, 0x55, 0x00,0x55, 0x55, 0x55,0x55, 0x55, 0xAA,0x55, 0x55, 0xFF,0x55, 0xAA, 0x00,0x55, 0xAA, 0x55,0x55, 0xAA, 0xAA,0x55, 0xAA, 0xFF,0x55, 0xFF, 0x00,0x55, 0xFF, 0x55,0x55, 0xFF, 0xAA,0x55, 0xFF, 0xFF,0xAA, 0x00, 0x00,0xAA, 0x00, 0x55,0xAA, 0x00, 0xAA,0xAA, 0x00, 0xFF,0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A, 0xAA, 0xAA,0xAA, 0xAA, 0xFF,0xAA, 0xFF, 0x00,0xAA, 0xFF, 0x55,0xAA, 0xFF, 0xAA,0xAA, 0xFF, 0xFF,0xFF, 0x00, 0x00,0xFF, 0x00, 0x55,0xFF, 0x00, 0xAA,0xFF, 0x00, 0xFF,0xFF, 0x55, 0x00,0xFF, 0x55, 0x55,0xFF, 0x55, 0xAA,0xFF, 0x55, 0xFF,0xFF, 0xAA, 0x00,0xFF, 0xAA, 0x55,0xFF, 0xAA, 0xAA,0xFF, 0xAA, 0xFF,0xFF, 0xFF, 0x00,0xFF, 0xFF, 0x55,0xFF, 0xFF, 0xAA,0xFF, 0xFF, 0xFF]
def gen_pal():
asm = [
'initial_palette:',
' .byte %s' % ','.join( str(b) for b in vga_pal)
]
return '\n'.join(asm)