Skip to content

Commit f0d2b70

Browse files
committed
it works
1 parent a579ace commit f0d2b70

3 files changed

Lines changed: 268 additions & 2 deletions

File tree

convert.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
with open("./programs/hello.bin", "rb") as f:
1+
with open("./programs/mandelbrot.bin", "rb") as f:
22
data = f.read()
33

4-
with open("./programs/hello.hex", "w") as f:
4+
with open("./programs/mandelbrot.hex", "w") as f:
55
for i in range(0, len(data), 4):
66
word = data[i:i+4]
77
val = int.from_bytes(word, "little")

programs/mandelbrot.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
__attribute__((naked)) void _start(void) {
2+
__asm__ volatile(
3+
"li sp, 0x4000\n"
4+
"call main\n"
5+
"loop: j loop\n"
6+
);
7+
}
8+
9+
#define SCALE 1024
10+
#define MAX_ITER 32
11+
12+
void draw_mandelbrot(volatile unsigned char* frame, int cx, int cy, int zoom) {
13+
int x_start = cx - zoom;
14+
int y_start = cy - (zoom * 240 / 320);
15+
int x_step = (zoom * 2) / 320;
16+
int y_step = (zoom * 2 * 240 / 320) / 240;
17+
18+
if (x_step < 1) x_step = 1;
19+
if (y_step < 1) y_step = 1;
20+
21+
for (int py = 0; py < 240; py++) {
22+
int ci = y_start + py * y_step;
23+
for (int px = 0; px < 320; px++) {
24+
int cr = x_start + px * x_step;
25+
26+
int zr = 0;
27+
int zi = 0;
28+
int iter = 0;
29+
30+
while (iter < MAX_ITER) {
31+
int zr2 = (zr * zr) >> 10;
32+
int zi2 = (zi * zi) >> 10;
33+
34+
if (zr2 + zi2 > 4 * SCALE) break;
35+
36+
int new_zr = zr2 - zi2 + cr;
37+
zi = ((2 * zr * zi) >> 10) + ci;
38+
zr = new_zr;
39+
iter++;
40+
}
41+
42+
unsigned char color;
43+
if (iter == MAX_ITER) {
44+
color = 0x00;
45+
} else {
46+
color = (unsigned char)(iter * 7);
47+
}
48+
49+
frame[0x4000 * (320 * py + px)] = color;
50+
}
51+
}
52+
}
53+
54+
int main() {
55+
volatile unsigned char* frame = (volatile unsigned char*)0x0;
56+
57+
int cx = -768;
58+
int cy = 0;
59+
int step = 0;
60+
61+
while (1) {
62+
int zoom = 1536;
63+
for (int i = 0; i < step; i++) {
64+
zoom = (zoom * 3) / 4;
65+
}
66+
draw_mandelbrot(frame, cx, cy, zoom);
67+
step++;
68+
if (step > 8) step = 0;
69+
}
70+
}

programs/mandelbrot.hex

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
00004137
2+
268000ef
3+
0000006f
4+
00000013
5+
fa010113
6+
04112e23
7+
04812c23
8+
06010413
9+
faa42623
10+
fab42423
11+
fac42223
12+
fad42023
13+
fa842703
14+
fa042783
15+
40f707b3
16+
fcf42623
17+
fa042703
18+
00070793
19+
00479793
20+
40e787b3
21+
00479793
22+
66666737
23+
66770713
24+
02e79733
25+
40775713
26+
41f7d793
27+
40e787b3
28+
fa442703
29+
00f707b3
30+
fcf42423
31+
fa042783
32+
66666737
33+
66770713
34+
02e79733
35+
40675713
36+
41f7d793
37+
40f707b3
38+
fef42623
39+
fa042783
40+
66666737
41+
66770713
42+
02e79733
43+
40675713
44+
41f7d793
45+
40f707b3
46+
fef42423
47+
fec42783
48+
00f04663
49+
00100793
50+
fef42623
51+
fe842783
52+
00f04663
53+
00100793
54+
fef42423
55+
fe042223
56+
16c0006f
57+
fe442703
58+
fe842783
59+
02f707b3
60+
fc842703
61+
00f707b3
62+
fcf42223
63+
fe042023
64+
1340006f
65+
fe042703
66+
fec42783
67+
02f707b3
68+
fcc42703
69+
00f707b3
70+
fcf42023
71+
fc042e23
72+
fc042c23
73+
fc042a23
74+
0840006f
75+
fdc42783
76+
02f787b3
77+
40a7d793
78+
faf42e23
79+
fd842783
80+
02f787b3
81+
40a7d793
82+
faf42c23
83+
fbc42703
84+
fb842783
85+
00f70733
86+
000017b7
87+
06e7c063
88+
fbc42703
89+
fb842783
90+
40f707b3
91+
fc042703
92+
00f707b3
93+
faf42a23
94+
fdc42703
95+
fd842783
96+
02f707b3
97+
00179793
98+
40a7d793
99+
fc442703
100+
00f707b3
101+
fcf42c23
102+
fb442783
103+
fcf42e23
104+
fd442783
105+
00178793
106+
fcf42a23
107+
fd442703
108+
01f00793
109+
f6e7dce3
110+
0080006f
111+
00000013
112+
fd442703
113+
02000793
114+
00f71663
115+
fc0409a3
116+
0200006f
117+
fd442783
118+
0ff7f793
119+
00078713
120+
00070793
121+
00379793
122+
40e787b3
123+
fcf409a3
124+
fe442703
125+
00070793
126+
00279793
127+
00e787b3
128+
00679793
129+
00078713
130+
fe042783
131+
00f707b3
132+
00e79793
133+
00078713
134+
fac42783
135+
00e787b3
136+
fd344703
137+
00e78023
138+
fe042783
139+
00178793
140+
fef42023
141+
fe042703
142+
13f00793
143+
ece7d4e3
144+
fe442783
145+
00178793
146+
fef42223
147+
fe442703
148+
0ef00793
149+
e8e7d8e3
150+
00000013
151+
00000013
152+
05c12083
153+
05812403
154+
06010113
155+
00008067
156+
fd010113
157+
02112623
158+
02812423
159+
03010413
160+
fe042023
161+
d0000793
162+
fcf42e23
163+
fc042c23
164+
fe042623
165+
60000793
166+
fef42423
167+
fe042223
168+
0340006f
169+
fe842703
170+
00070793
171+
00179793
172+
00e787b3
173+
41f7d713
174+
00377713
175+
00f707b3
176+
4027d793
177+
fef42423
178+
fe442783
179+
00178793
180+
fef42223
181+
fe442703
182+
fec42783
183+
fcf744e3
184+
fe842683
185+
fd842603
186+
fdc42583
187+
fe042503
188+
d25ff0ef
189+
fec42783
190+
00178793
191+
fef42623
192+
fec42703
193+
00800793
194+
f8e7d6e3
195+
fe042623
196+
f85ff06f

0 commit comments

Comments
 (0)