-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsz80em.fs
1949 lines (1699 loc) · 72.1 KB
/
sz80em.fs
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
namespace FSpectrum
open System
open System.IO
open System.Drawing
open System.Drawing.Imaging
open Microsoft.FSharp.NativeInterop
open System.Windows.Forms
open System.Threading
type IPort =
abstract read: uint16 -> byte
type KeyCode = { row: int; bit: uint8; }
type public IKeyboard =
abstract KeyPressed: Keys -> unit
abstract KeyReleased: Keys -> unit
type KeyboardMsg =
| KeyPressed of Keys
| KeyReleased of Keys
| ReadKeys of uint16 * AsyncReplyChannel<byte>
type Keyboard() = class
let ports = Array.create 8 0xFFuy
let keyMap =
Map.empty.
Add(Keys.ShiftKey, [ { row = 0; bit = 0x01uy } ]).
Add(Keys.Z, [ { row = 0; bit = 0x02uy } ]).
Add(Keys.X, [ { row = 0; bit = 0x04uy } ]).
Add(Keys.C, [ { row = 0; bit = 0x08uy } ]).
Add(Keys.V, [ { row = 0; bit = 0x10uy } ]).
Add(Keys.A, [ { row = 1; bit = 0x01uy } ]).
Add(Keys.S, [ { row = 1; bit = 0x02uy } ]).
Add(Keys.D, [ { row = 1; bit = 0x04uy } ]).
Add(Keys.F, [ { row = 1; bit = 0x08uy } ]).
Add(Keys.G, [ { row = 1; bit = 0x10uy } ]).
Add(Keys.Q, [ { row = 2; bit = 0x01uy } ]).
Add(Keys.W, [ { row = 2; bit = 0x02uy } ]).
Add(Keys.E, [ { row = 2; bit = 0x04uy } ]).
Add(Keys.R, [ { row = 2; bit = 0x08uy } ]).
Add(Keys.T, [ { row = 2; bit = 0x10uy } ]).
Add(Keys.D1, [ { row = 3; bit = 0x01uy } ]).
Add(Keys.D2, [ { row = 3; bit = 0x02uy } ]).
Add(Keys.D3, [ { row = 3; bit = 0x04uy } ]).
Add(Keys.D4, [ { row = 3; bit = 0x08uy } ]).
Add(Keys.D5, [ { row = 3; bit = 0x10uy } ]).
Add(Keys.D0, [ { row = 4; bit = 0x01uy } ]).
Add(Keys.D9, [ { row = 4; bit = 0x02uy } ]).
Add(Keys.D8, [ { row = 4; bit = 0x04uy } ]).
Add(Keys.D7, [ { row = 4; bit = 0x08uy } ]).
Add(Keys.D6, [ { row = 4; bit = 0x10uy } ]).
Add(Keys.P, [ { row = 5; bit = 0x01uy } ]).
Add(Keys.O, [ { row = 5; bit = 0x02uy } ]).
Add(Keys.I, [ { row = 5; bit = 0x04uy } ]).
Add(Keys.U, [ { row = 5; bit = 0x08uy } ]).
Add(Keys.Y, [ { row = 5; bit = 0x10uy } ]).
Add(Keys.Enter, [ { row = 6; bit = 0x01uy} ]).
Add(Keys.L, [ { row = 6; bit = 0x02uy } ]).
Add(Keys.K, [ { row = 6; bit = 0x04uy } ]).
Add(Keys.J, [ { row = 6; bit = 0x08uy } ]).
Add(Keys.H, [ { row = 6; bit = 0x10uy } ]).
Add(Keys.Space, [ { row = 7; bit = 0x01uy } ]).
Add(Keys.ControlKey, [ { row = 7; bit = 0x02uy } ]).
Add(Keys.M, [ { row = 7; bit = 0x04uy } ]).
Add(Keys.N, [ { row = 7; bit = 0x08uy } ]).
Add(Keys.B, [ { row = 7; bit = 0x10uy } ]).
Add(Keys.Escape, [ { row = 0; bit = 0x01uy };
{ row = 7; bit = 0x01uy } ]).
Add(Keys.Back, [ { row = 0; bit = 0x01uy };
{ row = 4; bit = 0x01uy } ]).
Add(Keys.Capital, [ { row = 0; bit = 0x01uy };
{ row = 3; bit = 0x02uy } ]).
Add(Keys.Oemplus, [ { row = 7; bit = 0x02uy };
{ row = 6; bit = 0x02uy } ]) // ctrl + L
let processor =
MailboxProcessor.Start(fun inbox ->
let rec nextMsg =
async {
let! msg = inbox.Receive()
match msg with
| KeyPressed key ->
if keyMap.ContainsKey(key) then
for k in keyMap.Item(key) do
ports.[k.row] <- ports.[k.row] &&& (k.bit ^^^ 0xFFuy)
return! nextMsg
| KeyReleased key ->
if keyMap.ContainsKey(key) then
for k in keyMap.Item(key) do
ports.[k.row] <- ports.[k.row] ||| k.bit
return! nextMsg
| ReadKeys (port, reply) ->
let result =
(byte (port >>> 8))
|> Seq.unfold (fun i -> Some(i, i >>> 1))
|> Seq.take 8
|> Seq.mapi (fun i port -> if port &&& 0x01uy = 0uy then ports.[i] else 0uy)
|> Seq.filter(fun i -> i > 0uy)
|> Seq.fold (fun r v -> r &&& v) 0xFFuy
reply.Reply(result)
return! nextMsg
}
nextMsg
)
interface IKeyboard with
member x.KeyPressed e = processor.Post(KeyPressed(e))
member x.KeyReleased e = processor.Post(KeyReleased(e))
interface IPort with
member x.read port = processor.PostAndReply(
(fun reply -> ReadKeys(port, reply)),
timeout = 200)
end
type DirectBitmapAccess(bitmap: Bitmap) =
let data = bitmap.LockBits(
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.WriteOnly,
bitmap.PixelFormat)
let setPixel x y (r,g,b) =
let address =
NativePtr.add<byte> (NativePtr.ofNativeInt data.Scan0)
((y * data.Stride) + (x * 3))
NativePtr.write address b
NativePtr.set address 1 g
NativePtr.set address 2 r
member this.SetPixel(x, y, color: Color) =
setPixel x y (color.R, color.G, color.B)
interface IDisposable with
member this.Dispose() =
bitmap.UnlockBits(data)
type IVideoMemoryProcessor =
abstract ChangeFlash: unit
abstract DrawImage: byte[] -> unit
abstract GetImage: Image
type VideoMemoryProcessorMsg =
| DrawImage of byte[]
| GetImage of AsyncReplyChannel<Bitmap>
| Flash
type VideoMemoryProcessor() as this = class
let WIDTH = 256;
let HEIGHT = 192;
let CHAR_WIDTH_HEIGHT = 8;
let WIDTH_IN_CHARS = WIDTH / CHAR_WIDTH_HEIGHT;
let HEIGHT_IN_CHARS = HEIGHT / CHAR_WIDTH_HEIGHT;
let SCREEN_MEMORY_SIZE = WIDTH * HEIGHT / CHAR_WIDTH_HEIGHT;
let CONTENDED_MEMORY_START = 0x4000
let screens =
Array.init 2 (fun i -> new Bitmap(WIDTH, HEIGHT, PixelFormat.Format24bppRgb))
let mutable screen_index = 0
let mutable flash = 0x7Fuy;
let processor =
MailboxProcessor.Start(fun inbox ->
let rec nextMsg =
async {
let! msg = inbox.Receive()
match msg with
| GetImage(reply) ->
reply.Reply(screens.[(screen_index + 1) % 2])
return! nextMsg
| DrawImage bitmap ->
this.drawFrame bitmap
screen_index <- (screen_index + 1) % 2
return! nextMsg
| Flash ->
flash <- if flash = 0x7Fuy then 0xFFuy else 0x7Fuy
return! nextMsg
}
nextMsg
)
let COLORS =
Map.empty.
Add(0b0000, Color.FromArgb(0xFF, 0x00, 0x00, 0x00)).
Add(0b1110, Color.FromArgb(0xFF, 0xFF, 0xFF, 0x00)).
Add(0b0001, Color.FromArgb(0xFF, 0x00, 0x00, 0xC0)).
Add(0b1001, Color.FromArgb(0xFF, 0x00, 0x00, 0xFF)).
Add(0b0010, Color.FromArgb(0xFF, 0xC0, 0x00, 0x00)).
Add(0b1010, Color.FromArgb(0xFF, 0xFF, 0x00, 0x00)).
Add(0b0011, Color.FromArgb(0xFF, 0xC0, 0x00, 0xC0)).
Add(0b1011, Color.FromArgb(0xFF, 0xFF, 0x00, 0xFF)).
Add(0b0100, Color.FromArgb(0xFF, 0x00, 0xC0, 0x00)).
Add(0b1000, Color.FromArgb(0xFF, 0x00, 0x00, 0x00)).
Add(0b0101, Color.FromArgb(0xFF, 0x00, 0xC0, 0xC0)).
Add(0b1101, Color.FromArgb(0xFF, 0x00, 0xFF, 0xFF)).
Add(0b0110, Color.FromArgb(0xFF, 0xC0, 0xC0, 0x00)).
Add(0b1100, Color.FromArgb(0xFF, 0x00, 0xFF, 0x00)).
Add(0b0111, Color.FromArgb(0xFF, 0xC0, 0xC0, 0xC0)).
Add(0b1111, Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF))
member private x.drawFrame(memory: byte[]) =
using (new DirectBitmapAccess(screens.[screen_index])) (fun lockContext ->
for i in CONTENDED_MEMORY_START..(CONTENDED_MEMORY_START + SCREEN_MEMORY_SIZE - 1) do
let address = i - CONTENDED_MEMORY_START // 0x4000
let row = ((address &&& 0b0001100000000000) >>> 8) ||| ((address &&& 0b11100000) >>> 5)
let col = (address &&& 0b11111)
let line = ((address &&& 0b11100000000) >>> 8)
let attribute = memory.[CONTENDED_MEMORY_START
+ SCREEN_MEMORY_SIZE + row * WIDTH_IN_CHARS + col] &&& flash
let mutable ink = int ((attribute &&& 0b111uy) ||| ((attribute &&& 0b1000000uy) >>> 3))
let mutable paper = int ((attribute &&& 0b1111000uy) >>> 3)
if (attribute &&& 0x80uy) <> 0uy then
let tmp = ink
ink <- paper
paper <- tmp
let mutable x = 0
let mutable mask = 0b10000000uy
while mask > 0uy do
let xx = (col * CHAR_WIDTH_HEIGHT + x);
let yy = row * CHAR_WIDTH_HEIGHT + line;
let clr = COLORS.[if (memory.[i] &&& mask) = 0uy then paper else ink]
lockContext.SetPixel(xx, yy, clr)
mask <- mask >>> 1
x <- x + 1
)
interface IVideoMemoryProcessor with
member x.ChangeFlash = processor.Post(Flash)
member x.DrawImage bitmap = processor.Post(DrawImage bitmap)
member x.GetImage =
processor.PostAndReply(
(fun reply -> GetImage(reply)), timeout = 200) :> Image
end
type IDisplay =
abstract draw: byte[] -> uint16 -> unit
type Display(videoProcessor: IVideoMemoryProcessor) as this = class
inherit Panel(Dock = DockStyle.Fill)
do
this.DoubleBuffered <- true
this.Visible <- true
override x.OnPaint e =
e.Graphics.DrawImage(videoProcessor.GetImage, 0, 0)
interface IDisplay with
member x.draw (memory: byte[]) frame =
if frame % 16us = 0us then
videoProcessor.ChangeFlash
videoProcessor.DrawImage memory
x.Invalidate()
end
exception InstructionNotSupported of string
type Z80(keyboard: IPort) as this = class
let A = 0
let F = 1
let B = 2
let C = 3
let D = 4
let E = 5
let H = 6
let L = 7
let AS = 8
let FS = 9
let BS = 10
let CS = 11
let DS = 12
let ES = 13
let HS = 14
let LS = 15
let regs = [B; C; D; E; H; L; A]
let IX = 0
let IY = 1
let S_FLAG = 0b10000000uy
let Z_FLAG = 0b01000000uy
let X5_FLAG = 0b00100000uy
let H_FLAG = 0b00010000uy
let X3_FLAG = 0b00001000uy
let P_FLAG = 0b00000100uy
let V_FLAG = 0b00000100uy
let N_FLAG = 0b00000010uy
let C_FLAG = 0b00000001uy
let X53_FLAGS = X5_FLAG ||| X3_FLAG
let SZ_FLAGS = S_FLAG ||| Z_FLAG
let SZP_FLAGS = SZ_FLAGS ||| P_FLAG
let SZHN_FLAGS = SZ_FLAGS ||| H_FLAG ||| N_FLAG
let mutable ir = 0us
let mutable sp = 0us
let mutable pc = 0us
let register = Array.create 16 0uy
let index_register = Array.create 2 0us
let memory = Array.create (0xFFFF+1) 0xFFuy
let precomputed_flags =
Array.create 256 0uy |> Array.mapi (
fun i v -> (if i > 0x7F then S_FLAG else 0uy) |||
((byte i) &&& X53_FLAGS) |||
(if i = 0 then Z_FLAG else 0uy))
let sz53n_add = precomputed_flags
let sz53n_sub = precomputed_flags |> Array.map (fun v -> v ||| N_FLAG)
let powers_of_two = 1 |> Seq.unfold (fun i -> Some(i, i <<< 1))
let parity v len = powers_of_two |>
Seq.take len |>
Seq.fold (fun e i -> if i &&& v <> 0 then not e else e) true
let parityByte v = parity v 8
let sz53pn_add = sz53n_add |> Array.mapi (fun i v -> v ||| (if parityByte i then P_FLAG else 0uy))
let sz53pn_sub = sz53n_sub |> Array.mapi (fun i v -> v ||| (if parityByte i then P_FLAG else 0uy))
let halfcarry_add_table = [| false; true; true; true; false; false; false; true |]
let halfcarry_sub_table = [| false; false; true; false; true; false; true; true |]
let overflow_add_table = [| false; false; false; true; true; false; false; false |]
let overflow_sub_table = [| false; true; false; false; false; false; true; false |]
let SCREEN_CONTENTS = 128
let LEFT_BORDER = 24
let RIGHT_BORDER = 24
let RETRACE = 48
let SCREEN_CONTENTS_AND_BORDER_AND_RETRACE =
SCREEN_CONTENTS + RIGHT_BORDER + LEFT_BORDER + RETRACE
let TOTAL_LINES = 192
let PATTERN_LENGTH = 8
let TSTATE_START = 14335
let TSTATE_END =
TSTATE_START + SCREEN_CONTENTS_AND_BORDER_AND_RETRACE * TOTAL_LINES - 1
let CONTENDED_MEMORY_START = 0x4000us
let CONTENDED_MEMORY_END = 0x7FFFus
let FRAME_CYCLES_TOTAL = 69888
let mutable tstates = 0
let mutable frames = 0us
let mutable im = 0
let mutable ei = false
let mutable iff1 = false
let mutable iff2 = false
let mutable halt = false
let mutable intr = false
let MEMORY_READ_TIME = 3
let MEMORY_WRITE_TIME = 3
let OP_FETCH_TIME = 4
let MASK_8 = 0xFFuy
let operations =
Array.init 256 (fun i ->
(fun () -> raise (InstructionNotSupported("Unknown Instruction")) ))
let operations_extended =
Array.init 256 (fun i ->
(fun () -> raise (InstructionNotSupported("Unknown Extended Instruction")) ))
let operations_bits =
Array.init 256 (fun i ->
(fun () -> raise (InstructionNotSupported("Unknown Bit Instruction")) ))
let operations_indexes =
Array.init 256 (fun i ->
(fun (ireg: int) -> raise (InstructionNotSupported("Unknown Index Instruction")) ))
let operations_indexes_bits =
Array.init 256 (fun i -> (fun (address: uint16) (value: byte) ->
raise (InstructionNotSupported("Unknown Index Bit Instruction")) ))
do
this.initializeOperations
this.initializeExtendedOperations
this.initializeBitOperations
this.initializeIndexOperations
this.initializeIndexBitOperations
member x.Memory
with get() = memory
member x.Tstates
with get() = tstates
member x.Frames
with get() = frames
member x.R
with get() = byte ir
and set(v: byte) = ir <- (ir &&& 0xFF00us) ||| (uint16 v)
member x.I
with get() = byte (ir >>> 8)
and set(v: byte) = ir <- (ir &&& 0x00FFus) ||| ((uint16 v) <<< 8)
member x.Iff1
with get() = iff1
and set(v) = iff1 <- v
member x.Iff2
with get() = iff2
and set(v) = iff2 <- v
member x.loadRom(romFile: String) =
let rom = File.ReadAllBytes(romFile)
rom.CopyTo(memory, 0)
member x.get_contention_delay tstate =
match tstate with
| tstate when tstate < TSTATE_START || tstate > TSTATE_END -> 0
| _ -> let b_m = (tstate - TSTATE_START) % SCREEN_CONTENTS_AND_BORDER_AND_RETRACE
if b_m >= SCREEN_CONTENTS then 0 else
let delay = (PATTERN_LENGTH - (b_m % PATTERN_LENGTH)) - 2
if delay > 0 then delay else 0
member x.check_memory_contention (addr: uint16) =
tstates <- tstates + (if addr >= CONTENDED_MEMORY_START && addr <= CONTENDED_MEMORY_END
then x.get_contention_delay tstates
else 0)
member x.check_memory_contention_m (addr: uint16) times =
if addr >= CONTENDED_MEMORY_START && addr <= CONTENDED_MEMORY_END then
for i = 1 to times do
tstates <- tstates + 1 + x.get_contention_delay tstates
else
tstates <- tstates + times
member x.write_to_port (port: uint16) (value: byte) =
x.check_memory_contention port
tstates <- tstates + 1
if port &&& 0x0001us <> 0us then
x.check_memory_contention_m port 3
else
x.check_memory_contention port
tstates <- tstates + 3
member x.read_from_port (port: uint16) =
x.check_memory_contention port
tstates <- tstates + 1
if port &&& 0x0001us <> 0us then
x.check_memory_contention_m port 3
else
x.check_memory_contention port
tstates <- tstates + 3
if port &&& 0x00FFus = 0xFEus then
keyboard.read port
else
0xFFuy
member x.read_from_memory (addr: uint16) =
x.check_memory_contention addr
tstates <- tstates + MEMORY_READ_TIME
memory.[(int)addr]
member x.write_to_memory (addr: uint16) (data: byte) =
x.check_memory_contention addr
tstates <- tstates + MEMORY_WRITE_TIME
memory.[(int)addr] <- data
member x.increment_memory_refresh_register =
let r = ir &&& 0x00FFus
let r = (r &&& 0b10000000us) ||| ((r + 1us) &&& 0b1111111us)
ir <- (ir &&& 0xFF00us) ||| r
member x.get_compound (r1: byte) (r2: byte) = (uint16 r1 <<< 8) ||| (uint16 r2)
member x.push_to_stack (v: uint16) =
sp <- sp - 1us
x.write_to_memory sp (byte (v >>> 8))
sp <- sp - 1us
x.write_to_memory sp (byte v)
member x.pop_from_stack =
let addr1 = x.read_from_memory sp
sp <- sp + 1us
let addr2 = x.read_from_memory sp
sp <- sp + 1us
x.get_compound addr2 addr1
member x.interrupt =
if halt then
halt <- false
pc <- pc + 1us
tstates <- tstates + 7
x.increment_memory_refresh_register
iff1 <- false
iff2 <- false
x.push_to_stack pc
if im = 2 then
let i = (ir &&& 0xFF00us) >>> 8
let p = i <<< 8 ||| 0xFFus
let v1 = x.read_from_memory p
let v2 = x.read_from_memory (p + 1us)
let v = x.get_compound v2 v1
pc <- v
else
pc <- 0x38us
member x.read_next_instruction =
x.check_memory_contention pc
tstates <- tstates + OP_FETCH_TIME
memory.[(int)pc]
member public x.execute_next_instruction() =
x.increment_memory_refresh_register
let op = x.read_next_instruction
pc <- pc + 1us
operations.[(int)op]()
member x.execute utstates =
while tstates < utstates do
if intr && iff1 && (not ei) then
x.interrupt
x.execute_next_instruction()
if ei then ei <- false
member x.execute_frame =
if tstates < 32 then
intr <- true
x.execute 32
intr <- false
x.execute(FRAME_CYCLES_TOTAL)
tstates <- tstates % FRAME_CYCLES_TOTAL
frames <- frames + 1us
memory
member x.initializeOperations =
operations.[0x00] <- fun () -> ()
operations.[0x07] <- fun () ->
let cf = if (register.[A] &&& 0x80uy) <> 0uy then C_FLAG else 0uy
register.[A] <- (register.[A] <<< 1) ||| cf
register.[F] <- (register.[F] &&& SZP_FLAGS) ||| (register.[A] &&& X53_FLAGS) ||| cf
operations.[0x08] <- fun () ->
x.exchange_registers A AS
x.exchange_registers F FS
operations.[0x0F] <- fun () ->
let cf = register.[A] &&& 0x01uy
register.[A] <- (register.[A] >>> 1) ||| (cf <<< 7)
register.[F] <- (register.[F] &&& SZP_FLAGS) ||| (register.[A] &&& X53_FLAGS) ||| cf
operations.[0x10] <- fun () ->
x.check_memory_contention_m ir 1
register.[B] <- (register.[B] - 1uy)
if register.[B] <> 0uy then
let offset = x.get_signed_byte(x.read_from_memory pc)
x.check_memory_contention_m pc 5
pc <- uint16 (int pc + offset + 1)
else
pc <- pc + 1us
operations.[0x16] <- fun () ->
register.[D] <- x.read_from_memory pc
pc <- pc + 1us
operations.[0x17] <- fun () ->
let cf = if (register.[A] &&& 0x80uy) <> 0uy then C_FLAG else 0uy
register.[A] <- ((register.[A] <<< 1) &&& 0xFEuy) ||| (register.[F] &&& C_FLAG)
register.[F] <- (register.[F] &&& SZP_FLAGS) ||| (register.[A] &&& X53_FLAGS) ||| cf
operations.[0x18] <- fun () ->
let offset = x.get_signed_byte(x.read_from_memory pc)
x.check_memory_contention_m pc 5
pc <- uint16 (int pc + offset + 1)
operations.[0x1F] <- fun () ->
let cf = if register.[A] &&& 0x01uy <> 0uy then C_FLAG else 0uy
register.[A] <- ((register.[A] >>> 1) &&& MASK_8) ||| ((register.[F] &&& C_FLAG) <<< 7)
register.[F] <- (register.[F] &&& SZP_FLAGS) ||| (register.[A] &&& X53_FLAGS) ||| cf
operations.[0x22] <- fun () ->
let offset = x.read_address_pointing_by_pc
x.write_to_memory offset register.[L]
x.write_to_memory (offset + 1us) register.[H]
operations.[0x27] <- fun () ->
let cf = register.[F] &&& C_FLAG
let correction = if (register.[F] &&& C_FLAG <> 0uy) || (register.[A] > 0x99uy)
then 0x60uy else 0x00uy
let correction = correction |||
(if ((register.[F] &&& H_FLAG <> 0uy) ||
((register.[A] &&& 0x0Fuy) > 0x09uy))
then 0x06uy else 0x00uy)
let cf = if register.[A] > 0x99uy then C_FLAG else cf
if register.[F] &&& N_FLAG <> 0uy then
x.sub_from_register A correction
register.[F] <- sz53pn_sub.[int register.[A]] ||| (register.[F] &&& H_FLAG) ||| cf
else
x.add_to_register A correction
register.[F] <- sz53pn_add.[int register.[A]] ||| (register.[F] &&& H_FLAG) ||| cf
operations.[0x2A] <- fun () ->
let offset = x.read_address_pointing_by_pc
register.[L] <- x.read_from_memory offset
register.[H] <- x.read_from_memory (offset + 1us)
operations.[0x2F] <- fun () ->
register.[A] <- register.[A] ^^^ MASK_8
register.[F] <- (register.[F] &&& SZP_FLAGS) |||
H_FLAG |||
(register.[A] &&& X53_FLAGS) |||
N_FLAG |||
(register.[F] &&& C_FLAG)
operations.[0x31] <- fun () ->
sp <- x.read_address_pointing_by_pc
operations.[0x32] <- fun () ->
x.write_to_memory x.read_address_pointing_by_pc register.[A]
operations.[0x36] <- fun () ->
x.write_to_memory (x.get_register16 H L) (x.read_from_memory pc)
pc <- pc + 1us
operations.[0x37] <- fun () ->
register.[F] <- (register.[F] &&& SZP_FLAGS) |||
(register.[A] &&& X53_FLAGS) ||| C_FLAG
operations.[0x3A] <- fun () ->
let offset = x.read_address_pointing_by_pc
register.[A] <- x.read_from_memory offset
operations.[0x3F] <- fun () ->
register.[F] <- (register.[F] &&& SZP_FLAGS) |||
(register.[A] &&& X53_FLAGS) |||
(if (register.[F] &&& C_FLAG) <> 0uy then H_FLAG else C_FLAG)
operations.[0x76] <- fun () ->
halt <- true
pc <- pc - 1us
operations.[0xC3] <- fun () ->
pc <- x.read_address_pointing_by_pc
operations.[0xC9] <- fun () ->
pc <- x.pop_from_stack
operations.[0xCB] <- fun () ->
x.increment_memory_refresh_register
let op = int x.read_next_instruction
pc <- pc + 1us
operations_bits.[op]()
operations.[0xCD] <- fun () ->
let offset = x.read_address_pointing_by_pc
x.check_memory_contention_m (pc - 1us) 1
x.push_to_stack pc
pc <- offset
operations.[0xD3] <- fun () ->
let value = x.read_from_memory pc
x.write_to_port (x.get_compound register.[A] value) register.[A]
pc <- pc + 1us
operations.[0xD9] <- fun () ->
x.exchange_registers B BS
x.exchange_registers C CS
x.exchange_registers D DS
x.exchange_registers E ES
x.exchange_registers H HS
x.exchange_registers L LS
operations.[0xDB] <- fun () ->
register.[A] <- x.read_from_port (x.get_compound register.[A] (x.read_from_memory pc))
pc <- pc + 1us
operations.[0xDD] <- fun () ->
x.increment_memory_refresh_register
let op = int x.read_next_instruction
pc <- pc + 1us
operations_indexes.[op] IX
operations.[0xE3] <- fun () ->
let curr_h = register.[H]
let curr_l = register.[L]
register.[L] <- x.read_from_memory sp
register.[H] <- x.read_from_memory (sp + 1us)
x.check_memory_contention_m (sp + 1us) 1
x.write_to_memory sp curr_l
x.write_to_memory (sp + 1us) curr_h
x.check_memory_contention_m sp 2
operations.[0xE9] <- fun () ->
pc <- x.get_register16 H L
operations.[0xEB] <- fun () ->
x.exchange_registers D H
x.exchange_registers E L
operations.[0xED] <- fun () ->
x.increment_memory_refresh_register
let op = int x.read_next_instruction
pc <- pc + 1us
operations_extended.[op]()
operations.[0xF3] <- fun () ->
iff1 <- false
iff2 <- false
operations.[0xF9] <- fun () ->
x.check_memory_contention_m ir 2
sp <- x.get_register16 H L
operations.[0xFB] <- fun () ->
ei <- true
iff1 <- true
iff2 <- true
operations.[0xFD] <- fun () ->
x.increment_memory_refresh_register
let op = int x.read_next_instruction
pc <- pc + 1us
operations_indexes.[op] IY
[(0x01, [B; C]); (0x11, [D; E]); (0x21, [H; L])] |> List.iter
(fun c ->
match c with
| (op, reg) ->
operations.[op] <- fun () ->
reg |> List.rev |> List.iter (fun reg_half ->
register.[reg_half] <- x.read_from_memory pc
pc <- pc + 1us))
[(0x02, [B; C]); (0x12, [D; E])] |> List.iter
(fun c ->
match c with
| (op, [reg1; reg2]) ->
operations.[op] <- fun () ->
x.write_to_memory (x.get_register16 reg1 reg2) register.[A]
| (_, _) -> ()
)
[(0x33, +1); (0x3B, -1)] |> List.iter (fun c ->
match c with
| (op, offset) -> operations.[op] <- fun () ->
x.check_memory_contention_m ir 2
sp <- uint16 (int sp + offset))
[
(0x03, [B;C], +1)
(0x13, [D;E], +1)
(0x23, [H;L], +1)
(0x0B, [B;C], -1)
(0x1B, [D;E], -1)
(0x2B, [H;L], -1)
] |> List.iter (fun c ->
match c with
| (op, [reg1; reg2], offset) ->
operations.[op] <- fun () ->
x.check_memory_contention_m ir 2
let r = uint16 (int (x.get_register16 reg1 reg2) + offset)
register.[reg1] <- byte (r >>> 8)
register.[reg2] <- byte r
| (_, _, _) -> ()
)
let get_ops_with_regs ops regs = List.zip ops regs
let get_ops ops regs f = (get_ops_with_regs ops regs) |> List.map (fun (a, b) -> (a, b, f))
let ops1 = get_ops (List.append [0x04 .. 0x08 .. 0x2C] [0x3C]) regs x.inc_reg_8
let ops2 = get_ops (List.append [0x05 .. 0x08 .. 0x2D] [0x3D]) regs x.dec_reg_8
List.append ops1 ops2 |> List.iter (fun c ->
match c with
| (op, reg, f) ->
operations.[op] <- fun () -> f reg
)
get_ops_with_regs (List.append [0x06 .. 0x08 .. 0x2E] [0x3E]) regs |> List.iter (fun c ->
match c with
| (op, reg) ->
operations.[op] <- fun () ->
register.[reg] <- x.read_from_memory pc
pc <- pc + 1us
)
[(0x09, [B;C]); (0x19, [D;E]); (0x29, [H;L]); (0x39, [])] |> List.iter (fun c ->
match c with
| (op, [reg1; reg2]) ->
operations.[op] <- fun () ->
x.check_memory_contention_m ir 7
x.add_to_register16 H L (x.get_register16 reg1 reg2)
| (op, _) ->
operations.[op] <- fun () ->
x.check_memory_contention_m ir 7
x.add_to_register16 H L sp
)
[
(0x20, (Z_FLAG, false))
(0x30, (C_FLAG, false))
(0x28, (Z_FLAG, true))
(0x38, (C_FLAG, true))
] |> List.iter (fun c->
match c with
| (op, (flag, exp_flag_value)) ->
operations.[op] <- fun () ->
let offset = x.get_signed_byte (x.read_from_memory pc)
let flag_value = register.[F] &&& flag > 0uy
if flag_value = exp_flag_value then
x.check_memory_contention_m pc 5
pc <- uint16 (int pc + offset + 1)
else
pc <- pc + 1us
)
[
(0x34, x.inc_with_flags8)
(0x35, x.dec_with_flags8)
] |> List.iter (fun c ->
match c with
| (op, f) ->
operations.[op] <- fun () ->
let hl = x.get_register16 H L
let value = f (x.read_from_memory hl)
x.check_memory_contention_m hl 1
x.write_to_memory hl value
)
[
(0x0A, [B;C])
(0x1A, [D;E])
] |> List.iter (fun c ->
match c with
|(op, [reg1; reg2]) ->
operations.[op] <- fun () ->
register.[A] <- x.read_from_memory (x.get_register16 reg1 reg2)
|(_, _) -> ()
)
[
([0x40..0x47], B)
([0x48..0x4F], C)
([0x50..0x57], D)
([0x58..0x5F], E)
([0x60..0x67], H)
([0x68..0x6F], L)
([0x78..0x7F], A)
] |> List.iter (fun c ->
match c with
| (ops, dest_reg) ->
List.zip ops [B;C;D;E;H;L;-1;A] |> List.iter (fun y ->
match y with
| (op, src_reg) ->
operations.[op] <-
if src_reg <> -1 then
fun () -> register.[dest_reg] <- register.[src_reg]
else
fun () -> register.[dest_reg] <- x.read_from_memory (x.get_register16 H L)
)
)
List.zip (List.append [0x70..0x75] [0x77]) regs |> List.iter (fun c ->
match c with
|(op, src_reg) ->
operations.[op] <- fun () ->
x.write_to_memory (x.get_register16 H L) register.[src_reg]
)
[
([0x80..0x87], x.add_to_register)
([0x88..0x8F], x.add_to_register_with_carry)
([0x90..0x97], x.sub_from_register)
([0x98..0x9F], x.sub_from_register_with_carry)
([0xa0..0xA7], x.and_register)
([0xa8..0xAF], x.xor_register)
([0xb0..0xB7], x.or_register)
([0xb8..0xBF], x.cp_to_register)
] |> List.iter (fun c ->
match c with
| (ops, f) ->
List.zip ops [B;C;D;E;H;L;-1;A] |> List.iter (fun y ->
match y with
| (op, src_reg) ->
operations.[op] <-
if src_reg <> -1 then
fun () -> f A register.[src_reg]
else
fun () -> f A (x.read_from_memory (x.get_register16 H L))
)
)
[
(0xC0, (Z_FLAG, false))
(0xD0, (C_FLAG, false))
(0xE0, (P_FLAG, false))
(0xF0, (S_FLAG, false))
(0xC8, (Z_FLAG, true))
(0xD8, (C_FLAG, true))
(0xE8, (P_FLAG, true))
(0xF8, (S_FLAG, true))
] |> List.iter (fun c ->
match c with
| (op, (flag, exp_flag_value)) ->
operations.[op] <- fun () ->
x.check_memory_contention_m ir 1
let flag_value = register.[F] &&& flag > 0uy
if flag_value = exp_flag_value then
pc <- x.pop_from_stack
)
[
(0xC1,[B;C])
(0xD1,[D;E])
(0xE1,[H;L])
(0xF1,[A;F])
] |> List.iter (fun c ->
match c with
|(op, [reg1;reg2]) ->
operations.[op] <- fun () ->
let v = x.pop_from_stack
register.[reg1] <- byte (v >>> 8)
register.[reg2] <- byte v
| (_, _) -> ()
)
[
(0xC2,(Z_FLAG, false))
(0xD2,(C_FLAG, false))
(0xE2,(P_FLAG, false))
(0xF2,(S_FLAG, false))
(0xCA,(Z_FLAG, true))
(0xDA,(C_FLAG, true))
(0xEA,(P_FLAG, true))
(0xFA,(S_FLAG, true))
] |> List.iter (fun c ->
match c with
| (op, (flag, exp_flag_value)) ->
operations.[op] <- fun () ->
let offset1 = x.read_from_memory pc
pc <- pc + 1us
let offset2 = x.read_from_memory pc
pc <- pc + 1us
let offset = x.get_compound offset2 offset1
let flag_value = register.[F] &&& flag > 0uy
if flag_value = exp_flag_value then
pc <- offset
)
[
(0xC4,(Z_FLAG, false))
(0xD4,(C_FLAG, false))
(0xE4,(P_FLAG, false))
(0xF4,(S_FLAG, false))
(0xCC,(Z_FLAG, true))
(0xDC,(C_FLAG, true))
(0xEC,(P_FLAG, true))
(0xFC,(S_FLAG, true))
] |> List.iter (fun c ->
match c with
| (op, (flag, exp_flag_value)) ->
operations.[op] <- fun () ->
let offset1 = x.read_from_memory pc
let offset2 = x.read_from_memory (pc + 1us)
let offset = x.get_compound offset2 offset1
let flag_value = register.[F] &&& flag > 0uy
if flag_value = exp_flag_value then
pc <- pc + 1us
x.check_memory_contention_m pc 1
pc <- pc + 1us
x.push_to_stack pc
pc <- offset
else
pc <- pc + 2us
)
[
(0xC5, [B;C])
(0xD5, [D;E])
(0xE5, [H;L])
(0xF5, [A;F])
] |> List.iter (fun c ->
match c with
| (op, [reg1;reg2]) ->
operations.[op] <- fun () ->
x.check_memory_contention_m ir 1
x.push_to_stack (x.get_register16 reg1 reg2)
| (_, _) -> ()
)