-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathball.c
394 lines (336 loc) · 13.8 KB
/
ball.c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
ball.c for Simulation on RISC-V
Copyright (C) 2024 Daniele Giovanardi daniele.giovanardi@madenetwork.it
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/*
On a Debian-based Linux distribution install Clang:
sudo apt install -y clang
Then compile ball.c with Clang:
clang --target=riscv32 -march=rv32i -static -S -fno-addrsig ball.c
DON'T FORGET TO CUT THE FOLLOWING LINES FROM ball.s (in .text section):
.attribute 4, 16
.attribute 5, "rv32i2p0"
Parameters for assembler compiler:
(https://riscvasm.lucasteske.dev/#)
__heap_size = 0x200;
__stack_size = 0x800;
MEMORY
{
ROM (rwx) : ORIGIN = 0x00000000, LENGTH = 0x01000
RAM (rwx) : ORIGIN = 0x00001000, LENGTH = 0x01000
}
Parameters for debugger:
- set memory size to 2000 (hex)
- set program counter to 0
- set stack pointer to 1A40 where:
1000h (RAM origin)
+ 40h (variables)
+ 200h (heap size)
+ 800h (stack size)
Video controller port:
- 0x1b00 (short) If not 0 => Ball pos to be updated
- 0x1b02 (short) Ball left position
- 0x1b04 (short) Ball top position
*/
//---------------------------------------------------------------------------
// Constants (set in Init() function)
//---------------------------------------------------------------------------
unsigned int LcgState; // Random number generator seed
int kScale; // Float-point emulation: bits to shift
int kSpeedGear; // Movement speed
int kBallRadius; // Ball radius (on view port)
int kViewPortW; // View port width
int kViewPortH; // View port height
int kFieldW; // View port width padding to prevent ball cropping (i.e. ball movement range)
int kFieldH; // View port height padding
int kSegmentPad[3]; // (.data segment padding for stack pointer alignment)
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Variables
//---------------------------------------------------------------------------
enum TSide : int { sideTop=0, sideRight, sideBottom, sideLeft };
int ReboundPoint; // Side coordinate (X or Y) where ball rebound
int ReboundSide; // Side where ball rebound
int scBallPosX; // Center of the ball (emulated float precision)
int scBallPosY; // Center of the ball (emulated float precision)
int scBallDelta; // X increment for every Y step on sides Left/Right
// Y increment for every X step on sides Top/Bottom
// (emulated float precision)
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Functions declaration
//---------------------------------------------------------------------------
// Float-point emulation
int ScaleUp (int AIntValue); // Integer to be right shifted
int ScaleDown(int AScaledValue); // Scaled value to be left shifted
int ScaleDrop(int AScaledValue); // Drop "decimal" part of scaled value
void Init(); // Init constants and initial var values
void Tick(); // Move ball
void NewRebound(); // Calculate new rebound target and delta (X or Y) on path
int RandomX(); // Generate new target on X axis (top/bottom side)
int RandomY(); // Generate new target on X axis (top/bottom side)
unsigned int LcgRandom(unsigned int *state); // Random number generator
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Implementation
//---------------------------------------------------------------------------
int main()
{
short *pToBeUpdated = (short *)0x1b00;
short *pBallLeft = (short *)0x1b02;
short *pBallTop = (short *)0x1b04;
Init();
while(1)
{
Tick();
*pToBeUpdated = 1;
*pBallLeft = ScaleDown(scBallPosX);
*pBallTop = ScaleDown(scBallPosY);
}
}
//---------------------------------------------------------------------------
int ScaleUp(int AIntValue)
{
return AIntValue << kScale;
}
//---------------------------------------------------------------------------
int ScaleDown(int AScaledValue)
{
return AScaledValue >> kScale;
}
//---------------------------------------------------------------------------
int ScaleDrop(int AScaledValue)
{
return (AScaledValue >> kScale) << kScale;
}
//---------------------------------------------------------------------------
void Init()
{
LcgState = 0x55AA55AA;
kSpeedGear = 2; // Not upscaled yet
kBallRadius = 10;
kViewPortW = 367;
kViewPortH = 223;
// 1-bit sign + X-bits to fit int part + (32-X)-bits as "decimal"
if( kViewPortW > 65535 ) kScale= 8;
else if( kViewPortW > 32767 ) kScale= 15;
else if( kViewPortW > 16383 ) kScale= 16;
else if( kViewPortW > 8191 ) kScale= 17;
else if( kViewPortW > 4095 ) kScale= 18;
else if( kViewPortW > 2047 ) kScale= 19;
else if( kViewPortW > 1023 ) kScale= 20;
else if( kViewPortW > 511 ) kScale= 21;
else if( kViewPortW > 255 ) kScale= 22;
else if( kViewPortW > 127 ) kScale= 23;
kFieldW = kViewPortW - (kBallRadius*2);
kFieldH = kViewPortH - (kBallRadius*2);
ReboundSide = sideLeft;
scBallPosX = 0;
scBallPosY = ScaleUp((LcgRandom(&LcgState) % (kFieldH-100)) + 50);
NewRebound();
}
//---------------------------------------------------------------------------
void Tick()
{
switch (ReboundSide)
{
case sideTop:
if (scBallPosY <= 0) // Touch!
NewRebound();
else
{
scBallPosX += scBallDelta * kSpeedGear;
scBallPosY -= ScaleUp(1) * kSpeedGear;
}
break;
case sideRight:
if (scBallPosX >= ScaleUp(kFieldW-1)) // Touch!
NewRebound();
else
{
scBallPosX += ScaleUp(1) * kSpeedGear;
scBallPosY += scBallDelta * kSpeedGear;
}
break;
case sideBottom:
if (scBallPosY >= ScaleUp((kFieldH-1))) // Touch!
NewRebound();
else
{
scBallPosX -= scBallDelta * kSpeedGear;
scBallPosY += ScaleUp(1) * kSpeedGear;
}
break;
case sideLeft:
if (scBallPosX <= 0) // Touch!
NewRebound();
else
{
scBallPosX -= ScaleUp(1) * kSpeedGear;
scBallPosY -= scBallDelta * kSpeedGear;
}
break;
}
}
//---------------------------------------------------------------------------
void NewRebound()
{
switch (ReboundSide)
{
case sideTop: // from Top to Right
ReboundSide = sideRight;
scBallPosX = ScaleDrop(scBallPosX); // Drop "decimals"
scBallPosY = 0;
ReboundPoint = RandomY(); // Random point on right side (Y)
scBallDelta = ScaleUp(ReboundPoint) / (kFieldW - ScaleDown(scBallPosX)); // Y delta
break;
case sideRight: // from Right to Bottom
ReboundSide = sideBottom;
scBallPosX = ScaleUp (kFieldW);
scBallPosY = ScaleDrop(scBallPosY); // Drop "decimals"
ReboundPoint = RandomX(); // Random point on bottom side (X)
scBallDelta = ScaleUp(ReboundPoint) / (kFieldH - ScaleDown(scBallPosY)); // X delta
break;
case sideBottom: // from Bottom to Left
ReboundSide = sideLeft;
scBallPosX = ScaleDrop(scBallPosX); // Drop "decimals"
scBallPosY = ScaleUp (kFieldH);
ReboundPoint = RandomY(); // Random point on left side (Y)
scBallDelta = ScaleUp(ReboundPoint) / ScaleDown(scBallPosX); // Y delta
break;
case sideLeft: // from Left to Top
ReboundSide = sideTop;
scBallPosX = 0;
scBallPosY = ScaleDrop(scBallPosY); // Drop "decimals"
ReboundPoint = RandomX(); // Random point on top side (X)
scBallDelta = ScaleUp(ReboundPoint) / ScaleDown(scBallPosY); // X delta
break;
}
}
//---------------------------------------------------------------------------
int RandomX()
{
return (LcgRandom(&LcgState) % (kFieldW-100)) + 50; // Not too near the edge (at least 50px far)
}
//---------------------------------------------------------------------------
int RandomY()
{
return (LcgRandom(&LcgState) % (kFieldH-100)) + 50; // Not too near the edge (at least 50px far)
}
//---------------------------------------------------------------------------
unsigned int LcgRandom(unsigned int *state)
{
// Precomputed parameters for Schrage's method
// https://en.wikipedia.org/wiki/Lehmer_random_number_generator
const unsigned int M = 0x7fffffff;
const unsigned int A = 48271;
const unsigned int Q = M / A; // 44488
const unsigned int R = M % A; // 3399
unsigned int div = *state / Q; // max: M / Q = A = 48,271
unsigned int rem = *state % Q; // max: Q - 1 = 44,487
unsigned int s = rem * A; // max: 44,487 * 48,271 = 2,147,431,977 = 0x7fff3629
unsigned int t = div * R; // max: 48,271 * 3,399 = 164,073,129
unsigned int result = s - t;
if (result < 0)
result += M;
return *state = result;
}
//---------------------------------------------------------------------------
// Functions adapted from muldi3.S and div.S at:
// https://github.com/gcc-mirror/gcc/blob/master/libgcc/config/riscv/muldi3.S
// https://github.com/gcc-mirror/gcc/blob/master/libgcc/config/riscv/div.S
void StdlibFunctions()
{
__asm("__mulsi3: \n\t\
mv a2, a0 \n\t\
li a0, 0 \n\t\
.mulsi3_L1: \n\t\
andi a3, a1, 1 \n\t\
beqz a3, .mulsi3_L2 \n\t\
add a0, a0, a2 \n\t\
.mulsi3_L2: \n\t\
srli a1, a1, 1 \n\t\
slli a2, a2, 1 \n\t\
bnez a1, .mulsi3_L1 \n\t\
ret \n\t\
\n\t\
\n\t\
__divsi3: \n\t\
bltz a0, .divsi3_L10 \n\t\
bltz a1, .divsi3_L11 \n\t\
\n\t\
\n\t\
__udivsi3: \n\t\
mv a2, a1 \n\t\
mv a1, a0 \n\t\
li a0, -1 \n\t\
beqz a2, .udivsi3_L5 \n\t\
li a3, 1 \n\t\
bgeu a2, a1, .udivsi3_L2 \n\t\
.udivsi3_L1: \n\t\
blez a2, .udivsi3_L2 \n\t\
slli a2, a2, 1 \n\t\
slli a3, a3, 1 \n\t\
bgtu a1, a2, .udivsi3_L1 \n\t\
.udivsi3_L2: \n\t\
li a0, 0 \n\t\
.udivsi3_L3: \n\t\
bltu a1, a2, .udivsi3_L4 \n\t\
sub a1, a1, a2 \n\t\
or a0, a0, a3 \n\t\
.udivsi3_L4: \n\t\
srli a3, a3, 1 \n\t\
srli a2, a2, 1 \n\t\
bnez a3, .udivsi3_L3 \n\t\
.udivsi3_L5: \n\t\
ret \n\t\
\n\t\
\n\t\
__umodsi3: \n\t\
move t0, ra \n\t\
jal __udivsi3 \n\t\
move a0, a1 \n\t\
jr t0 \n\t\
\n\t\
\n\t\
.divsi3_L10: \n\t\
neg a0, a0 \n\t\
bgtz a1, .divsi3_L12 \n\t\
neg a1, a1 \n\t\
j __udivsi3 \n\t\
.divsi3_L11: \n\t\
neg a1, a1 \n\t\
.divsi3_L12: \n\t\
move t0, ra \n\t\
jal __udivsi3 \n\t\
neg a0, a0 \n\t\
jr t0 \n\t\
\n\t\
\n\t\
__modsi3: \n\t\
move t0, ra \n\t\
bltz a1, .modsi3_L31 \n\t\
bltz a0, .modsi3_L32 \n\t\
.modsi3_L30: \n\t\
jal __udivsi3 \n\t\
move a0, a1 \n\t\
jr t0 \n\t\
.modsi3_L31: \n\t\
neg a1, a1 \n\t\
bgez a0, .modsi3_L30 \n\t\
.modsi3_L32: \n\t\
neg a0, a0 \n\t\
jal __udivsi3 \n\t\
neg a0, a1 \n\t\
jr t0");
}