-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasm68k.c
More file actions
5209 lines (4696 loc) · 131 KB
/
asm68k.c
File metadata and controls
5209 lines (4696 loc) · 131 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
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
//
// Yet another 68000 assembler
//
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <alloca.h>
#include <stdlib.h>
#include <stdint.h>
//
// GLOBAL PROGRAM DEFINITIONS
// ==========================
//
//
// Define some helper values and types.
//
typedef int bool; // basic T/F value
typedef uint8_t byte; // 8 bit unsigned type
typedef uint16_t word; // 16 bit unsigned type
typedef uint32_t dword; // 32 bit unsigned type
//
// The following type is used for expression evaulation.
//
// While this can be just another 32 bit value, where possible
// this should be a signed type of greater than 32 bits. This is
// to facilitate the handling of signed and unsigned 32 bit values
// without ambiguity.
//
typedef long int number;
//
// Provide some printf format strings to generate correct and
// consistent output for each of the above define types.
//
#define BOOL_FORMAT "%d"
#define BYTE_FORMAT "%02X"
#define WORD_FORMAT "%04X"
#define DWORD_FORMAT "%08X"
#define NUMBER_FORMAT "%ld"
#define ERROR (-1)
#define TRUE (0==0)
#define FALSE (0==1)
#define EOS '\0'
#define ZERO '0'
#define NINE '9'
#define PERIOD '.'
#define PLUS '+'
#define MINUS '-'
#define OPAREN '('
#define CPAREN ')'
#define COMMA ','
#define DOLLAR '$'
#define PERCENT '%'
#define HASH '#'
#define COLON ':'
#define SEMICOLON ';'
#define ASTERIX '*'
#define SLASH '/'
#define UNDERSCORE '_'
#define QUOTE '\''
#define QUOTES '"'
#define ESCAPE '\\'
#define AMPERSAND '&'
#define BAR '|'
#define HAT '^'
#define BANG '!'
//
// Constants impacting program operation.
//
// BUFFER The limiting size of an input line.
//
// DIRECTIVE_ARGS The maximum number of arguments an assembler
// directive can have.
//
// INSTRUCTION_ARGS The maximum number of agruments an assembler
// instruction can have.
//
// MAXIMUM_VERIFICATIONS The maximum number of times the assembler
// tries to get the labels to settle.
//
#define BUFFER 200
#define DIRECTIVE_ARGS 8
#define INSTRUCTION_ARGS 2
#define MAXIMUM_VERIFICATIONS 4
//
// Syntactic sugar for memory allocations and function indirection.
//
#define NEW(t) ((t *)malloc(sizeof(t)))
#define STACK(t) ((t *)alloca(sizeof(t)))
#define STACK_N(t,n) ((t *)alloca(sizeof( t )*(n)))
#define SPACE(n) ((char *)alloca((n)))
#define FUNC(a) (*(a))
//
// Word splitting macros
//
#define H(v) (((v)>>8)&0xff)
#define L(v) ((v)&0xff)
//
// DWord splitting macros
// (for MSB ordering)
//
#define HW(v) (((v)>>16)&0xffff)
#define LW(v) ((v)&0xffff)
//
#define DW0(v) (((v)>>24)&0xff)
#define DW1(v) (((v)>>16)&0xff)
#define DW2(v) (((v)>>8)&0xff)
#define DW3(v) ((v)&0xff)
//
// Bitty stuff.
//
#define BIT(n) (1<<(n))
#define MASK(n) (BIT(n)-1)
//
// Declare two values that will help with the
// address managment.
//
#define FIRST_ADDRESS ((dword)0x00000000)
#define LAST_ADDRESS ((dword)0xffffffff)
//
// Numerical limits
// ================
//
#define MIN_S_BYTE ((number)-128)
#define MAX_S_BYTE ((number)127)
#define MIN_U_BYTE ((number)0)
#define MAX_U_BYTE ((number)255)
#define MIN_S_WORD ((number)-32768)
#define MAX_S_WORD ((number)32767)
#define MIN_U_WORD ((number)0)
#define MAX_U_WORD ((number)65535)
#define MIN_S_DWORD ((number)-2147483648)
#define MAX_S_DWORD ((number)2147483647)
#define MIN_U_DWORD ((number)0)
#define MAX_U_DWORD ((number)4294967295)
//
// Program Option Flags
// ====================
//
//
// Define a set of flags that are used to configure
// and control the assembler. These are OR'd together
// into the option_flags variable.
//
#define OPTIONS_NONE 000000
//
#define OPTION_68000 0000001
#define OPTION_68008 0000002
#define OPTION_68010 0000004
#define OPTION_68020 0000010
#define OPTION_68030 0000020
#define OPTION_68881 0000040
#define CPU_MASK (OPTION_68000|OPTION_68008|OPTION_68010|OPTION_68020|OPTION_68030)
//
#define DISPLAY_TEXT 0000100
#define DISPLAY_INTEL 0000200
#define DISPLAY_MOTOROLA 0000400
#define DISPLAY_NOTHING 0001000
#define DISPLAY_LISTING 0002000
#define DISPLAY_MASK (DISPLAY_TEXT|DISPLAY_INTEL|DISPLAY_MOTOROLA|DISPLAY_NOTHING|DISPLAY_LISTING)
#define DISPLAY_STDOUT 0004000
//
#define DISPLAY_SYMBOLS 0010000
#define DISPLAY_SECTIONS 0020000
#define DISPLAY___00400 0040000
//
#define DISPLAY_OPCODES 0100000
#define DISPLAY_HELP 0200000
#define DISPLAY_DEBUG 0400000
//
// This is where the flags are combined.
//
static int option_flags = OPTIONS_NONE;
//
// COMPILE TIME DEBUGGING
// ======================
//
// Define ENABLE_DEBUG to include options
// for including additional checking code
// and optional debugging output messages.
//
#ifdef ENABLE_DEBUG
//
// Define for debugging.
//
#define ASSERT(v) do{ if(!(v)) { fprintf( stderr, "Assert Failed \"%s\" (%s:%d:%s).\n", #v, __FILE__, __LINE__, __FUNCTION__ ); abort(); }}while(0)
#define PRINT(a) do{ if( option_flags & DISPLAY_DEBUG ) printf a; }while(0)
#else
//
// Define for not debugging.
//
#define ASSERT(v)
#define PRINT(a)
#endif
//
// The ABORT macro is always fully specified. What is
// the point of crashing without giving some indication
// of why?
//
#define ABORT(m) do{ fprintf( stderr, "Program Abort \"%s\" (%s:%d:%s).\n", (m), __FILE__, __LINE__, __FUNCTION__ ); abort(); }while(0)
//
// GLOBALLY STATIC PASS TRACKING
// =============================
//
// The assembler pass indicates which process through the
// source code the assembler is currently on.
//
typedef enum {
NOT_ASSEMBLING,
DATA_GATHERING,
DATA_VERIFICATION,
CODE_GENERATION
} pass_mode;
static pass_mode assembler_pass = NOT_ASSEMBLING;
//
// ASSEMBLY LANGUAGE CONSTRUCTS
// ============================
//
//
// Define enumeration to capture all of the possible
// addressing modes.
//
// Nomenclature:
//
// Dn Data register D0 .. D7
// An Address register A0 .. A7
// (?) Indirect memory access
// (?)+ Address register post increment after indirect access
// -(?) Address register pre-decrement before indirect access
// d8(?) 8 bit address displacement
// d16(?) 16 bit address displacement
// Rm.s Generic register as either Word or Long size.
// a16 Absolute 16 bit memory address
// a32 Absolute 32 bit memory address
// #i An immediate value in byte, word or long sizes
//
typedef enum {
NO_ARGUMENT = 000000000000, // Argument should be empty
//
// The Effective Address arguments
//
EA_DREG = 000000000001, // EA 000: Dn, Data Register Direct
EA_AREG = 000000000002, // EA 001: An, Address Register Direct
EA_AREG_IND = 000000000004, // EA 010: (An), Address Register Indirect
EA_AREG_IND_INC = 000000000010, // EA 011: (An)+, Address Register Indirect with post-increment
EA_AREG_DEC_IND = 000000000020, // EA 100: -(An), Address Register Indirect with pre-decrement
EA_AREG_IND_DISP = 000000000040, // EA 101: d16(An), Address Register Indirect with 16 bit displacement
EA_AREG_IND_DISP_REG = 000000000100, // EA 110: d8(An,Rm.s), Address Register Indirect with 8 bit displacement and Register offset
EA_ABS_SHORT_IND = 000000000200, // EA 111 000: a16, absolute indirect 16 bit address
EA_ABS_LONG_IND = 000000000400, // EA 111 001: a32, absolute indirect 32 bit address
EA_PC_IND_DISP = 000000001000, // EA 111 010: d16(PC), PC Indirect with 16 bit displacement
EA_PC_IND_DISP_REG = 000000002000, // EA 111 011: d8(PC,Rm.s), PC Indirect with 8 bit displacement and Register offset
EA_IMMEDIATE = 000000004000, // EA 111 100: #i, an immediate value in byte, word or long form (used as source only)
EA_STATUS_REG = 000000010000, // EA 111 100: SR, The status register (used as destination only)
EA_FLAGS_REG = 000000020000, // EA 111 100: CCR, The condition codes register (used as destination only)
//
// The 'specialist' arguments which are
// not classified as Effective Address
// arguments and are used in specific
// opcodes.
//
REGISTER_LIST = 000000040000, // A specific argument type for the MOVEM opcode.
ABS_ADDRESS = 000000100000, // Argument is an absolute address
ARG__UNUSED__ = 000000200000, // Unused argument flag
NUM_DREG = 000000400000, // Number: Dn, Data Register Direct
NUM_AREG = 000001000000, // Number: An, Address Register Direct
NUM_AREG_DEC_IND = 000002000000, // Number: -(An), Address Register Indirect with pre-decrement
NUM_AREG_IND_INC = 000004000000, // Number: (An)+, Address Register Indirect with post-increment
NUM_IMMEDIATE_3BIT = 000010000000, // Number: #i, An unsigned bit number from 0 to 7
NUM_IMMEDIATE_QUICK = 000020000000, // Number: #i, and unsigned number from 1 to 8
NUM_IMMEDIATE_5BIT = 000040000000, // Number: #i, An unsigned bit number from 0 to 31
NUM_IMMEDIATE_4 = 000100000000, // Number: #i, An unsigned number from 0 to 15
NUM_IMMEDIATE_8 = 000200000000, // Number: #i, a number (signed or unsigned) in the 8 bit range
NUM_IMMEDIATE_16 = 000400000000, // Number: #i, a number (signed or unsigned) in the 16 bit range
CONTROL_REG = 001000000000 // One of a set of CPU control registers
} arg_type;
//
// Define some common argument combinations that
// can be used by the opcodes data file.
//
#define EA_SOURCE_ARG (EA_DREG|EA_AREG|EA_AREG_IND|EA_AREG_IND_INC|EA_AREG_DEC_IND|EA_AREG_IND_DISP|EA_AREG_IND_DISP_REG|EA_ABS_SHORT_IND|EA_ABS_LONG_IND|EA_PC_IND_DISP|EA_PC_IND_DISP_REG|EA_IMMEDIATE)
#define EA_DEST_ARG (EA_DREG|EA_AREG_IND|EA_AREG_IND_INC|EA_AREG_DEC_IND|EA_AREG_IND_DISP|EA_AREG_IND_DISP_REG|EA_ABS_SHORT_IND|EA_ABS_LONG_IND)
#define EA_MEMORY_ARG (EA_AREG_IND|EA_AREG_IND_INC|EA_AREG_DEC_IND|EA_AREG_IND_DISP|EA_AREG_IND_DISP_REG|EA_ABS_SHORT_IND|EA_ABS_LONG_IND)
#define EA_ADDRESS_ARG (EA_AREG_IND|EA_AREG_IND_DISP|EA_AREG_IND_DISP_REG|EA_ABS_SHORT_IND|EA_ABS_LONG_IND|EA_PC_IND_DISP|EA_PC_IND_DISP_REG)
//
// EA combination especially for the MOVEM instruction
//
#define EA_MOVEM_ARG (EA_AREG_IND|EA_AREG_IND_DISP|EA_AREG_IND_DISP_REG|EA_ABS_SHORT_IND|EA_ABS_LONG_IND)
//
// If a parameter is appended after the opcode then
// its bit position is marked with the APPEND value
// telling the code to create additional opcode words.
//
#define APPEND (-1)
//
// If a supplied parameter is actually ignored, then
// mark out with IGNORE.
//
#define IGNORE (-2)
//
// Use this macro to specify a bit position in the
// word following the opcode. This is characterised
// as the auxiliary opcode in this program.
//
#define AUX(b) ((b)+16)
//
// Testing for above and extracting bit from above.
//
#define IS_AUX(b) ((b)>=16)
#define AUX_BIT(b) ((b)-16)
//
// Identifiers values will have some form of 'scope'
// which outlines the context in which their value
// has meaning.
//
typedef enum {
NO_SCOPE = 000000, // When a value has been used but
// nothing about what it might reflect
// can be inferred
SCOPE_S8 = 000001, // An 8 bit signed value
SCOPE_S16 = 000002, // A 16 bit signed value
SCOPE_S32 = 000004, // A full 32 bit signed value
SCOPE_U8 = 000010, // An 8 bit unsigned value
SCOPE_U16 = 000020, // A 16 bit unsigned value
SCOPE_U32 = 000040, // A full 32 bit unsigned value
SCOPE_ADRS = 000100, // An address value not associated
// with a specific section
SCOPE_TEXT = 001000, // An address in the TEXT section
SCOPE_DATA = 002000, // An address in the DATA section
SCOPE_BSS = 004000, // An address in the BSS section
//
// Scope data pertinent to symbols:
//
SCOPE_IMPORT = 010000,
SCOPE_EXPORT = 020000,
//
// Joint scope specifications:
//
SCOPE_NUMERIC = SCOPE_S8|SCOPE_S16|SCOPE_S32|SCOPE_U8|SCOPE_U16|SCOPE_U32,
SCOPE_ADDRESS = SCOPE_ADRS|SCOPE_TEXT|SCOPE_DATA|SCOPE_BSS
} scope;
//
// Opcodes are optionally (commonly) size modified. The
// following enumeration captures the sizing options.
//
// When no size indication is provided (and the instruction
// expects it) then Word is the assumed size.
//
typedef enum {
NO_SIZE = 00, // No size specification
//
// Individual size specifications..
//
SIZE_B = 01, // Byte 8 bit values
SIZE_W = 02, // Word 16 bit values
SIZE_L = 04, // Long 32 bit values
//
// Joint size specifications..
//
SIZE_WL = SIZE_W|SIZE_L,
SIZE_BWL = SIZE_B|SIZE_W|SIZE_L
} sizing;
//
// Define common sets of sizes required in the instruction set
//
//
// Return the size (in bytes) of the size provided. If multiple
// bits are set, returns biggest.
//
static int get_size( sizing z ) {
if( z & SIZE_L ) return( 4 );
if( z & SIZE_W ) return( 2 );
if( z & SIZE_B ) return( 1 );
return( 0 );
}
//
// provide basic scoping information on a sizing value.
//
static scope scope_size( sizing z ) {
scope s;
s = NO_SCOPE;
if( z & SIZE_L ) s |= ( SCOPE_S8 | SCOPE_U8 | SCOPE_S16 | SCOPE_U16 | SCOPE_S32 | SCOPE_U32 | SCOPE_ADRS );
if( z & SIZE_W ) s |= ( SCOPE_S8 | SCOPE_U8 | SCOPE_S16 | SCOPE_U16 );
if( z & SIZE_B ) s |= ( SCOPE_S8 | SCOPE_U8 );
return( s );
}
//
// provide basic scoping information on a dword value.
//
static scope scope_value( number val ) {
scope s;
s = 0;
if( val < 0 ) {
if( val >= MIN_S_BYTE ) s |= SCOPE_S8;
if( val >= MIN_S_WORD ) s |= SCOPE_S16;
if( val >= MIN_S_DWORD ) s |= SCOPE_S32;
}
else {
if( val <= MAX_S_BYTE ) s |= SCOPE_S8;
if( val <= MAX_U_BYTE ) s |= SCOPE_U8;
if( val <= MAX_S_WORD ) s |= SCOPE_S16;
if( val <= MAX_U_WORD ) s |= SCOPE_U16;
if( val <= MAX_S_DWORD ) s |= SCOPE_S32;
if( val <= MAX_U_DWORD ) s |= SCOPE_U32 | SCOPE_ADRS;
}
return( s );
}
//
// This is the enumeration capturing any specific opcode
// encoding actions that cannot captured in the table.
//
typedef enum {
NO_FLAGS = 00000, // Nothing extra required
FLAG_REV_ARG_1 = 00001, // Reverse bit order of argument 1
FLAG_COMP_SIZE = 00002, // The size bits must be complemented
FLAG_USP_ONLY = 00004, // Only the USP control register allowed
FLAG_8_16_REL = 00010, // Argument is an 8 or 16 bit PC relative address
FLAG_16_REL = 00020, // Argument is a 16 bit PC relative address
FLAG_68000 = 00040, // 68010 Instructions
FLAG_68010 = 00100, // 68010 Instructions
FLAG_68020 = 00200, // 68020 Instructions
FLAG_68030 = 00400, // 68030 Instructions
FLAG_68881 = 01000 // FPU Instructions
} opflags;
//
// Define TWO classes:
//
// o The first captures a specific instance (one of possibly a
// number) that an opcode mnemonic can be used. These linked
// together as a linked list.
//
// o The second to facilitate more rapid lookup/conversion from
// an opcode mnemonic to the list of description records which
// details the individual ways that mnemonic can be used. These
// formulated into binary tree.
//
//
typedef struct _opcode_ {
//
// Mnemonic variant:
//
sizing size; // Is opcode sized?
arg_type arg1, // The arguments to the opcode
arg2;
//
// Construction components.
//
word basecode, // The base 16 bit opcode
auxcode; // The start value for extended data
// This is only non zero for the larger
// opcodes
int sizebit, // bit position of size code if used
off1, // The bit offsets for the arguments
off2;
//
// Capture any special instructions that this opcode
// requires. This is normally 0, NO_FLAGS.
//
opflags flags;
//
// Next variant of this mnemonic.
//
struct _opcode_
*next;
} opcode;
typedef struct _mnemonic_ {
//
// The mnemonic name:
//
char *name; // The basic name of the opcode
//
// The list of valid opcodes:
//
opcode *opcodes; // The list of varients
//
// The tree search structure.
//
struct _mnemonic_
*before,
*after;
} mnemonic;
//
// GENERAL SUPPORT ROUTINES
// ========================
//
//
// Routine to do caseless string comparison between a EOS
// terminated fixed string (with) and a buffer located
// length delimited input string (check).
//
static int compare_word( char *check, int len, char *with ) {
int c, w;
while( len-- ) {
if(( c = toupper( *check++ )) != ( w = toupper( *with++ ))) return( c - w );
}
return( - *with );
}
//
// Character to value conversion.
//
static int digit_value( char digit ) {
if(( digit >= '0' )&&( digit <= '9' )) return( digit - '0' );
if(( digit >= 'a' )&&( digit <= 'f' )) return(( digit - 'a' ) + 10 );
if(( digit >= 'A' )&&( digit <= 'F' )) return(( digit - 'A' ) + 10 );
return( ERROR );
}
//
// MOTOROLA 680x0 INSTRUCTIONS
// ===========================
//
//
// Define a data structure that contains the opcodes for the
// Motorola 68000 instruction set.
//
// This is a generated section of C source code produced by
// the 'op68k' executed against the input data in the file
// 'ops68k.ops' and placed into the header file 'ops68k.h'.
//
// The starting point for the assembly of a line of code
// is in the symbol 'ROOT_NODE' which gives the name of the
// object containing the first mnemonic record. This will
// probably need accessing as a pointer, thus: &ROOT_NODE.
//
#include "ops68k.h"
//
// This is the routine which searches the mnemonic tree for
// the start of an opcode list, and returns either a valid
// pointer address or NULL on failure.
//
static opcode *find_opcode( char *find, int len ) {
mnemonic *look;
look = &ROOT_NODE;
while( look != NULL ) {
int cmp;
if(( cmp = compare_word( find, len, look->name )) == 0 ) {
PRINT(( "Found opcode '%s'\n", look->name ));
return( look->opcodes );
}
look = ( cmp < 0 )? look->before: look->after;
}
return( NULL );
}
//
// OPCODE TABLE DISPLAY ROUTINES
// =============================
//
//
// For the assistance of debugging and extensions
// the dump_opcodes routine (and friedns) will output
// the whole table of recognised opcodes to the
// standard output.
//
typedef struct {
arg_type arg;
char *desc;
} arg_desc;
static arg_desc arg_types[] = {
{ EA_DREG, "Dn" },
{ EA_AREG, "An" },
{ EA_AREG_IND, "(An)" },
{ EA_AREG_IND_INC, "(An)+" },
{ EA_AREG_DEC_IND, "-(An)" },
{ EA_AREG_IND_DISP, "d16(An)" },
{ EA_AREG_IND_DISP_REG, "d8(An,Rm.s)" },
{ EA_ABS_SHORT_IND, "a16" },
{ EA_ABS_LONG_IND, "a32" },
{ EA_PC_IND_DISP, "d16(PC)" },
{ EA_PC_IND_DISP_REG, "d8(PC,Rm.s)" },
{ EA_IMMEDIATE, "#i" },
{ EA_STATUS_REG, "SR" },
{ EA_FLAGS_REG, "CCR" },
{ REGISTER_LIST, "RegList" },
{ ABS_ADDRESS, "adrs" },
{ NUM_DREG, "Dn" },
{ NUM_AREG, "An" },
{ NUM_AREG_DEC_IND, "-(An)" },
{ NUM_AREG_IND_INC, "(An)+" },
{ NUM_IMMEDIATE_3BIT, "#0-7" },
{ NUM_IMMEDIATE_QUICK, "#1-8" },
{ NUM_IMMEDIATE_5BIT, "#0-31" },
{ NUM_IMMEDIATE_4, "#0-15" },
{ NUM_IMMEDIATE_8, "#i8" },
{ NUM_IMMEDIATE_16, "#i16" },
{ CONTROL_REG, "ControlReg" },
{ 0 }
};
static void _dump_argument( arg_type type ) {
arg_desc *p;
bool z;
p = arg_types;
z = FALSE;
printf( "[" );
while( p->arg ) {
if( type & p->arg ) {
printf(( z? "|%s": "%s" ), p->desc );
z = TRUE;
}
p++;
}
printf( "]" );
}
static void _dump_opcodes( mnemonic *at ) {
if( at ) {
_dump_opcodes( at->before );
for( opcode *op = at->opcodes; op != NULL; op = op->next ) {
printf( "%s", at->name );
if( op->size != NO_SIZE ) {
printf( ".[" );
if( op->size & SIZE_B ) printf( "B" );
if( op->size & SIZE_W ) printf( "W" );
if( op->size & SIZE_L ) printf( "L" );
printf( "]" );
}
printf( "\t" );
if( op->arg1 != NO_ARGUMENT ) {
_dump_argument( op->arg1 );
}
if( op->arg2 != NO_ARGUMENT ) {
printf( ", " );
_dump_argument( op->arg2 );
}
printf( " -> %04X", op->basecode );
if( op->auxcode ) printf( " %04X", op->auxcode );
printf( "\n" );
}
_dump_opcodes( at->after );
}
}
static void dump_opcodes( void ) {
_dump_opcodes( &ROOT_NODE );
}
//
// THE DATA OUTPUT SYSTEM
// ======================
//
// A system of routines that provide a unified API for outputting
// a range of data formats both computer and human accessible.
//
//
// Define some generic API variables which the specific
// output implementations can initialise and use as appropiate.
//
#define MAX_OUTPUT_BUFFER 16
static byte output_buffer[ MAX_OUTPUT_BUFFER ];
static int buffered_output = 0;
static dword buffered_address = 0;
static FILE *output_file = NULL;
//
// This will be structured around a data structure containing
// pointers to the routines which implement the individual
// API routines.
//
typedef struct {
void FUNC( init_output )( char *source );
void FUNC( next_line )( int line, char *code );
void FUNC( set_address )( dword adrs );
void FUNC( set_start )( dword adrs );
void FUNC( add_byte )( byte data );
void FUNC( end_output )( void );
} output_api;
//
// No Output
// ---------
//
// A dummy output system which does not output
// anything.
//
static void _null_init_output( char *source ) {
}
static void _null_next_line( int line, char *code ) {
}
static void _null_set_address( dword adrs ) {
}
static void _null_set_start( dword adrs ) {
}
static void _null_add_byte( byte data ) {
}
static void _null_end_output( void ) {
}
static output_api _null_output_api = {
_null_init_output,
_null_next_line,
_null_set_address,
_null_set_start,
_null_add_byte,
_null_end_output
};
//
// The listing output system.
// --------------------------
//
// For source code listing these are reuqired. The buffer is
// allocated only if required.
//
// Ensure MAX_SOURCE_OUTPUT_BUFFER <= MAX_OUTPUT_BUFFER
//
#define MAX_SOURCE_CODE_BUFFER 80
#define MAX_SOURCE_OUTPUT_BUFFER 6
static int _listing_this_line = 0;
static char _listing_source_code[ MAX_SOURCE_CODE_BUFFER+1 ]; // +1 for EOS
static void _listing_init_output( char *source ) {
buffered_output = 0;
buffered_address = 0;
_listing_this_line = 0;
}
static void _listing_flush_output( void ) {
int i;
//
// The output format will be:
//
// address %04X
// space
// data %02X Repeat to fixed limit
// source |... To end of line
//
if(( buffered_output == 0 )&&( _listing_this_line == 0 )) return;
if( buffered_output ) {
fprintf( output_file, DWORD_FORMAT" ", buffered_address );
for( i = 0; i < buffered_output; fprintf( output_file, BYTE_FORMAT, output_buffer[ i++ ]));
while( i++ < MAX_SOURCE_OUTPUT_BUFFER ) fprintf( output_file, " " );
buffered_address += buffered_output;
buffered_output = 0;
}
else {
fprintf( output_file, " " );
for( i = 0; i < MAX_SOURCE_OUTPUT_BUFFER; i++ ) fprintf( output_file, " " );
}
if( _listing_this_line ) {
fprintf( output_file, " |%4d|%s\n", _listing_this_line, _listing_source_code );
_listing_this_line = 0;
}
else {
fprintf( output_file, "\n" );
}
}
static void _listing_next_line( int line, char *code ) {
ASSERT( line > 0 );
//
// Flush out anything we have pending..
//
_listing_flush_output();
//
// Save this line for later..
//
strncpy( _listing_source_code, code, MAX_SOURCE_CODE_BUFFER );
_listing_source_code[ MAX_SOURCE_CODE_BUFFER ] = EOS;
_listing_this_line = line;
}
static void _listing_set_address( dword adrs ) {
if(( buffered_address + buffered_output ) != adrs ) {
if( buffered_output ) _listing_flush_output();
buffered_address = adrs;
}
}
static void _listing_set_start( dword adrs ) {
}
static void _listing_add_byte( byte data ) {
output_buffer[ buffered_output++ ] = data;
if( buffered_output >= MAX_SOURCE_OUTPUT_BUFFER ) _listing_flush_output();
}
static void _listing_end_output( void ) {
_listing_flush_output();
}
static output_api _listing_output_api = {
_listing_init_output,
_listing_next_line,
_listing_set_address,
_listing_set_start,
_listing_add_byte,
_listing_end_output
};
//
// The hexadecimal output system.
// ------------------------------
//
static void _hexadecimal_init_output( char *source ) {
buffered_output = 0;
buffered_address = 0;
}
static void _hexadecimal_next_line( int line, char *code ) {
}
static void _hexadecimal_flush_output( void ) {
if( buffered_output ) {
fprintf( output_file, DWORD_FORMAT, buffered_address );
for( int i = 0; i < buffered_output; fprintf( output_file, " " BYTE_FORMAT, output_buffer[ i++ ]));
fprintf( output_file, "\n" );
buffered_address += buffered_output;
buffered_output = 0;
}
}
static void _hexadecimal_set_address( dword adrs ) {
if(( buffered_address + buffered_output ) != adrs ) {
if( buffered_output ) _hexadecimal_flush_output();
buffered_address = adrs;
}
}
static void _hexadecimal_set_start( dword adrs ) {
}
static void _hexadecimal_add_byte( byte data ) {
output_buffer[ buffered_output++ ] = data;
if( buffered_output >= MAX_OUTPUT_BUFFER ) _hexadecimal_flush_output();
}
static void _hexadecimal_end_output( void ) {
_hexadecimal_flush_output();
}
static output_api _hexadecimal_output_api = {
_hexadecimal_init_output,
_hexadecimal_next_line,
_hexadecimal_set_address,
_hexadecimal_set_start,
_hexadecimal_add_byte,
_hexadecimal_end_output
};
//
// The Motorola S record output system.
// ------------------------------------
//
static bool _motorola_have_start = FALSE;
static dword _motorola_start_address = FIRST_ADDRESS;
static dword _motorola_srec_count = 0;
static void _motorola_init_output( char *source ) {
int l;
byte s;
buffered_output = 0;
buffered_address = 0;
_motorola_have_start = FALSE;
_motorola_start_address = FIRST_ADDRESS;
_motorola_srec_count = 0;
//
// Send out header record.
//
// For the moment This is not following the header format:
//
// 20 bytes Module name
// 2 bytes Version
// 2 bytes Revision
// 0-36 bytes Comment
//
s = 0;
l = strlen( source );
fprintf( output_file, "S0%02X0000", l + 3 ); // 3 accounts for address '0000' + checksum 'nn'
for( int i = 0; i < l; i++ ) {
s += source[ i ];
fprintf( output_file, "%02X", source[ i ]);
}
fprintf( output_file, "%02X\r\n", ~s & 0xff );
}
static void _motorola_next_line( int line, char *code ) {
}
static void _motorola_flush_output( void ) {
if( buffered_output ) {
byte s;
s = H( buffered_address ) + L( buffered_address ) + buffered_output;
fprintf( output_file, "S3%02X%08X", 5 + buffered_output, buffered_address );
for( int i = 0; i < buffered_output; i++ ) {
s += output_buffer[ i ];
fprintf( output_file, "%02X", output_buffer[ i ]);
}
fprintf( output_file, "%02X\r\n", ~s & 0xff );
buffered_address += buffered_output;
buffered_output = 0;
_motorola_srec_count++;
}
}
static void _motorola_set_address( dword adrs ) {
if(( buffered_address + buffered_output ) != adrs ) {
if( buffered_output ) _motorola_flush_output();
buffered_address = adrs;
}
}
static void _motorola_set_start( dword adrs ) {
if( _motorola_have_start ) {
fprintf( stderr, "Duplicate START address specified\n" );
}
else {
_motorola_have_start = TRUE;
_motorola_start_address = adrs;
}
}
static void _motorola_add_byte( byte data ) {
output_buffer[ buffered_output++ ] = data;
if( buffered_output >= MAX_OUTPUT_BUFFER ) _motorola_flush_output();
}
static void _motorola_end_output( void ) {
byte s;
_motorola_flush_output();
//
// The end records:
//
if( _motorola_srec_count <= MAX_U_WORD ) {
s = H( _motorola_srec_count ) + L( _motorola_srec_count ) + 3;
fprintf( output_file, "S503%04X%02X\r\n", _motorola_srec_count, ~s & 0xff );
}
else {
s = DW2( _motorola_srec_count ) + DW1( _motorola_srec_count ) + DW0( _motorola_srec_count ) + 4;
fprintf( output_file, "S604%06X%02X\r\n", ( _motorola_srec_count & 0xFFFFFF ), ~s & 0xff );
}
s = DW3( _motorola_start_address ) + DW2( _motorola_start_address ) + DW1( _motorola_start_address ) + DW0( _motorola_start_address ) + 5;
fprintf( output_file, "S905%08X%02X\r\n", _motorola_start_address, ~s & 0xff );
}
static output_api _motorola_output_api = {
_motorola_init_output,
_motorola_next_line,
_motorola_set_address,
_motorola_set_start,
_motorola_add_byte,
_motorola_end_output
};
//
// The Intel Hex record output system.