4 * Copyright (C) 1998-2011 Alan R. Baldwin
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * This Assember Ported by
25 * John L. Hartman (JLH)
26 * jhartman at compuserve dot com
27 * noice at noicedebugger dot com
29 * Benny Kim (2011/07/21)
30 * bennykim at coreriver dot com
31 * Fixed bugs in relative address with "."
37 char *cpu
= "Intel 8051";
41 * Opcode Cycle Definitions
43 #define OPCY_SDP ((char) (0xFF))
44 #define OPCY_ERR ((char) (0xFE))
46 /* OPCY_NONE ((char) (0x80)) */
47 /* OPCY_MASK ((char) (0x7F)) */
49 #define UN ((char) (OPCY_NONE | 0x00))
54 * opcycles = i51pg1[opcode]
56 static char i51pg1
[256] = {
57 /*--*--* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
58 /*--*--* - - - - - - - - - - - - - - - - */
59 /*00*/ 12,24,24,12,12,12,12,12,12,12,12,12,12,12,12,12,
60 /*10*/ 24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,12,
61 /*20*/ 24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,12,
62 /*30*/ 24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,12,
63 /*40*/ 24,24,12,24,12,12,12,12,12,12,12,12,12,12,12,12,
64 /*50*/ 24,24,12,24,12,12,12,12,12,12,12,12,12,12,12,12,
65 /*60*/ 24,24,12,24,12,12,12,12,12,12,12,12,12,12,12,12,
66 /*70*/ 24,24,24,24,12,24,12,12,12,12,12,12,12,12,12,12,
67 /*80*/ 24,24,24,24,48,24,24,24,24,24,24,24,24,24,24,24,
68 /*90*/ 24,24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,
69 /*A0*/ 24,24,12,24,48,UN
,24,24,24,24,24,24,24,24,24,24,
70 /*B0*/ 24,24,12,12,24,24,24,24,24,24,24,24,24,24,24,24,
71 /*C0*/ 24,24,12,12,12,12,12,12,12,12,12,12,12,12,12,12,
72 /*D0*/ 24,24,12,12,12,24,12,12,24,24,24,24,24,24,24,24,
73 /*E0*/ 24,24,24,24,12,12,12,12,12,12,12,12,12,12,12,12,
74 /*F0*/ 24,24,24,24,12,12,12,12,12,12,12,12,12,12,12,12
78 * Process machine ops.
81 machine(struct mne
*mp
)
85 struct expr e
, e1
, e2
;
100 * 11 bit destination.
101 * Top 3 bits become the MSBs of the op-code.
104 outrwm(&e
, R_J11
, op
);
116 xerr('a', "Argument must be A.");
121 /* A, direct, @R0, @R1, R0 to R7. "INC" also allows DPTR */
131 /* Direct is also legal */
137 outab(op
+ 6 + e
.e_addr
);
141 outab(op
+ 8 + e
.e_addr
);
146 /* only INC (op=0) has DPTR mode */
147 xerr('a', "DPTR allowed only in INC instruction.");
153 xerr('a', "Invalid Addressing Mode.");
158 /* A,#imm; A,direct; A,@R0; A,@R1; A,R0 to A,R7 */
161 xerr('a', "First argument must be A.");
178 outab(op
+ 6 + e1
.e_addr
);
182 outab(op
+ 8 + (e1
.e_addr
));
186 xerr('a', "Invalid Addressing Mode.");
192 * A,#imm; A,direct; A,@R0; A,@R1; A,R0 to A,R7
193 * C,direct; C,/direct
215 xerr('a', "Invalid Addressing Mode.");
233 outab(op
+ 6 + e1
.e_addr
);
237 outab(op
+ 8 + e1
.e_addr
);
241 xerr('a', "Invalid Addressing Mode.");
246 /* XRL has no boolean version. Trap it */
248 xerr('a', "XRL does not support boolean.");
263 xerr('a', "Invalid Addressing Mode.");
268 xerr('a', "Invalid Addressing Mode.");
273 /* A,direct; A,@R0; A,@R1; A,R0 to A,R7 */
276 xerr('a', "First argument must be A.");
288 outab(op
+ 6 + e1
.e_addr
);
292 outab(op
+ 8 + e1
.e_addr
);
296 xerr('a', "Invalid Addressing Mode.");
300 /* MOV instruction, all modes */
321 outab(0xE6 + e1
.e_addr
);
325 outab(0xE8 + e1
.e_addr
);
329 xerr('a', "Invalid Addressing Mode.");
336 outab(0xF8 + e
.e_addr
);
340 outab(0x78 + e
.e_addr
);
346 outab(0xA8 + e
.e_addr
);
351 xerr('a', "Invalid Addressing Mode.");
377 outab(0x86 + e1
.e_addr
);
382 outab(0x88 + e1
.e_addr
);
392 xerr('a', "Invalid Addressing Mode.");
399 outab(0x76 + e
.e_addr
);
405 outab(0xA6 + e
.e_addr
);
410 outab(0xF6 + e
.e_addr
);
414 xerr('a', "Invalid Addressing Mode.");
419 if ((t1
!= S_DIR
) && (t1
!= S_EXT
))
420 xerr('a', "Second argument must be an address.");
427 xerr('a', "#__ is required second argument.");
433 xerr('a', "Invalid Addressing Mode.");
437 case S_BITBR
: /* JB, JBC, JNB bit,rel */
438 /* Branch on bit set/clear */
440 if ((t
!= S_DIR
) && (t
!= S_EXT
))
441 xerr('a', "Argument must be an address.");
442 /* sdcc svn rev #4994: fixed bug 1865114 */
450 v1
= (int) (e1
.e_addr
- dot
.s_addr
- 1);
451 /* sdcc svn rev #602: Fix some path problems */
452 if (pass
== 2 && ((v1
< -128) || (v1
> 127)))
453 xerr('a', "Branching Range Exceeded.");
458 if (e1
.e_mode
!= S_USER
)
462 case S_BR
: /* JC, JNC, JZ, JNZ */
463 /* Relative branch */
464 /* sdcc svn rev #4994: fixed bug 1865114 */
469 v1
= (int) (e1
.e_addr
- dot
.s_addr
- 1);
470 /* sdcc svn rev #602: Fix some path problems */
471 if (pass
== 2 && ((v1
< -128) || (v1
> 127)))
472 xerr('a', "Branching Range Exceeded.");
477 if (e1
.e_mode
!= S_USER
)
482 /* A,#; A,dir; @R0,#; @R1,#; Rn,# */
497 else if ((t1
== S_DIR
) || (t1
== S_EXT
)) {
502 xerr('a', "Invalid Addressing Mode.");
506 outab(op
+ 6 + e
.e_addr
);
508 xerr('a', "#__ is required second argument.");
513 outab(op
+ 8 + e
.e_addr
);
515 xerr('a', "#__ is required second argument.");
520 xerr('a', "Invalid Addressing Mode.");
524 /* branch destination */
526 v1
= (int) (e2
.e_addr
- dot
.s_addr
- 1);
527 /* sdcc svn rev #602: Fix some path problems */
528 if (pass
== 2 && ((v1
< -128) || (v1
> 127)))
529 xerr('a', "Branching Range Exceeded.");
534 if (e2
.e_mode
!= S_USER
)
539 /* Dir,dest; Reg,dest */
541 /* sdcc svn rev #4994: fixed bug 1865114 */
553 outab(op
+ 8 + e
.e_addr
);
557 xerr('a', "Invalid Addressing Mode.");
560 /* branch destination */
561 /* sdcc svn rev #4994: fixed bug 1865114 */
563 v1
= (int) (e1
.e_addr
- dot
.s_addr
- 1);
564 /* sdcc svn rev #602: Fix some path problems */
565 if (pass
== 2 && ((v1
< -128) || (v1
> 127)))
566 xerr('a', "Branching Range Exceeded.");
571 if (e1
.e_mode
!= S_USER
)
579 xerr('a', "JMP @A+DPTR is the only allowed mode.");
584 /* A,@A+DPTR A,@A+PC */
587 xerr('a', "First argument must be A.");
592 else if (t1
== S_AT_APC
)
595 xerr('a', "MOVC A,@A+DPTR; A,@A+PC are the allowed modes.");
599 /* A,@DPTR A,@R0 A,@R1 @DPTR,A @R0,A @R1,A */
612 outab(0xE2 + e1
.e_addr
);
616 xerr('a', "Second argument must be @DPTR or @Rn.");
624 xerr('a', "Second argument must A.");
629 outab(0xF2 + e
.e_addr
);
631 xerr('a', "Second argument must A.");
635 xerr('a', "Invalid Addressing Mode.");
643 xerr('a', "A,B is the only valid argument.");
647 /* CLR or CPL: A, C, or bit */
669 xerr('a', "Invalid Addressing Mode.");
688 xerr('a', "Invalid Addressing Mode.");
699 if ((t
!= S_DIR
) && (t
!= S_EXT
)) {
700 xerr('a', "Argument must be an address.");
711 xerr('a', "First argument must A.");
716 outab(op
+ e1
.e_addr
);
720 xerr('a', "Invalid Addressing Mode.");
726 xerr('o', "Internal Opcode Error.");
729 if (opcycles
== OPCY_NONE
) {
730 opcycles
= i51pg1
[cb
[0] & 0xFF];
735 * Branch/Jump PCR Mode Check
738 mchpcr(struct expr
*esp
)
740 if (esp
->e_base
.e_ap
== dot
.s_area
) {
743 if (esp
->e_flag
==0 && esp
->e_base
.e_ap
==NULL
) {
745 * Absolute Destination
747 * Use the global symbol '.__.ABS.'
748 * of value zero and force the assembler
749 * to use this absolute constant as the
750 * base value for the relocation.
753 esp
->e_base
.e_sp
= &sym
[1];
759 * Machine specific initialization
762 static int beenHere
= 0; /* set non-zero if we have done that... */
785 * add the pre-defined symbols to the table
792 for (i
=0; i
<2; i
++) {
794 * i == 0, Create Upper Case Symbols
795 * i == 1, Create Lower Case Symbols
800 *p
= ccase
[*p
& 0x007F];
805 if (sp
->s_type
== S_NEW
) {
806 sp
->s_addr
= pd
->value
;
808 sp
->s_flag
= S_LCL
| S_ASG
;