-
Notifications
You must be signed in to change notification settings - Fork 0
/
pi_project.asm
468 lines (452 loc) · 7.98 KB
/
pi_project.asm
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
; PI_project, a Monte-Carlo pi calculator written in 8bit-microchip-Assembly
;
; Copyright (C) 2010 Thomas Bertani <sylar@anche.no>
; portions Copyright (C) 2010 Giacomo Mariani
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License, version 3, as
; published by the Free Software Foundation.
;
; 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, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
list p=16F628A
#include p16f628A.inc
I EQU 0x20
J EQU I+1
_I EQU J+1
_J EQU _I+1
__I EQU _J+1
__J EQU __I+1
TMP0 EQU __J+1 ; riservato per le funzioni
TMP1 EQU TMP0+1
TMP2 EQU TMP1+1
_RANDOM EQU TMP2+1
RANDOM EQU _RANDOM+1
RAND_HI EQU RANDOM+1
RAND_LO EQU RAND_HI+1
N_max EQU RAND_LO+1
n_max EQU N_max+1
n_punti EQU n_max+1
i_y EQU n_punti+1
n EQU i_y+1
X EQU n+1
x EQU X+1
Y EQU x+1
y EQU Y+1
a EQU y+1
b_ EQU a+1
c EQU b_+1
d EQU c+1
e EQU d+1
f EQU e+1
g EQU f+1
n_slice EQU g+1
i EQU n_slice+1
r EQU i+1
DH EQU r+1
DL EQU DH+1
VH EQU DL+1
VL EQU VH+1
QH EQU VL+1
QL EQU QH+1
CL EQU QL+1
FL EQU CL+1
RES_ EQU FL+1
RES_HI EQU RES_+1
RES_LO EQU RES_HI+1
PRODHI EQU RES_LO+1
PRODLO EQU PRODHI+1
COUNT EQU PRODLO+1
ORG 0x0000
goto init
#include functions.inc
var_init:
movlw 0x07
movwf N_max
movlw 0x04
movwf n_max
movf n_max, 0
movwf I
movlw 0x02
movwf J
call div
movwf y ; y = n_max/2
movlw 0x0F
movwf n_punti
movlw 0x50
movwf _RANDOM
return
init:
bsf STATUS, 5
bcf PCON, 3 ; setta INTOSC a 48 KHz
bcf STATUS, 5
bsf INTCON,T0IE ;;
bsf INTCON,GIE ;;
call var_init
clrf n
clrf i_y
computation_loop:
incf i_y ; i_y+= 1
movf i_y, 0
movwf I
movf N_max, 0
movwf TMP0
incf TMP0
movf TMP0, 0
movwf J
call is_min ; controlla se i_y <= N_max
movwf TMP0
btfss TMP0, 0
goto result
goto pi_slice
pi_slice:
movf i_y, 0
movwf Y
clrf n_slice
clrf i
pi_slice_loop:
movf i, 0
movwf I
movf n_punti, 0
movwf J
call is_min ; controlla se i < n_punti
movwf TMP0
btfss TMP0, 0
goto pi_slice_end
;;
movlw 0x01
movwf I
movf N_max, 0
movwf J
call rand
movwf X ; X = randint(1, N_max)
;===============================;
movf X, 0
movwf I
movwf J
call mult
movwf TMP1
movf Y, 0
movwf I
movwf J
call mult
movwf TMP2
movf TMP1, 0
movwf I
movwf TMP2
movwf J
call add
movwf TMP1 ; TMP1 = X**2+Y**2
movf N_max, 0
movwf I
movwf J
call mult
movwf I
movlw 0x01
movwf J
call add
movwf TMP2 ; TMP2 = N_max**2+1
movf TMP1, 0
movwf I
movf TMP2, 0
movwf J
call is_min ; controlla se X**2+Y**2 <= N_max**2
movwf TMP0
btfss TMP0, 0
goto next_check_1
incf n_slice
goto pi_slice_addr0
next_check_1:
movf N_max, 0
movwf I
movwf J
call mult
movwf TMP1 ; TMP1 = N_max**2
movf X, 0
movwf TMP0
decf TMP0
movf TMP0, 0
movwf I
movwf J
call mult ; W = (X-1)**2
movwf TMP2
movf Y, 0
movwf TMP0
decf TMP0
movf TMP0, 0
movwf I
movwf J
call mult
movwf I
movf TMP2, 0
movwf J
call add
movwf TMP2 ; TMP2 = (X-1)**2+(Y-1)**2
movwf I
movf TMP1, 0
movwf J
call is_min
movwf TMP0
btfss TMP0, 0
goto pi_slice_addr0
goto next_check_2
next_check_2:
movlw 0x01
movwf I
movf n_max, 0
movwf J
call rand
movwf x ; x = randint(1, n_max)
movf X, 0
movwf TMP0
decf TMP0
movf TMP0, 0
movwf I
movwf J
call mult
movwf TMP1
movf Y, 0
movwf I
movwf J
call mult
movwf I
movf TMP1, 0
movwf J
call add
movwf TMP1 ; TMP1 = (X-1)**2+Y**2
movf N_max, 0
movwf I
movwf J
call mult
movwf I
movf Y, 0
movwf J
call add
movwf TMP2 ; TMP2 = N_max**2+Y
movf TMP1, 0
movwf I
movf TMP2, 0
movwf J
call is_min
movwf TMP0
btfss TMP0, 0
goto pi_slice_addr1
goto pi_slice_addr2
pi_slice_addr1:
movf x, 0
movwf I
movwf J
call mult
movwf TMP1
movf n_max, 0
movwf I
movwf J
call mult
movwf I
movf N_max, 0
movwf J
call mult
movwf TMP2
movf TMP1, 0
movwf I
movf TMP2, 0
movwf J
call add
movwf a ; a = x**2+N_max*n_max**2
movf N_max, 0
movwf I
movwf J
call mult
movwf I
movf Y, 0
movwf J
call add
movwf I
movf N_max
movwf J
call add
movwf TMP1 ; TMP1 = N_max**2+Y+N_max
movf X, 0
movwf TMP2
decf TMP2
movf TMP2, 0
movwf I
movwf J
call mult
movwf J
movf TMP1, 0
movwf I
call sub
movwf TMP1
movf Y, 0
movwf I
movwf J
call mult
movwf J
movf TMP1, 0
movwf I
call sub
movwf c ; c = N_max**2-(X-1)**2-Y**2+Y+N_max
goto pi_slice_addr3
pi_slice_addr2:
movf x, 0
movwf I
movwf J
call mult
movwf a ; a = x**2
movf N_max, 0
movwf I
movwf J
call mult
movwf I
movf Y, 0
movwf J
call add
movwf TMP1 ; TMP1 = N_max**2+Y
movf X, 0
movwf TMP2
decf TMP2
movf TMP2, 0
movwf I
movwf J
call mult
movwf J
movf TMP1, 0
movwf I
call sub
movwf TMP1
movf Y, 0
movwf I
movwf J
call mult
movwf J
movf TMP1, 0
movwf I
call sub
movwf c ; c = N_max**2-(X-1)**2-Y**2+Y
goto pi_slice_addr3
pi_slice_addr3:
movf X, 0
movwf TMP0
decf TMP0
movf TMP0, 0
movwf I
movlw 0x02
movwf J
call mult
movwf I
movf x, 0
movwf J
call mult
movwf I
movf n_max, 0
movwf J
call mult
movwf b_ ; b_ = x*(X-1)*2*n_max
movf n_max, 0
movwf I
movwf J
call mult
movwf d ; d = n_max**2
movf d, 0
movwf I
movf c, 0
movwf J
call mult
movwf e ; e = c*d
movf d, 0
movwf I
movlw 0x04
movwf J
call div
movwf J
movf e, 0
movwf I
call sub
movwf f ; f = e-d/4
movf a, 0
movwf I
movf b_, 0
movwf J
call add
movwf g ; g = a+b_
movf f, 0
movwf TMP1
incf TMP1
movf TMP1, 0
movwf J
movf g, 0
movwf I
call is_min ; controlla se g <= f
movwf TMP0
btfss TMP0, 0
nop
incf n_slice
;===============================;
pi_slice_addr0:
incf i
goto pi_slice_loop
pi_slice_end:
movf n_slice, 0
movwf I
movf n, 0
movwf J
call add
movwf n ; n += n_slice
goto computation_loop
result:
movf n, 0
movwf PRODLO
movlw 0x0A
call MPY8X8
;;;;
movf PRODHI, 0
movwf DH
movf PRODLO, 0
movwf DL
clrf VH
movlw 0x1A ; 26
movwf VL
call DIV16
movf QL, 0 ; int(PI)
movwf RES_
clrf DH
movwf DL
clrf VH
movlw 0x0A
movwf VL
call DIV16
movf QL, 0
movwf RES_HI
movf DL, 0
movwf RES_LO
end_:
bsf STATUS, 5
clrf TRISA
clrf TRISB
bcf STATUS, 5
result_printing:
movf RES_HI, 0
movwf I
call to_led
bcf PORTA, 1
movwf PORTB
bsf PORTB, 0 ;forza il "."
bsf PORTA, 0
movf RES_LO, 0
movwf I
call to_led
bcf PORTA, 0
movwf PORTB
bsf PORTA, 1
goto result_printing
sleep
;movf RES_, 0 ;per debuggare decommentare questa riga e commentare le due precedenti ;)
__end:
END