-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathretromalina.pas
3220 lines (2699 loc) · 82.3 KB
/
retromalina.pas
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// *****************************************************************************
// The retromachine unit for Raspberry Pi/Ultibo
// Ultibo v. 0.18 - 2017.01.22
// Piotr Kardasz
// pik33@o2.pl
// www.eksperymenty.edu.pl
// GPL 2.0 or higher
// uses combinedwaveforms.bin by Johannes Ahlebrand - MIT licensed
//******************************************************************************
//
// --- ALPHA, don't complain !!!
//
// Retromachine memory map @bytes FOR ULTIBO
//
// 0000_0000 - heap_start (about 0190_0000) - Ultibo area
// Heap start - 2EFF_FFFF retromachine program area, about 720 MB
//
// BASE=$2F00_0000 - this can change or become dynamic
//
// 2F00_0000 - 2F00_FFFF - 6502 area
// 2F00_D400 - 2F00_D418 SID
//
// 2F01_0000 - 2F05_FFFF - system data area
// 2F01_0000 - 2F04_FFFF pallette banks; 65536 entries
// 2F05_0000 - 2F05_1FFF font definition; 256 char @8x16 px
// 2F05_2000 - 2F05_9FFF static sprite defs 8x4k
// 2F05_A000 - 2F05_BFFF 8kB area for 16-bit audio buffer
// 2F05_C000 - 2F05_C03F audio DMA ctrl block
// 2F05_C040 - 2F05_FFFF reserved
//
// 2F06_0000 - 2F06_FFFF virtual hardware regs area
// 2F06_0000 - frame counter
// 2F06_0004 - display start
// 2F06_0008 - current graphics mode ----TODO
// 2F06_0009 - bytes per pixel
// 2F06_000C - border color
// 2F06_0010 - pallette bank ----TODO
// 2F06_0014 - horizontal pallette selector: bit 31 on, 30..20 add to $60010, 11:0 pixel num. ----TODO
// 2F06_0018 - display list start addr ----TODO
// DL entry: 00xx_YYLLL_MM - display LLL lines in mode MM
// xx: 00 - do nothing
// 01 - raster interrupt
// 10 - set pallette bank YY
// 11 - set horizontal scroll at YY
// 10xx_AAAAAAA - set display address to xxAAAAAAA
// 11xx_AAAAAAA - goto address xxAAAAAAA
// 2F06_001C - horizontal scroll right register ----TODO
// 2F06_0020 - x res
// 2F06_0024 - y res
// 2F06_0028 - KBD. 28 - ASCII 29 modifiers, 2A raw code 2B reserved
// 2F06_002C - mouse. 6002c,d x 6002e,f y
// 2F06_0030 - mouse keys, 2F06_0032 - mouse wheel; 127 up 129 down
// 2F06_0034 - current dl position ----TODO
// 2F06_0040 - 2F06_007C sprite control long 0 31..16 y pos 15..0 x pos
// long 1 30..16 y zoom 15..0 x zoom
// 2F06_0080 - 206009C dynamic sprite data pointer
// 2F06_00A0 - text cursor position
// 2F06_00A4 - text color
// 2F06_00A8 - background color
// 2F06_00AC - text size and pitch
// 2F06_00B0 - 2F06_00C0 - reserved
// 2F06_00C0 - 2F06_00FF - audio DMA ctrl blocks, 2x32 bytes
//
// 2F06_0100 - 2F06_01?? - blitter
// 2F06_0100 - 2F06_011F - blitter DMA ctrl block
// 2F06_0120 - 2F06_0127 - blitter fill color area
// 2F06_0100 - 2F06_FFFF - reserved
//
// 2F07_0000 - 2F08_FFFF - 2x64k long audio buffer for noise shaper
// 2F0D_0000 - 2FFF_FFFF - retromachine system area
// 3000_0000 - 30FF_FFFF - virtual framebuffer area
// 3100_0000 - 3AFF_FFFF - Ultibo system memory area
// 3B00_0000 - 3EFF_FFFF - GPU memory
// 3F00_0000 - 3FFF_FFFF - RPi real hardware registers
// TODO planned retromachine graphic modes:
// 00..15 Propeller retromachine compatible
// 16 - 1792x1120 @ 8bpp
// 17 - 896x560 @ 8 bpp
// 18 - 448x280 @ 8 bpp
// 19 - 224x140 @ 8 bpp
// 20..23 - 16 bpp modes
// 24..27 - 32 bpp modes
// 28 ..31 text modes - ?
// ---------------------------- This is still alpha quality code
unit retromalina;
{$mode objfpc}{$H+}
interface
uses sysutils,classes,unit6502,Platform,Framebuffer,retrokeyboard,retromouse,
threads,GlobalConst,ultibo,retro, simpleaudio, mp3, xmp, HeapManager;
const base= $2F000000; // retromachine system area base
nocache= $C0000000; // cache off address addition
const _pallette= $10000;
_systemfont= $50000;
_sprite0def= $52000;
_sprite1def= $53000;
_sprite2def= $54000;
_sprite3def= $55000;
_sprite4def= $56000;
_sprite5def= $57000;
_sprite6def= $58000;
_sprite7def= $59000;
_framecnt= $60000;
_displaystart= $60004;
_graphicmode= $60008;
_bpp= $60009;
_bordercolor= $6000C;
_pallettebank= $60010;
_palletteselector=$60014;
_dlstart= $60018;
_hscroll= $6001C;
_xres= $60020;
_yres= $60024;
_keybd= $60028;
_mousexy= $6002C;
_mousekey= $60030;
_dlpos= $60034;
_reserved01= $60038;
_reserved02= $6003C;
_spritebase= $60040;
_sprite0xy= $60040;
_sprite0zoom= $60044;
_sprite1xy= $60048;
_sprite1zoom= $6004C;
_sprite2xy= $60050;
_sprite2zoom= $60054;
_sprite3xy= $60058;
_sprite3zoom= $6005C;
_sprite4xy= $60060;
_sprite4zoom= $60064;
_sprite5xy= $60068;
_sprite5zoom= $6006C;
_sprite6xy= $60070;
_sprite6zoom= $60074;
_sprite7xy= $60078;
_sprite7zoom= $6007C;
_sprite0ptr= $60080;
_sprite1ptr= $60084;
_sprite2ptr= $60088;
_sprite3ptr= $6008C;
_sprite4ptr= $60090;
_sprite5ptr= $60094;
_sprite6ptr= $60098;
_sprite7ptr= $6009C;
_textcursor= $600A0;
_textcolor= $600A4;
_bkcolor= $600A8;
_textsize= $600AC;
_audiodma= $600C0;
type
// Retromachine main thread
TRetro = class(TThread)
private
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
end;
// File buffer thread
TFileBuffer= class(TThread)
private
buf:array[0..131071] of byte;
tempbuf:array[0..32767] of byte;
outbuf: array[0..8191] of byte;
//outbuf: array[0..4095] of smallint;// absolute outbuf;
pocz:integer;
koniec:integer;
il,fh,newfh:integer;
newfilename:string;
needclear:boolean;
seekamount:int64;
eof:boolean;
mp3:integer;
qq:integer;
maintenance:boolean;
reading:boolean;
protected
procedure Execute; override;
public
m:integer;
empty,full:boolean;
Constructor Create(CreateSuspended : boolean);
function getdata(b,ii:integer):integer;
procedure setfile(nfh:integer);
procedure clear;
procedure seek(amount:int64);
procedure setmp3(mp3b:integer);
end;
// mouse thread
Tmouse= class(TThread)
private
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
end;
TKeyboard= class(TThread)
private
protected
procedure Execute; override;
public
Constructor Create(CreateSuspended : boolean);
end;
type wavehead=packed record
riff:integer;
size:cardinal;
wave:integer;
fmt:integer;
fmtl:integer;
pcm:smallint;
channels:smallint;
srate:integer;
brate:integer;
bytesps:smallint;
bps:smallint;
data:integer;
datasize:cardinal;
end;
TSample=array[0..1] of smallint;
TSample32=array[0..1] of integer;
var fh,filetype:integer; // this needs cleaning...
sfh:integer; // SID file handler
play:word;
p2:^integer;
tim,t,t2,t3,ts,t6,time6502:int64;
vblank1:byte;
scope:array[0..1023] of integer;
db:boolean=false;
debug:integer;
sidtime:int64=0;
timer1:int64=-1;
siddelay:int64=20000;
songtime,songfreq:int64;
skip:integer;
scj:integer=0;
thread:TRetro;
times6502:array[0..15] of integer;
attacktable:array[0..15] of double=(5.208e-4,1.302e-4,6.51e-5,4.34e-5,2.74e-5,1.86e-5,1.53e-5,1.3e-5,1.04e-5,4.17e-6,2.08e-6,1.302e-6,1.04e-6,3.47e-7,2.08e-7,1.3e-7);
attacktablei:array[0..15] of int64;
srtablei:array[0..15] of int64;
sidcount:integer=1;
sampleclock:integer=0;
sidclock:integer=0;
siddata:array[0..1151] of integer;
i,j,k,l,fh2,lines:integer;
p,p3:pointer;
b:byte;
scrfh:integer;
running:integer=0;
p4:^integer;
fb:pframebufferdevice;
FramebufferProperties:TFramebufferProperties;
kbd:array[0..15] of TKeyboarddata;
m:array[0..128] of Tmousedata;
pause1:boolean=false;
i1l,i2l,fbl,topl:integer;
i1r,i2r,fbr,topr:integer;
buf2:array[0..1919] of smallint;
buf2f:array[0..959] of single absolute buf2;
filebuffer:TFileBuffer;
amouse:tmouse ;
akeyboard:tkeyboard ;
psystem,psystem2:pointer;
vol123:integer=0;
textcursoron:boolean=false;
head:wavehead;
nextsong:integer=0;
oldsc:integer=0;
sc:integer=0;
channel1on:byte=1;
channel2on:byte=1;
channel3on:byte=1;
mp3time:int64;
// system variables
systempallette:array[0..255] of TPallette absolute base+_pallette;
systemfont:TFont absolute base+_systemfont;
sprite0def:TSprite absolute base+_sprite0def;
sprite1def:TSprite absolute base+_sprite1def;
sprite2def:TSprite absolute base+_sprite2def;
sprite3def:TSprite absolute base+_sprite3def;
sprite4def:TSprite absolute base+_sprite4def;
sprite5def:TSprite absolute base+_sprite5def;
sprite6def:TSprite absolute base+_sprite6def;
sprite7def:TSprite absolute base+_sprite7def;
framecnt: cardinal absolute base+_framecnt;
displaystart: cardinal absolute base+_displaystart;
graphicmode: cardinal absolute base+_graphicmode;
bpp: byte absolute base+_bpp;
bordercolor: cardinal absolute base+_bordercolor;
pallettebank: cardinal absolute base+_pallettebank;
palletteselector:cardinal absolute base+_palletteselector;
dlstart: cardinal absolute base+_dlstart;
hscroll: cardinal absolute base+_hscroll;
xres: cardinal absolute base+_xres;
yres: cardinal absolute base+_yres;
key_charcode: byte absolute base+_keybd;
key_modifiers: byte absolute base+_keybd+1;
key_scancode: byte absolute base+_keybd+2;
mousexy: cardinal absolute base+_mousexy;
mousex: word absolute base+_mousexy;
mousey: word absolute base+_mousexy+2;
mousek: byte absolute base+_mousekey;
mouseclick: byte absolute base+_mousekey+1;
mousewheel: byte absolute base+_mousekey+2;
mousedblclick: byte absolute base+_mousekey+3;
dlpos: cardinal absolute base+_dlpos;
sprite0xy: cardinal absolute base+_sprite0xy;
sprite0x: word absolute base+_sprite0xy;
sprite0y: word absolute base+_sprite0xy+2;
sprite0zoom: cardinal absolute base+_sprite0zoom;
sprite0zoomx: word absolute base+_sprite0zoom;
sprite0zoomy: word absolute base+_sprite0zoom+2;
sprite1xy: cardinal absolute base+_sprite0xy;
sprite1x: word absolute base+_sprite1xy;
sprite1y: word absolute base+_sprite1xy+2;
sprite1zoom: cardinal absolute base+_sprite1zoom;
sprite1zoomx: word absolute base+_sprite1zoom;
sprite1zoomy: word absolute base+_sprite1zoom+2;
sprite2xy: cardinal absolute base+_sprite2xy;
sprite2x: word absolute base+_sprite2xy;
sprite2y: word absolute base+_sprite2xy+2;
sprite2zoom: cardinal absolute base+_sprite2zoom;
sprite2zoomx: word absolute base+_sprite2zoom;
sprite2zoomy: word absolute base+_sprite2zoom+2;
sprite3xy: cardinal absolute base+_sprite3xy;
sprite3x: word absolute base+_sprite3xy;
sprite3y: word absolute base+_sprite3xy+2;
sprite3zoom: cardinal absolute base+_sprite3zoom;
sprite3zoomx: word absolute base+_sprite3zoom;
sprite3zoomy: word absolute base+_sprite3zoom+2;
sprite4xy: cardinal absolute base+_sprite4xy;
sprite4x: word absolute base+_sprite4xy;
sprite4y: word absolute base+_sprite4xy+2;
sprite4zoom: cardinal absolute base+_sprite4zoom;
sprite4zoomx: word absolute base+_sprite4zoom;
sprite4zoomy: word absolute base+_sprite4zoom+2;
sprite5xy: cardinal absolute base+_sprite5xy;
sprite5x: word absolute base+_sprite5xy;
sprite5y: word absolute base+_sprite5xy+2;
sprite5zoom: cardinal absolute base+_sprite5zoom;
sprite5zoomx: word absolute base+_sprite5zoom;
sprite5zoomy: word absolute base+_sprite5zoom+2;
sprite6xy: cardinal absolute base+_sprite6xy;
sprite6x: word absolute base+_sprite6xy;
sprite6y: word absolute base+_sprite6xy+2;
sprite6zoom: cardinal absolute base+_sprite6zoom;
sprite6zoomx: word absolute base+_sprite6zoom;
sprite6zoomy: word absolute base+_sprite6zoom+2;
sprite7xy: cardinal absolute base+_sprite7xy;
sprite7x: word absolute base+_sprite7xy;
sprite7y: word absolute base+_sprite7xy+2;
sprite7zoom: cardinal absolute base+_sprite7zoom;
sprite7zoomx: word absolute base+_sprite7zoom;
sprite7zoomy: word absolute base+_sprite7zoom+2;
sprite0ptr: cardinal absolute base+_sprite0ptr;
sprite1ptr: cardinal absolute base+_sprite1ptr;
sprite2ptr: cardinal absolute base+_sprite2ptr;
sprite3ptr: cardinal absolute base+_sprite3ptr;
sprite4ptr: cardinal absolute base+_sprite4ptr;
sprite5ptr: cardinal absolute base+_sprite5ptr;
sprite6ptr: cardinal absolute base+_sprite6ptr;
sprite7ptr: cardinal absolute base+_sprite7ptr;
spritepointers: array[0..7] of cardinal absolute base+_sprite0ptr;
textcursorx: word absolute _textcursor;
textcursory: word absolute _textcursor+2;
textcolor: cardinal absolute _textcolor;
bkcolor: cardinal absolute _bkcolor;
textsizex: byte absolute _textsize;
textsizey: byte absolute _textsize+1;
textpitch: byte absolute _textsize+2;
audiodma1: array[0..7] of cardinal absolute _audiodma;
audiodma2: array[0..7] of cardinal absolute _audiodma+32;
desired, obtained:TAudioSpec;
error:integer;
mousereports:array[0..7] of TMouseReport;
mp3bufidx:integer=0;
outbufidx:integer=0;
framesize:integer;
// prototypes
procedure initmachine;
procedure stopmachine;
procedure setpallette(pallette:TPallette;bank:integer);
procedure cls(c:integer);
procedure putpixel(x,y,color:integer);
procedure putchar(x,y:integer;ch:char;col:integer);
procedure outtextxy(x,y:integer; t:string;c:integer);
procedure blit(from,x,y,too,x2,y2,length,lines,bpl1,bpl2:integer);
procedure box(x,y,l,h,c:integer);
procedure box2(x1,y1,x2,y2,color:integer);
function gettime:int64;
procedure poke(addr:integer;b:byte);
procedure dpoke(addr:integer;w:word);
procedure lpoke(addr:integer;c:cardinal);
procedure slpoke(addr,i:integer);
function peek(addr:integer):byte;
function dpeek(addr:integer):word;
function lpeek(addr:integer):cardinal;
function slpeek(addr:integer):integer;
procedure sethidecolor(c,bank,mask:cardinal);
procedure fcircle(x0,y0,r,c:integer);
procedure circle(x0,y0,r,c:integer);
procedure line(x,y,dx,dy,c:integer);
procedure line2(x1,y1,x2,y2,c:integer);
procedure putcharz(x,y:integer;ch:char;col,xz,yz:integer);
procedure outtextxyz(x,y:integer; t:string;c,xz,yz:integer);
procedure outtextxys(x,y:integer; t:string;c,s:integer);
procedure outtextxyzs(x,y:integer; t:string;c,xz,yz,s:integer);
procedure scrollup;
function sid(mode:integer):tsample;
procedure AudioCallback(userdata: Pointer; stream: PUInt8; len:Integer );
function getpixel(x,y:integer):integer; inline;
function getkey:integer; inline;
function readkey:integer; inline;
function click:boolean;
function dblclick:boolean;
procedure waitvbl;
procedure removeramlimits(addr:integer);
function readwheel: shortint; inline;
procedure unhidecolor(c,bank:cardinal);
//procedure dma_box(x,y,l,h,c:cardinal);
//procedure box3(x,y,l,h,c:integer);
implementation
procedure scrconvert(screen:pointer); forward;
procedure sprite(screen:pointer); forward;
// ---- TMouse thread methods --------------------------------------------------
constructor TMouse.Create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TMouse.Execute;
label p101;
var mb:tmousedata;
i,j:integer;
mi:cardinal;
x,y,w:integer;
m:TMouseReport;
mousexy,buttons,offsetx,offsety,wheel:integer;
const mousecount:integer=0;
begin
repeat
repeat m:=getmousereport; threadsleep(1); until m[0]<>255;
mousecount+=1;
j:=0; for i:=0 to 7 do if m[i]<>0 then j+=1;
if (j>1) or (mousecount<16) then
begin
for i:=0 to 7 do mouserecord[i]:=(m[i]);
for i:=0 to 6 do mousereports[i]:=mousereports[i+1];
mousereports[7]:=m;
end;
mousetype:=0;
j:=0;
for i:=0 to 6 do if mousereports[i,7]<>m[7] then j+=1;
for i:=0 to 6 do if mousereports[i,6]<>m[6] then j+=1;
for i:=0 to 6 do if mousereports[i,5]<>m[5] then j+=1;
for i:=0 to 6 do if mousereports[i,4]<>m[4] then j+=1;
if j=0 then begin mousetype:=0; goto p101; end;
j:=0;
for i:=0 to 7 do begin j+=mousereports[i,1]; j+=mousereports[i,7]; end;
for i:=0 to 7 do if (mousereports[i,3]<>$FF) and (mousereports[i,3]<>0) then j+=1;
if j=0 then begin mousetype:=3; goto p101; end;
for i:=0 to 6 do if mousereports[i,7]<>m[7] then mousetype:=m[0]; // 1 or 2
p101:
if mousetype=0 then // most standard mouse type
begin
buttons:=m[0];
offsetx:=shortint(m[1]);
offsety:=shortint(m[2]);
wheel:=shortint(m[3]);
end
else if mousetype=2 then // the strange Logitech wireless 12-bit mouse
begin
buttons:=m[1];
mousexy:=m[2]+256*(m[3] and 15);
if mousexy>=2048 then mousexy:=mousexy-4096;
if m[6]=0 then offsetx:=mousexy else offsetx:=0;
mousexy:=m[4]*16 + m[3] div 16;
if mousexy>=2048 then mousexy:=mousexy-4096;
if m[6]=0 then offsety:=mousexy else offsety:=0;
if ((m[7]=134) or (m[7]=198)) and (m[6]=0) and (m[1]=0) and (m[2]=0) and (m[3]=0) and (m[4]=0) then wheel:=shortint(m[5]) else wheel:=0;
end
else if mousetype=1 then
begin
buttons:=m[1];
mousexy:=m[2]+256*(m[3] and 15);
if mousexy>=2048 then mousexy:=mousexy-4096;
offsetx:=mousexy;
mousexy:=m[4]*16 + m[3] div 16;
if mousexy>=2048 then mousexy:=mousexy-4096;
offsety:=mousexy;
wheel:=shortint(m[5]);
end
else if mousetype=3 then // 16-bit mouse
begin
buttons:=shortint(m[0]);
offsetx:=shortint(m[2]);
offsety:=shortint(m[4]);
wheel:=shortint(m[6]);
end;
x:=mousex+offsetx;
if x<0 then x:=0;
if x>1791 then x:=1791;
mousex:=x;
y:=mousey+offsety;
if y<0 then y:=0;
if y>1119 then y:=1119;
mousey:=y;
mousek:=Buttons and 255;
if wheel<-1 then wheel:=-1;
if wheel>1 then wheel:=1;
w:=mousewheel+Wheel;
if w<127 then w:=127;
if w>129 then w:=129;
mousewheel:=w;//poke(base+$60032,w);
until terminated;
end;
// ---- TKeyboard thread methods --------------------------------------------------
constructor TKeyboard.Create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
procedure TKeyboard.Execute;
// At every vblank the thread tests if there is a report from the keyboard
// If yes, the kbd codes are poked to the system variables
// $60028 - translated code
// $60029 - modifiers
// $6002A - raw code
const rptcnt:integer=0;
activekey:integer=0;
m:integer=0;
c:integer=0;
dblclick:integer=0;
dblcnt:integer=0;
clickcnt:integer=0;
click:integer=0;
var ch:TKeyboardReport;
i:integer;
begin
repeat
waitvbl;
if textcursoron then
begin
i:=(framecnt div 15) mod 2 ; // todo - replace constant with sys var
sprite6y:=68+32*textcursory;
sprite6x:=64+16*textcursorx;
// cursor blink
if i=0 then sprite6x+=$1000 else sprite6x:=sprite6x and $0FFF;
end;
if mousedblclick=2 then begin dblclick:=0; dblcnt:=0; mousedblclick:=0; end;
if (dblclick=0) and (mousek=1) then begin dblclick:=1; dblcnt:=0; end;
if (dblclick=1) and (mousek=0) then begin dblclick:=2; dblcnt:=0; end;
if (dblclick=2) and (mousek=1) then begin dblclick:=3; dblcnt:=0; end;
if (dblclick=3) and (mousek=0) then begin dblclick:=4; dblcnt:=0; end;
inc(dblcnt); if dblcnt>10 then begin dblcnt:=10; dblclick:=0; end;
if dblclick=4 then mousedblclick:=1 else mousedblclick:=0;
if peek(base+$60031)=2 then begin click:=2; clickcnt:=10; end;
if (mousek=1) and (click=0) then begin click:=1; clickcnt:=0; end;
inc(clickcnt); if clickcnt>10 then begin clickcnt:=10; click:=2; end;
if (mousek=0) then click:=0;
if click=1 then poke (base+$60031,1) else poke (base+$60031,0);
ch:=getkeyboardreport;
if ch[0]<>255 then m:=ch[0];
if (ch[2]<>0) and (ch[2]<>255) then activekey:=ch[2]
else if (ch[0]<>0) and (ch[0]<>255) then activekey:=256+ch[0];
if (ch[2]<>0) and (activekey>0) then inc(rptcnt)
else if (ch[0]<>0) and (activekey>0) then inc(rptcnt);
if (ch[2]=0) and (ch[0]=0) then begin rptcnt:=0; activekey:=0; end;
if rptcnt>26 then rptcnt:=24 ;
if (rptcnt=1) or (rptcnt=24) then
begin
if (activekey<256) and ((m and $22)<>0) then c:=byte(translatescantochar(activekey,1))
else if (activekey<256) and ((m and $42)=$40) then c:=byte(translatescantochar(activekey,2))
else if (activekey<256) and ((m and $42)=$42) then c:=byte(translatescantochar(activekey,3))
else if (activekey<256) and (m=0) then c:=byte(translatescantochar(activekey,0));
key_charcode:=c;
key_modifiers:=m;
key_scancode:=activekey mod 256;
end;
until terminated;
end;
// ---- TFileBuffer thread methods --------------------------------------------------
constructor TFileBuffer.Create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
m:=131072;
pocz:=0;
koniec:=0;
fh:=-1;
newfh:=-1;
il:=0;
newfilename:='';
empty:=true; full:=false;
needclear:=false;
seekamount:=0;
eof:=true;
mp3:=0;
qq:=32768;
end;
procedure TFileBuffer.Execute;
var i,il2,k:integer;
ml:int64;
const cnt:integer=0;
var outbuf2: PSmallint;
// info:mp3_info_t;
// framesize:integer;
begin
outbuf2:=@outbuf;
ThreadSetCPU(ThreadGetCurrent,CPU_ID_2);
sleep(1);
repeat
if needclear or (seekamount<>0) or (newfh>0) then
// now do not do maintenence tasks while other thread is reading the buffer or the conflit may happen
begin
repeat until not reading;
maintenance:=true;
if eof and (newfh>0) then
begin
fh:=newfh;
newfh:=-1;
eof:=false;
qq:=32768;
end;
if seekamount<>0 then needclear:=true;
if needclear then
begin
koniec:=0;
pocz:=0;
needclear:=false;
empty:=true;
m:=131071;
for i:=0 to 131071 do buf[i]:=0;
qq:=32768;
for i:=0 to 32767 do tempbuf[i]:=0;
end;
if (seekamount<>0) and (fh>0) then
begin
fileseek(fh,seekamount,fsFromCurrent);
seekamount:=0;
end;
maintenance:=false;
end;
// end of maintenance processes
if (fh>0) and not eof then
begin
if koniec>=pocz then m:=131072-koniec+pocz-1 else m:=pocz-koniec-1;
if m>=32768 then // more than 32k free place, do a read
begin
if mp3=0 then // no decoding needed, simply read 32k from file
begin
il:=fileread(fh,tempbuf[0],qq);
for i:=0 to il-1 do buf[(i+koniec) and $1FFFF]:=tempbuf[i] ;
koniec:=(koniec+il) and $1FFFF;
m:=m-il;
if m<3*32678 then empty:=false;
if (il<qq) and empty then eof:=true;
end
else // compressed file: read and decompress
begin
cnt+=1;
il:=fileread(fh,tempbuf[32768-qq],qq);
if (il<qq) and empty then eof:=true;
if il=qq then
begin
ml:=gettime;
mad_stream_buffer(@test_mad_stream,@tempbuf, 32768);
mad_frame_decode(@test_mad_frame, @test_mad_stream);
mad_synth_frame(@test_mad_synth,@test_mad_frame);
if test_mad_synth.pcm.channels=2 then for i:=0 to 1151 do begin outbuf2[2*i]:= test_mad_synth.pcm.samples[0,i] div 8704; outbuf2[2*i+1]:= test_mad_synth.pcm.samples[1,i] div 8704; end;
if test_mad_synth.pcm.channels=1 then for i:=0 to 1151 do begin outbuf2[2*i]:= test_mad_synth.pcm.samples[0,i] div 8704; outbuf2[2*i+1]:= test_mad_synth.pcm.samples[0,i] div 8704; end;
il2:= (PtrUInt(test_mad_stream.next_frame)-ptruint(@tempbuf));
// box(100,100,100,100,0); outtextxyz(100,100,inttostr(PtrUInt(test_mad_stream.next_frame)-ptruint(@tempbuf)),15,2,2); outtextxyz(100,132,inttostr(tempbuf[il2]),15,2,2);
if head.srate=44100 then head.brate:=8*((130+il2*10) div 261)
else head.brate:=8*((120+il2*10) div 240);
head.srate:=44100;//info.sample_rate;
head.channels:=2;//info.channels;
for i:=il2 to 32767 do tempbuf[i-il2]:=tempbuf[i];
for i:=0 to 4*1152-1 do buf[(i+koniec) and $1FFFF]:=outbuf[i]; // audio bytes
qq:=il2;
koniec:=(koniec+4*1152) and $1FFFF;
mp3time:=gettime-ml;
if koniec>=pocz then m:=131072-koniec+pocz-1 else m:=pocz-koniec-1;
if m<131072-32768 then empty:=false;
end;
end;
end
else
begin
full:=true;
end;
end
else
begin
// if newfh>0 then
// begin
// fh:=newfh;
// newfh:=-1;
// eof:=false;
// end;
end;
sleep(1);
until terminated;
end;
procedure TFileBuffer.setmp3(mp3b:integer);
begin
mp3:=mp3b;
qq:=32768;
needclear:=true;
end;
procedure TFileBuffer.seek(amount:int64);
begin
seekamount:=amount;
end;
function TFileBuffer.getdata(b,ii:integer):integer;
var i,d:integer;
begin
repeat until not maintenance;
reading:=true;
result:=0;
if not empty then
begin
if koniec>=pocz then d:=koniec-pocz
else d:=131072-pocz+koniec;
if d>=ii then
begin
full:=false;
result:=ii;
for i:=0 to ii-1 do poke(b+i,buf[(pocz+i) and $1FFFF]);
pocz:=(pocz+ii) and $1FFFF;
if pocz=koniec then empty:=true;
end
else
begin
for i:=0 to ii-1 do poke(b+i,0);
result:=0;
empty:=true;
end;
end;
reading:=false;
end;
procedure TFileBuffer.setfile(nfh:integer);
begin
self.newfh:=nfh;
//eof:=false;
end;
procedure TFileBuffer.clear;
begin
self.needclear:=true;
end;
// ---- TRetro thread methods --------------------------------------------------
// ----------------------------------------------------------------------
// constructor: create the thread for the retromachine
// ----------------------------------------------------------------------
constructor TRetro.Create(CreateSuspended : boolean);
begin
FreeOnTerminate := True;
inherited Create(CreateSuspended);
end;
// ----------------------------------------------------------------------
// THIS IS THE MAIN RETROMACHINE THREAD
// - convert retromachine screen to raspberry screen
// - display sprites
// ----------------------------------------------------------------------
procedure TRetro.Execute;
// --- rev 21070111
var id:integer;
begin
ThreadSetCPU(ThreadGetCurrent,CPU_ID_3);
//ThreadSetPriority(ThreadGetCurrent,5);
sleep(1);
running:=1;
repeat
begin
vblank1:=0;
t:=gettime;
// InvalidateDataCacheRange(displaystart,$200000);
scrconvert(p2);
tim:=gettime-t;
t:=gettime;
sprite(p2);
ts:=gettime-t;
vblank1:=1;
CleanDataCacheRange(integer(p2),9216000);
framecnt+=1;
FramebufferDeviceSetOffset(fb,0,0,True);
FramebufferDeviceWaitSync(fb);
vblank1:=0;
t:=gettime;
// InvalidateDataCacheRange(displaystart,$200000);
scrconvert(p2+2304000);
tim:=gettime-t;
t:=gettime;
sprite(p2+2304000);
ts:=gettime-t;
vblank1:=1;
CleanDataCacheRange(integer(p2)+9216000,9216000);
framecnt+=1;
FramebufferDeviceSetOffset(fb,0,1200,True);
FramebufferDeviceWaitSync(fb);
end;
until terminated;
running:=0;
end;
// ---- Retromachine procedures ------------------------------------------------
// ----------------------------------------------------------------------
// initmachine: start the machine
// ----------------------------------------------------------------------
procedure initmachine;
// -- rev 20170111
var a,i,j,k:integer;
l,bb:byte;
fh2:integer;
Entry:TPageTableEntry ;
f: textfile;
begin
//init the framebuffer
//TODO: if the screen is 1920x1080 init it to this resolution
fb:=FramebufferDevicegetdefault;
FramebufferDeviceRelease(fb);
Sleep(100);
FramebufferProperties.Depth:=32;
FramebufferProperties.PhysicalWidth:=1920;
FramebufferProperties.PhysicalHeight:=1200;
FramebufferProperties.VirtualWidth:=FramebufferProperties.PhysicalWidth;
FramebufferProperties.VirtualHeight:=FramebufferProperties.PhysicalHeight * 2;
FramebufferDeviceAllocate(fb,@FramebufferProperties);
sleep(100);
FramebufferDeviceGetProperties(fb,@FramebufferProperties);
p2:=Pointer(FramebufferProperties.Address);
for i:=0 to (1920*2400)-1 do lpoke(PtrUint(p2)+4*i,ataripallette[146]);
for i:=base to base+$FFFFF do poke(i,0); // clean all system area
displaystart:=$30000000; // vitual framebuffer address
framecnt:=0; // frame counter
for i:=0 to 1792*1120 do lpoke($30800000+4*i,$30000000+i);
// init pallette, font and mouse cursor
systemfont:=st4font;
sprite7def:=mysz;
setpallette(ataripallette,0);
cls(146);
// init sprite data pointers
for i:=0 to 7 do spritepointers[i]:=base+_sprite0def+4096*i;
// start frame refreshing thread
thread:=tretro.create(true);
thread.start;
// init sid variables
for i:=0 to 127 do siddata[i]:=0;
for i:=0 to 15 do siddata[$30+i]:=round(1073741824*(1-2*attacktable[i])); //20*
for i:=0 to 15 do siddata[$40+i]:=2*round(1073741824*attacktable[i]);
for i:=0 to 1023 do siddata[128+i]:=combined[i];
for i:=0 to 1023 do siddata[128+i]:=(siddata[128+i]-128) shl 16;
siddata[$0e]:=$7FFFF8;
siddata[$1e]:=$7FFFF8;
siddata[$2e]:=$7FFFF8;
reset6502;
mad_stream_init(@test_mad_stream);
mad_synth_init(@test_mad_synth);
mad_frame_init(@test_mad_frame);
// start audio, mouse, kbd and file buffer threads
desired.callback:=@AudioCallback;
desired.channels:=2;
desired.format:=AUDIO_S16;