From eff10f66a65d0577aa9d10ee08b469cb9c83e1a3 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 16 Feb 2015 18:17:07 +0000 Subject: py: Implement bl/bx instructions for inline Thumb assembler. --- py/asmthumb.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'py/asmthumb.c') diff --git a/py/asmthumb.c b/py/asmthumb.c index 854f455455..d23be0ef2b 100644 --- a/py/asmthumb.c +++ b/py/asmthumb.c @@ -40,6 +40,7 @@ #define SIGNED_FIT8(x) (((x) & 0xffffff80) == 0) || (((x) & 0xffffff80) == 0xffffff80) #define SIGNED_FIT9(x) (((x) & 0xffffff00) == 0) || (((x) & 0xffffff00) == 0xffffff00) #define SIGNED_FIT12(x) (((x) & 0xfffff800) == 0) || (((x) & 0xfffff800) == 0xfffff800) +#define SIGNED_FIT23(x) (((x) & 0xffc00000) == 0) || (((x) & 0xffc00000) == 0xffc00000) struct _asm_thumb_t { mp_uint_t pass; @@ -455,6 +456,20 @@ void asm_thumb_bcc_label(asm_thumb_t *as, int cond, uint label) { } } +#define OP_BL_HI(byte_offset) (0xf000 | (((byte_offset) >> 12) & 0x07ff)) +#define OP_BL_LO(byte_offset) (0xf800 | (((byte_offset) >> 1) & 0x07ff)) + +void asm_thumb_bl(asm_thumb_t *as, uint label) { + mp_uint_t dest = get_label_dest(as, label); + mp_int_t rel = dest - as->code_offset; + rel -= 4; // account for instruction prefetch, PC is 4 bytes ahead of this instruction + if (SIGNED_FIT23(rel)) { + asm_thumb_op32(as, OP_BL_HI(rel), OP_BL_LO(rel)); + } else { + printf("asm_thumb_bl: branch does not fit in 23 bits\n"); + } +} + #define OP_BLX(reg) (0x4780 | ((reg) << 3)) #define OP_SVC(arg) (0xdf00 | (arg)) -- cgit v1.2.3