-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproiectCa.asm
More file actions
515 lines (424 loc) · 9.1 KB
/
Copy pathproiectCa.asm
File metadata and controls
515 lines (424 loc) · 9.1 KB
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
; auth: JavaKet (Mitran Andrei)
; computes frame buffer size + I2C timing
; prints to screen & simulates sending a line to Arduino over COM1.
;
org 100h
jmp start
; Inputs
WIDTH dw 320
HEIGHT dw 240
BPP dw 2 ; bytes per pixel
APB_KHZ dd 80000 ; 80 MHz = 80000 kHz
SCL_KHZ dw 400 ; 400
;Nota bene: apb khz must be dd because dw is 16 bits and we dont want that
; we need 32 bit because we will overwflow at 65535 otherwise
;outputs
FRAME_LO dw 0
FRAME_HI dw 0 ;hi low frame bites (32 bits)
TOTAL_CYC dw 0
HIGH_CYC dw 0
LOW_CYC dw 0
WH_LO dw 0
WH_HI dw 0
REM_CYC dw 0
OUT_NR dw 0 ; output counter
msgTitle db 13,10,'Computation Results',13,10,'$'
msgOutNr db 'Output nr = $'
msgFrame db 13,10,'Frame bytes = $'
msgI2C db 13,10,'I2C: total_cycles = $'
msgHigh db 13,10,'I2C: high_cycles = $'
msgLow db 13,10,'I2C: low_cycles = $'
msgWH db 13,10,'W*H = $'
msgBPP db 13,10,'BPP = $'
msgAPB db 13,10,'APB_KHZ = $'
msgSCL db 13,10,'SCL_KHZ = $'
msgRem db 13,10,'I2C: remainder = $'
msgNL db 13,10,'$'
msgSend db 13,10,'Sending to COM1 as: $'
msgAgain db 13,10,13,10,'Press R to recompute, ESC to quit$'
; sim serial buffer
COMBUF db 128 dup(0)
COMIDX dw 0
; FB=<u32>,T=<u16>,H=<u16>,L=<u16>\r\n
start:
main_loop:
call ComputeAll
call PrintResults
call SimSendResultsToCom1
lea dx, msgAgain
call PrintStr
; wait key
mov ah, 00h
int 16h
cmp al, 27 ; ESC
je exit
cmp al, 'r'
je main_loop
cmp al, 'R'
je main_loop
jmp main_loop
exit:
mov ax, 4C00h
int 21h
ComputeAll:
; output counter
inc word ptr [OUT_NR]
; frame_bytes = WIDTH * HEIGHT * BPP
mov ax, [WIDTH]
mov bx, [HEIGHT]
mul bx ; DX:AX = WIDTH*HEIGHT
; save W*H for display/debug
mov [WH_LO], ax
mov [WH_HI], dx
; mult 32bit (DX:AX) by 16bit BPP
mov bx, [BPP]
push dx ; save WH_hi
push ax ; and WH_lo
; low_part = WH_lo * BPP
pop ax
mul bx ; DX:AX = low_part
mov [FRAME_LO], ax
mov [FRAME_HI], dx
; high_part = WH_hi * BPP shifted by 16 => add into FRAME_HI
pop ax ; AX = WH_hi
xor dx, dx
mul bx ; DX:AX = high_part
add [FRAME_HI], ax ; add shifted contribution
; I2C total cycles: TOTAL = APB_KHZ / SCL_KHZ
mov ax, word ptr [APB_KHZ] ; low word
mov dx, word ptr [APB_KHZ+2] ; high word
mov bx, [SCL_KHZ]
call DivU32ByU16
; store quotient + remainder for display/debug
mov [TOTAL_CYC], ax
mov [REM_CYC], dx
; high = total / 2
mov ax, [TOTAL_CYC]
shr ax, 1
mov [HIGH_CYC], ax
; low = total - high
mov ax, [TOTAL_CYC]
sub ax, [HIGH_CYC]
mov [LOW_CYC], ax
ret
PrintResults: ;print results logic
lea dx, msgTitle
call PrintStr
; Output nr
lea dx, msgOutNr
call PrintStr
mov ax, [OUT_NR]
call PrintU16
lea dx, msgNL
call PrintStr
; Frame bytes 32bit
lea dx, msgFrame
call PrintStr
mov dx, [FRAME_HI]
mov ax, [FRAME_LO]
call PrintU32
lea dx, msgNL
call PrintStr
; total cycles
lea dx, msgI2C
call PrintStr
mov ax, [TOTAL_CYC]
call PrintU16
; high cycles
lea dx, msgHigh
call PrintStr
mov ax, [HIGH_CYC]
call PrintU16
; low cycles
lea dx, msgLow
call PrintStr
mov ax, [LOW_CYC]
call PrintU16
lea dx, msgNL
call PrintStr
;debug displays for the computation steps
; W*H
lea dx, msgWH
call PrintStr
mov dx, [WH_HI]
mov ax, [WH_LO]
call PrintU32
; BPP
lea dx, msgBPP
call PrintStr
mov ax, [BPP]
call PrintU16
; APB_KHZ (32-bit)
lea dx, msgAPB
call PrintStr
mov ax, word ptr [APB_KHZ]
mov dx, word ptr [APB_KHZ+2]
call PrintU32
; SCL_KHZ
lea dx, msgSCL
call PrintStr
mov ax, [SCL_KHZ]
call PrintU16
; remainder from APB_KHZ SCL_KHZ
lea dx, msgRem
call PrintStr
mov ax, [REM_CYC]
call PrintU16
lea dx, msgNL
call PrintStr
ret
; sim for sendin the frame buffer to com1
SimSendResultsToCom1:
; reset buffer index
mov word ptr [COMIDX], 0
;"FB="
mov al, 'F'; build buffer
call BufPutChar
mov al, 'B'
call BufPutChar
mov al, '='
call BufPutChar
;FB value (u32)
mov dx, [FRAME_HI]
mov ax, [FRAME_LO]
call BufPutU32
mov al, ','
call BufPutChar
mov al, 'T'
call BufPutChar
mov al, '='
call BufPutChar
mov ax, [TOTAL_CYC]
call BufPutU16
mov al, ','
call BufPutChar
mov al, 'H'
call BufPutChar
mov al, '='
call BufPutChar
mov ax, [HIGH_CYC]
call BufPutU16
mov al, ','
call BufPutChar
mov al, 'L'
call BufPutChar
mov al, '='
call BufPutChar
mov ax, [LOW_CYC]
call BufPutU16
; line end \r\n
mov al, 13
call BufPutChar
mov al, 10
call BufPutChar
;terminate for PrintStr ($)
mov al, '$'
call BufPutChar
;display
lea dx, msgSend
call PrintStr
lea dx, COMBUF
call PrintStr
lea dx, msgNL
call PrintStr
ret
;append AL into COMBUF at COMIDX
BufPutChar:
push bx
push di
mov bx, [COMIDX]
lea di, COMBUF
add di, bx
mov [di], al
inc bx
mov [COMIDX], bx
pop di
pop bx
ret
;append unsigned 16-bit AX in decimal to COMBUF
BufPutU16:
push ax
push bx
push cx
push dx
mov cx, 0
mov bx, 10
cmp ax, 0
jne b16_loop
mov al, '0'
call BufPutChar
jmp b16_done
b16_loop:
xor dx, dx
div bx
push dx
inc cx
cmp ax, 0
jne b16_loop
b16_print:
pop dx
add dl, '0'
mov al, dl
call BufPutChar
loop b16_print
b16_done:
pop dx
pop cx
pop bx
pop ax
ret
;append unsigned 32bit DX:AX in decimal to COMBUF
BufPutU32:
push ax
push bx
push cx
push dx
push si
push di
mov si, ax
or si, dx
jne b32_conv
mov al, '0'
call BufPutChar
jmp b32_out
b32_conv:
mov cx, 0
mov bx, 10
b32_next:
; divide 32bit DX:AX by 10
mov si, ax
mov di, dx
; q_high = high/10, rem_high = high%10
mov ax, di
xor dx, dx
div bx
mov di, ax ; q_high
; q_low = (rem_high<<16 + low)/10
mov ax, si
div bx ; AX=q_low, DX=remainder
mov si, ax ; !!! IMPORTANT: update quotient low !!!
push dx
inc cx
mov dx, di
mov ax, si
mov si, ax
or si, dx
jne b32_next
b32_print:
pop dx
add dl, '0'
mov al, dl
call BufPutChar
loop b32_print
b32_out:
pop di
pop si
pop dx
pop cx
pop bx
pop ax
ret
; input: DX:AX = numerator 32 bit & BX = divisor 16 bit
; output: AX = quotient 7 DX = remainder
DivU32ByU16:
push si
push di
mov si, ax ; low
mov di, dx ; high
; q_high = high / BX, rem_high = high % BX
mov ax, di
xor dx, dx
div bx ; AX=q_high, DX=rem_high
; Now divide (rem_high<<16 + low) by BX
mov ax, si ; low in AX, rem_high already in DX
div bx ; AX=q_low, DX=remainder
pop di
pop si
ret
; DS:DX points to '$'-terminated string
PrintStr:
mov ah, 09h
int 21h
ret
; prints unsigned 16-bit AX in decimal
PrintU16:
push ax
push bx
push cx
push dx
mov cx, 0
mov bx, 10
cmp ax, 0
jne pu16_loop
mov dl, '0'
mov ah, 02h
int 21h
jmp pu16_done
pu16_loop:
xor dx, dx
div bx
push dx
inc cx
cmp ax, 0
jne pu16_loop
pu16_print:
pop dx
add dl, '0'
mov ah, 02h
int 21h
loop pu16_print
pu16_done:
pop dx
pop cx
pop bx
pop ax
ret
; prints unsigned 32bit DX:AX in decimal/
PrintU32:
push ax
push bx
push cx
push dx
push si
push di
mov si, ax
or si, dx
jne pu32_conv
mov dl, '0'
mov ah, 02h
int 21h
jmp pu32_out
pu32_conv:
mov cx, 0
mov bx, 10
pu32_next:
mov si, ax
mov di, dx
mov ax, di
xor dx, dx
div bx
mov di, ax
mov ax, si
div bx
mov si, ax
push dx
inc cx
mov dx, di
mov ax, si
mov si, ax
or si, dx
jne pu32_next
pu32_print:
pop dx
add dl, '0'
mov ah, 02h
int 21h
loop pu32_print
pu32_out:
pop di
pop si
pop dx
pop cx
pop bx
pop ax
ret