-
Notifications
You must be signed in to change notification settings - Fork 0
/
moro8.h.cmake
1369 lines (1279 loc) · 35.1 KB
/
moro8.h.cmake
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
#ifndef MORO8__h
#define MORO8__h
#ifdef __cplusplus
extern "C"
{
#endif
#include <stddef.h>
/** Major version of moro8. */
#define MORO8_VERSION_MAJOR @MORO8_VERSION_MAJOR@
/** Minor version of moro8. */
#define MORO8_VERSION_MINOR @MORO8_VERSION_MINOR@
/** Patch version of moro8. */
#define MORO8_VERSION_PATCH @MORO8_VERSION_PATCH@
/* Define to 1 if you have the <stdio.h> header file. */
#ifndef HAVE_STDIO_H
#cmakedefine HAVE_STDIO_H 1
#endif
/* Define to 1 if you have the <stdlib.h> header file. */
#ifndef HAVE_STDLIB_H
#cmakedefine HAVE_STDLIB_H 1
#endif
/* Define to 1 if you have the <string.h> header file. */
#ifndef HAVE_STRING_H
#cmakedefine HAVE_STRING_H 1
#endif
/* Define to 1 if you have the `free' function. */
#ifndef HAVE_FREE
#cmakedefine HAVE_FREE 1
#endif
/* Define to 1 if you have the `malloc' function. */
#ifndef HAVE_MALLOC
#cmakedefine HAVE_MALLOC 1
#endif
#ifndef MORO8_DOXYGEN
/* Builds Doxygen doc. */
#cmakedefine MORO8_DOXYGEN 1
#endif
#ifndef MORO8_MINIMALIST
/**
* Strips extra features from moro8.
*
* This implicitly sets MORO8_WITH_PARSER=0 and MORO8_WITH_HANDLERS=0.
*/
#cmakedefine MORO8_MINIMALIST 1
#endif
#ifndef MORO8_EXTENDED_OPCODES
/**
* Includes extended opcodes specific to moro8.
*/
#cmakedefine MORO8_EXTENDED_OPCODES 1
#endif
#ifndef MORO8_WITH_PARSER
/**
* Builds moro8 with support for parser.
*
* @note
* Parser is not available when MORO8_MINIMALIST is defined.
*
*/
#cmakedefine MORO8_WITH_PARSER 1
#endif
#ifndef MORO8_WITH_HANDLERS
/**
* Builds moro8 with support for custom opcodes.
*
* @note
* Handlers are not available when MORO8_MINIMALIST is defined.
*
*/
#cmakedefine MORO8_WITH_HANDLERS 1
#endif
#ifndef MORO8_MALLOC
#ifdef HAVE_MALLOC
/**
* Defines the malloc function used by moro8 at compile time.
*
* @code
* void* my_malloc(size_t size)
* {
* // do something
* }
*
* #define MORO8_MALLOC my_malloc
* @endcode
*/
#define MORO8_MALLOC malloc
#else
#define MORO8_MALLOC(size) NULL
#endif
#endif
#ifndef MORO8_FREE
#ifdef HAVE_FREE
/**
* Defines the free function used by moro8 at compile time.
*
* @code
* void my_free(void* ptr)
* {
* // do something
* }
*
* #define MORO8_FREE my_free
* @endcode
*/
#define MORO8_FREE free
#else
#define MORO8_FREE(ptr)
#endif
#endif
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
#define __WINDOWS__
#endif
#ifdef __WINDOWS__
#define MORO8_CDECL __cdecl
#define MORO8_STDCALL __stdcall
/* export symbols by default, this is necessary for copy pasting the C and header file */
#if !defined(MORO8_HIDE_SYMBOLS) && !defined(MORO8_IMPORT_SYMBOLS) && !defined(MORO8_EXPORT_SYMBOLS)
#define MORO8_EXPORT_SYMBOLS
#endif
#if defined(MORO8_HIDE_SYMBOLS)
#define MORO8_PUBLIC(type) type MORO8_STDCALL
#elif defined(MORO8_EXPORT_SYMBOLS)
#define MORO8_PUBLIC(type) __declspec(dllexport) type MORO8_STDCALL
#elif defined(MORO8_IMPORT_SYMBOLS)
#define MORO8_PUBLIC(type) __declspec(dllimport) type MORO8_STDCALL
#endif
#else /* !__WINDOWS__ */
#define MORO8_CDECL
#define MORO8_STDCALL
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
#define MORO8_PUBLIC(type) __attribute__((visibility("default"))) type
#else
#define MORO8_PUBLIC(type) type
#endif
#endif
/** Type for an unsigned single byte (8-bit). */
typedef unsigned char moro8_uword;
/** Type for an unsigned double byte (16-bit). */
typedef unsigned short moro8_udword;
/** Type for a signed single byte (8-bit). */
typedef char moro8_word;
/** Type for a signed double byte (16-bit). */
typedef short moro8_dword;
/**
* Total size of the memory allocated for the cpu: 0x10000.
*
* With a double word (16-bit) address, we can address up to 65536 bytes.
*/
#define MORO8_MEMORY_SIZE 0x10000
/**
* Total size of a single page in memory: 0x100.
*
* With a single word (8-bit) address, we can address up to 256 bytes.
*/
#define MORO8_PAGE_SIZE 0x100
/**
* Starting address of the stack in memory: 0x100.
*
* The stack starts after the zero page, thus at address 0x100.
*/
#define MORO8_STACK_OFFSET 0x100
/**
* Total size of the stack in memory: 0x100.
*
* As the stack register is a single word (8-bit), we can address up to 256 bytes.
*/
#define MORO8_STACK_SIZE 0x100
/**
* Total size of the ROM section in memory: 0x8000.
*
* This is an arbitrary value but must be big enough to contain the program.
*/
#define MORO8_ROM_SIZE 0x8000
/**
* Starting address of the ROM section in memory: MORO8_MEMORY_SIZE - MORO8_ROM_SIZE.
*
* The ROM section is always at the high end of memory.
*/
#define MORO8_ROM_OFFSET (MORO8_MEMORY_SIZE - MORO8_ROM_SIZE)
/**
* Offset of the RAM section in memory: 0x200.
*
* The RAM is always at the low end of memory, right after the stack.
*/
#define MORO8_RAM_OFFSET 0x200
/**
* Total size of the RAM section in memory: MORO8_ROM_OFFSET - MORO8_RAM_OFFSET.
*
* This is all memory that is no reserved by the CPU and is not ROM.
*/
#define MORO8_RAM_SIZE (MORO8_ROM_OFFSET - MORO8_RAM_OFFSET)
/**
* Address of the RES (Reset) Vector in memory: 0xFFFC.
*
* This points to the first address of the program in memory.
*/
#define MORO8_RES_VECTOR_ADDRESS 0xFFFC
/** Address of the IRQ (Interrupt Request) Vector in memory: 0xFFFE. */
#define MORO8_IRQ_VECTOR_ADDRESS 0xFFFE
struct moro8_cpu;
#if MORO8_WITH_HANDLERS
/**
* Signature of a handler function for handling opcodes.
*
* @note
* Available only if built with MORO8_WITH_HANDLERS=1
*
* @param[in] cpu CPU instance
* @param[in] op Opcode to handle
* @return If the instruction was handled.
*/
typedef int (*moro8_handler)(struct moro8_cpu *, moro8_uword op);
#endif
/** Struct for custom hooks configuration. */
struct moro8_hooks
{
/** Custom malloc function. */
void *(*malloc_fn)(size_t size);
/** Custom free function. */
void (*free_fn)(void *ptr);
};
/** Struct containing registers used by the CPU. */
struct moro8_registers
{
/** Program counter. */
moro8_udword pc;
/** Accumulator: math register. */
moro8_uword ac;
/** X: index registers. */
moro8_uword x;
/** Y: index registers. */
moro8_uword y;
/** Status register. */
struct sr
{
/** Carry. */
moro8_uword c : 1;
/** Zero. */
moro8_uword z : 1;
/** Interrupt. */
moro8_uword i : 1;
/** Decimal (not implemented). */
moro8_uword d : 1;
/** Break (not used). */
moro8_uword b : 1;
/** Ignored (not used). */
moro8_uword ignored : 1;
/** Overflow. */
moro8_uword v : 1;
/** Negative. */
moro8_uword n : 1;
} sr;
/** Stack pointer. */
moro8_uword sp;
};
/**
* Struct representing a bus allowing the CPU to read and write memory.
*
* You can imagine it as a physical bus connected to the CPU and the memory,
* allowing the CPU to read and write memory.
*/
struct moro8_bus
{
/**
* Gets size bytes from memory starting at offset.
* @param[in] bus Some moro8_bus instance
* @param[in] buffer Pointer to buffer for storing the bytes
* @param[in] offset Starting offset in memory
* @param[in] size Size of buffer
* @return The number of copied bytes.
*/
moro8_udword (*get)(const struct moro8_bus *bus, moro8_uword *buffer, moro8_udword offset, moro8_udword size);
/**
* Sets size bytes to memory starting at offset.
* @param[in] bus Some moro8_bus instance
* @param[in] buffer Pointer to buffer for storing the bytes
* @param[in] offset Starting offset in memory
* @param[in] size Size of buffer
* @return The number of copied bytes.
*/
moro8_udword (*set)(struct moro8_bus *bus, const moro8_uword *buffer, moro8_udword offset, moro8_udword size);
/**
* Gets a single word from memory.
* @param[in] bus Some moro8_bus instance
* @param[in] address Absolute address
* @return Word at requested address.
*/
moro8_uword (*get_word)(const struct moro8_bus *bus, moro8_udword address);
/**
* Sets a single word to memory.
* @param[in] bus Some moro8_bus instance
* @param[in] address Absolute address
* @param[in] value New value
*/
void (*set_word)(struct moro8_bus *bus, moro8_udword address, moro8_uword value);
#if !MORO8_MINIMALIST
/**
* Gets a double word from memory.
*
* @note
* Available only if built with MORO8_MINIMALIST=0
* @param[in] bus Some moro8_bus instance
* @param[in] address Absolute address
* @return Double word at requested address.
*/
moro8_udword (*get_dword)(const struct moro8_bus *bus, moro8_udword address);
/**
* Sets a double word to memory.
*
* @note
* Available only if built with MORO8_MINIMALIST=0
* @param[in] bus Some moro8_bus instance
* @param[in] address Absolute address
* @param[in] value New value
*/
void (*set_dword)(struct moro8_bus *bus, moro8_udword address, moro8_udword value);
#endif
/**
* Resets the memory.
* @param[in] bus Some moro8_bus instance
*/
void (*reset)(struct moro8_bus *bus);
};
/** Struct for the cpu emulating the moro8 CPU. */
struct moro8_cpu
{
/** Registers used by the CPU. */
struct moro8_registers registers;
/** Bus connecting the CPU to memory. */
struct moro8_bus *memory;
#if MORO8_WITH_HANDLERS
/**
* Registered handlers for custom handling of opcodes.
* @note
* Available only if built with MORO8_WITH_HANDLERS=1
*/
moro8_handler handlers[0x100];
#endif
};
/**
* The required size for a buffer passed to moro8_print not
* including the null-terminating character.
*
* Fortunately, the size of our cpu and the number of required
* bytes for printing its state are well known at compile time.
*/
#define MORO8_PRINT_BUFFER_SIZE 233482
/**
* Same as MORO8_PRINT_BUFFER_SIZE but for printing the
* header part only.
*/
#define MORO8_PRINT_HEADER_BUFFER_SIZE 65
enum moro8_opcode
{
MORO8_OP_ADC_IMM = 0x69,
MORO8_OP_ADC_ZP = 0x65,
MORO8_OP_ADC_ZP_X = 0x75,
MORO8_OP_ADC_ABS = 0x6D,
MORO8_OP_ADC_ABS_X = 0x7D,
MORO8_OP_ADC_ABS_Y = 0x79,
MORO8_OP_ADC_IND_X = 0x61,
MORO8_OP_ADC_IND_Y = 0x71,
MORO8_OP_AND_IMM = 0x29,
MORO8_OP_AND_ZP = 0x25,
MORO8_OP_AND_ZP_X = 0x35,
MORO8_OP_AND_ABS = 0x2D,
MORO8_OP_AND_ABS_X = 0x3D,
MORO8_OP_AND_ABS_Y = 0x39,
MORO8_OP_AND_IND_X = 0x21,
MORO8_OP_AND_IND_Y = 0x31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_OP_ROL_AC = 0x2A,
MORO8_OP_ROL_ZP = 0x26,
MORO8_OP_ROL_ZP_X = 0x36,
MORO8_OP_ROL_ABS = 0x2E,
MORO8_OP_ROL_ABS_X = 0x3E,
MORO8_OP_ROR_AC = 0x6A,
MORO8_OP_ROR_ZP = 0x66,
MORO8_OP_ROR_ZP_X = 0x76,
MORO8_OP_ROR_ABS = 0x6E,
MORO8_OP_ROR_ABS_X = 0x7E,
MORO8_OP_RTS = 0x60,
MORO8_OP_SBC_IMM = 0xE9,
MORO8_OP_SBC_ZP = 0xE5,
MORO8_OP_SBC_ZP_X = 0xF5,
MORO8_OP_SBC_ABS = 0xED,
MORO8_OP_SBC_ABS_X = 0xFD,
MORO8_OP_SBC_ABS_Y = 0xF9,
MORO8_OP_SBC_IND_X = 0xE1,
MORO8_OP_SBC_IND_Y = 0xF1,
MORO8_OP_SEC = 0x38,
MORO8_OP_SED = 0xF8,
MORO8_OP_STA_ZP = 0x85,
MORO8_OP_STA_ZP_X = 0x95,
MORO8_OP_STA_ABS = 0x8D,
MORO8_OP_STA_ABS_X = 0x9D,
MORO8_OP_STA_ABS_Y = 0x99,
MORO8_OP_STA_IND_X = 0x81,
MORO8_OP_STA_IND_Y = 0x91,
MORO8_OP_STX_ZP = 0x86,
MORO8_OP_STX_ZP_Y = 0x96,
MORO8_OP_STX_ABS = 0x8E,
MORO8_OP_STY_ZP = 0x84,
MORO8_OP_STY_ZP_X = 0x94,
MORO8_OP_STY_ABS = 0x8C,
MORO8_OP_TAX = 0xAA,
MORO8_OP_TAY = 0xA8,
MORO8_OP_TSX = 0xBA,
MORO8_OP_TXA = 0x8A,
MORO8_OP_TXS = 0x9A,
MORO8_OP_TYA = 0x98,
// Custom opcodes (non 6502)
MORO8_OP_DEA = 0x3A,
MORO8_OP_INA = 0x1A,
MORO8_OP_JMP_ABS_X = 0x7C,
#if MORO8_EXTENDED_OPCODES
MORO8_OP_LDA_SP = 0x02,
MORO8_OP_LDA_SP_X = 0x12,
MORO8_OP_LDX_SP = 0x22,
MORO8_OP_LDX_SP_Y = 0x32,
MORO8_OP_LDY_SP = 0x42,
MORO8_OP_LDY_SP_X = 0x52,
MORO8_OP_PHX = 0x62,
MORO8_OP_PHY = 0x72,
MORO8_OP_PLX = 0x82,
MORO8_OP_PLY = 0x92,
MORO8_OP_CMP_SP = 0xB2,
MORO8_OP_CMP_SP_X = 0xC2,
MORO8_OP_CPX_SP = 0xD2,
MORO8_OP_CPY_SP = 0xE2,
#endif
// Number of opcodes
MORO8_OP_MAX
};
enum moro8_register
{
/** Accumulator register. */
MORO8_REGISTER_AC,
/** Index register. */
MORO8_REGISTER_X,
/** Index register. */
MORO8_REGISTER_Y,
/** Status register. */
MORO8_REGISTER_SR,
/** Negative register. */
MORO8_REGISTER_N,
/** Overflow register. */
MORO8_REGISTER_V,
/** Zero register. */
MORO8_REGISTER_Z,
/** Carry register. */
MORO8_REGISTER_C,
/** Stack pointer. */
MORO8_REGISTER_SP
};
/**
* Register custom hooks.
*
* @code
* struct moro8_hooks hooks = { malloc, free };
* moro8_init_hooks(&hooks);
* @endcode
* @param[in] hooks Hooks configuration
*/
MORO8_PUBLIC(void)
moro8_init_hooks(struct moro8_hooks *hooks);
/**
* Allocates and initializes a new moro8_cpu instance.
* @code
* struct moro8_cpu* cpu = moro8_create();
*
* ...
*
* // Don't forget to free memory
* moro8_delete(cpu);
* @endcode
* @return Pointer to new cpu instance.
*/
MORO8_PUBLIC(struct moro8_cpu *)
moro8_create();
/**
* Initializes an already allocated moro8_cpu instance.
*
* This is called implicitly by moro8_create, but must be called
* manually otherwise.
* @code
* struct moro8_cpu cpu;
* moro8_init(&cpu);
*
* ...
* @endcode
* @param[in] cpu Some cpu instance
*/
MORO8_PUBLIC(void)
moro8_init(struct moro8_cpu *cpu);
/**
* Frees up memory allocated for a moro8_cpu instance.
* @code
* struct moro8_cpu* cpu = moro8_create();
*
* ...
*
* // Don't forget to free memory
* moro8_delete(cpu);
* @endcode
* @param[in] cpu Some cpu instance
*/
MORO8_PUBLIC(void)
moro8_delete(struct moro8_cpu *cpu);
/**
* Sets the bus for connecting the CPU to memory.
* @code
* struct moro8_cpu* cpu = moro8_create();
* struct moro8_array_memory* memory = moro8_array_memory_create();
*
* moro8_set_memory_bus(cpu, &memory->bus);
*
* // Disconnect
* moro8_set_memory_bus(cpu, NULL);
* @endcode
* @param[in] cpu Some cpu instance
* @param[in] bus Pointer to bus
*/
MORO8_PUBLIC(void)
moro8_set_memory_bus(struct moro8_cpu *cpu, struct moro8_bus *bus);
/**
* Gets the bus for connecting the CPU to memory.
* @param[in] cpu Some cpu instance
* @return Pointer to bus.
*/
MORO8_PUBLIC(struct moro8_bus *)
moro8_get_memory_bus(struct moro8_cpu *cpu);
/**
* Loads a program into the ROM section of memory.
*
* This also sets the program counter to the first address of loaded program.
* @param cpu cpu instance
* @param prog program address
* @param size program length
*/
MORO8_PUBLIC(void)
moro8_load(struct moro8_cpu *cpu, const moro8_uword *prog, moro8_udword size);
/**
* Executes the next instruction.
* @param cpu cpu instance
*/
MORO8_PUBLIC(size_t)
moro8_step(struct moro8_cpu *cpu);
/**
* Runs until the cpu halts.
* @param cpu cpu instance
*/
MORO8_PUBLIC(void)
moro8_run(struct moro8_cpu *cpu);
/**
* Gets the address stored in program counter.
* @param[in] cpu Some cpu
* @return Value in program counter.
*/
MORO8_PUBLIC(moro8_udword)
moro8_get_pc(const struct moro8_cpu *cpu);
/**
* Sets the address stored in program counter.
* @param[in] cpu Some cpu
* @param[in] address Absolute address
*/
MORO8_PUBLIC(void)
moro8_set_pc(struct moro8_cpu *cpu, moro8_udword address);
/**
* Gets the value stored in a register.
* @param[in] cpu Some cpu
* @param[in] reg Register
* @return Value in register.
*/
MORO8_PUBLIC(moro8_uword)
moro8_get_register(const struct moro8_cpu *cpu, enum moro8_register reg);
/**
* Sets the value stored in a register.
* @param[in] cpu Some cpu
* @param[in] reg Register
* @param[in] value New value
*/
MORO8_PUBLIC(void)
moro8_set_register(struct moro8_cpu *cpu, enum moro8_register reg, moro8_uword value);
/**
* Gets size bytes from memory starting at offset.
*
* @code
* moro8_uword buffer[4];
*
* moro8_get_memory(cpu, buffer, 0x1000, 4);
* @endcode
* @param[in] cpu Some cpu
* @param[in] buffer Pointer to buffer for storing the bytes
* @param[in] offset Starting offset in memory
* @param[in] size Size of buffer
* @return The number of copied bytes.
*/
MORO8_PUBLIC(moro8_udword)
moro8_get_memory(const struct moro8_cpu *cpu, moro8_uword *buffer, moro8_udword offset, moro8_udword size);
/**
* Fills in the memory starting at offset with size bytes.
*
* @code
* moro8_uword buffer[4] = { ... };
*
* moro8_set_memory(cpu, buffer, 0x1000, 4);
* @endcode
* @param[in] cpu Some cpu
* @param[in] buffer Pointer to buffer containing the bytes to copy
* @param[in] offset Starting offset in memory
* @param[in] size Size of buffer
* @return The number of copied bytes.
*/
MORO8_PUBLIC(moro8_udword)
moro8_set_memory(struct moro8_cpu *cpu, const moro8_uword *buffer, moro8_udword offset, moro8_udword size);
#if MORO8_WITH_HANDLERS
/**
* Gets the registered handler function for a specific opcode.
*
* @note
* Available only if built with MORO8_WITH_HANDLERS=1
*
* @param[in] cpu Some cpu
* @param[in] op Opcode number
* @return Registered handler function or NULL.
*/
MORO8_PUBLIC(moro8_handler)
moro8_get_handler(struct moro8_cpu *cpu, moro8_uword op);
/**
* Registers a moro8_handler for handling a specific opcode.
*
* Example of registering a function for printing the JMP instructions:
* @code
* int print_jmp(struct moro8_cpu* cpu, moro8_uword op) {
* printf("JMP");
*
* // Let the cpu handle this instruction
* return 0;
* }
*
* moro8_set_handler(cpu, MORO8_OP_JMP_ABS, print_jmp);
* @endcode
*
* Beware that it will replace already registered handler if any.
*
* @note
* Available only if built with MORO8_WITH_HANDLERS=1
*
* @param[in] cpu Some cpu
* @param[in] op Opcode number
* @param[in] handler Pointer to handler
*/
MORO8_PUBLIC(void)
moro8_set_handler(struct moro8_cpu *cpu, moro8_uword op, moro8_handler handler);
#endif
#if MORO8_WITH_PARSER
/**
* Parses a single [0-9A-F] character.
*
* @note
* Available only if built with MORO8_WITH_PARSER=1
*
* @param[in] value Character
* @return Corresponding value.
*/
MORO8_PUBLIC(moro8_uword)
moro8_parse_hex(char value);
/**
* Parses the next hexadecimal word.
*
* @note
* Available only if built with MORO8_WITH_PARSER=1
*
* @param[in] buf Some buffer
* @param[in] size Buffer size including the null-terminating character
* @param[out] value Parsed word
* @return Read characters or zero.
*/
MORO8_PUBLIC(size_t)
moro8_parse_word(const char* buf, size_t size, moro8_uword* value);
/**
* Parses the next hexadecimal double word.
*
* @note
* Available only if built with MORO8_WITH_PARSER=1
*
* @param[in] buf Some buffer
* @param[in] size Buffer size including the null-terminating character
* @param[out] value Parsed double word
* @return Read characters or zero.
*/
MORO8_PUBLIC(size_t)
moro8_parse_dword(const char* buf, size_t size, moro8_udword* value);
/**
* Dumps the current cpu state to an human-readable string.
*
* This works like snprintf for the cpu.
*
* The buffer must be at least MORO8_PRINT_BUFFER_SIZE bytes long to store
* the full string.
*
* If the resulting string would be longer than size-1 characters, the remaining
* characters are discarded and not stored, but counted for the value
* returned by the function.
*
* A terminating null character is automatically appended after the content written.
*
* @note
* Available only if built with MORO8_WITH_PARSER=1
*
* @param[in] cpu Some cpu
* @param[in] buf Pointer to a buffer for storing the result
* @param[in] size Buffer size including the null-terminating character
* @return The total number of bytes that would have been written, not counting
* the null-terminating character.
*/
MORO8_PUBLIC(size_t)
moro8_print(const struct moro8_cpu *cpu, char *buf, size_t size);
/**
* Parses the string representation of a cpu state.
*
* @note
* Available only if built with MORO8_WITH_PARSER=1
*
* @param[out] cpu Some cpu
* @param[in] buf Some buffer
* @param[in] size Buffer size
* @return The same cpu pointer if there is no errors, NULL otherwise.
*/
MORO8_PUBLIC(struct moro8_cpu *)
moro8_parse(struct moro8_cpu *cpu, const char *buf, size_t size);
#endif
#if !MORO8_MINIMALIST
/**
* Memory implementation using a continuous array of MORO8_MEMORY_SIZE bytes.
*
* This is suitable for running on systems with more than MORO8_MEMORY_SIZE
* available RAM.
*
* @note
* Available only if built with MORO8_MINIMALIST=0
*/
struct moro8_array_memory
{
/**
* The bus connected to this memory.
*
* You have to connect it to the CPU for it to work.
*/
struct moro8_bus bus;
/**
* A continuous array of MORO8_MEMORY_SIZE bytes allowing for fast read
* and write operations.
*/
moro8_uword buffer[MORO8_MEMORY_SIZE];
};
/**
* Allocates and initializes a new moro8_array_memory instance.
* @code
* struct moro8_array_memory* memory = moro8_array_memory_create();
*
* ...
*
* // Don't forget to free up memory
* moro8_array_memory_delete(memory);
* @endcode
*
* @note
* Available only if built with MORO8_MINIMALIST=0
* @return Pointer to new moro8_array_memory instance.
*/
MORO8_PUBLIC(struct moro8_array_memory *)
moro8_array_memory_create();
/**
* Initializes an already allocated moro8_array_memory instance.
* @code
* struct moro8_array_memory memory;
* moro8_array_memory_init(&memory);
* @endcode
*
* This initializes the bus correctly with:
* @code
* memory.bus.get = &moro8_array_memory_get;
* memory.bus.set = &moro8_array_memory_set;
* @endcode
*
* @note
* Available only if built with MORO8_MINIMALIST=0
* @param[in] memory Some moro8_array_memory instance
*/
MORO8_PUBLIC(void)
moro8_array_memory_init(struct moro8_array_memory *memory);
/**
* Frees up memory allocated for a moro8_array_memory instance.
* @code
* struct moro8_array_memory* memory = moro8_array_memory_create();
*
* ...
*
* // Don't forget to free up memory
* moro8_array_memory_delete(memory);
* @endcode
*
* @note
* Available only if built with MORO8_MINIMALIST=0
* @param[in] memory Some moro8_array_memory instance
*/
MORO8_PUBLIC(void)
moro8_array_memory_delete(struct moro8_array_memory *memory);
/**
* Gets size bytes from memory starting at offset.
*
* @code
* struct moro8_array_memory memory;
* moro8_array_memory_init(&memory);
*
* moro8_uword buffer[4];
*
* moro8_memory_get(&memory, buffer, 0x1000, 4);
* @endcode
*
* @note
* Available only if built with MORO8_MINIMALIST=0
* @param[in] memory Some moro8_bus instance
* @param[in] buffer Pointer to buffer for storing the bytes
* @param[in] offset Starting offset in memory
* @param[in] size Size of buffer
* @return The number of copied bytes.
*/
MORO8_PUBLIC(moro8_udword)
moro8_memory_get(const struct moro8_bus *memory, moro8_uword *buffer, moro8_udword offset, moro8_udword size);
/**
* Fills the memory with a buffer of bytes.
* @code