From e68d231ff6bbea90a39f3707a1308b113dccbb1c Mon Sep 17 00:00:00 2001 From: user Date: Fri, 1 Sep 2023 04:43:42 -0500 Subject: [PATCH] Add DDIR digital port direction instruction This should be useful for switching between output and pull-up input, which can be useful for scanning switch matrices. --- asm_2/bytecode.txt | 8 ++++---- asm_2/interp.asm | 45 ++++++++++++++++++++++++++++++++++++++++++++- asm_2/tbc.inc | 14 ++++++++++++-- 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/asm_2/bytecode.txt b/asm_2/bytecode.txt index 08eb179..69069c6 100644 --- a/asm_2/bytecode.txt +++ b/asm_2/bytecode.txt @@ -67,10 +67,10 @@ FXYN 2XY5 shra 2XY6 rol 2XY7 ror -2XY8 -2XY9 spi -2XYA mft -2XYB mtt +2XY8 spi +2XY9 mft +2XYA mtt +2XYB ddir 2XYC din 2XYD dout 2XYE ain diff --git a/asm_2/interp.asm b/asm_2/interp.asm index 8ad4d7f..6589817 100644 --- a/asm_2/interp.asm +++ b/asm_2/interp.asm @@ -542,10 +542,10 @@ imm4_dispatch_jumptable: rjmp exec_shra rjmp exec_rol rjmp exec_ror - rjmp exec_nop rjmp exec_spi rjmp exec_mft rjmp exec_mtt + rjmp exec_ddir rjmp exec_din rjmp exec_dout rjmp exec_ain @@ -1343,6 +1343,49 @@ exec_mtt: rjmp _dispatch_done +exec_ddir: + ; Restrict operand to 0-f + ldi r25, 0x0f + and r10, r25 + + ; Extract LSB from first operand + ldi r23, 0x01 + and r6, r23 + clr r7 + + ; Mask of all bits except LSB + ldi r24, 0xfe + ldi r25, 0xff + + ; Rotate LSB and mask into position specified by second operand +_ddir_loop: + dec r10 + brlt _din_loop_done + + sec + rol r24 + rol r25 + + clc + rol r6 + rol r7 + + rjmp _ddir_loop +_ddir_loop_done: + + ; Read-modify-write + in r22, DDRB + in r23, DDRC + and r22, r24 + and r23, r25 + or r22, r6 + or r23, r7 + out DDRB, r22 + out DDRC, r23 + + rjmp _dispatch_done + + exec_din: ; Restrict operand to 0-f ldi r25, 0x0f diff --git a/asm_2/tbc.inc b/asm_2/tbc.inc index d0898c1..4035615 100644 --- a/asm_2/tbc.inc +++ b/asm_2/tbc.inc @@ -483,7 +483,7 @@ .if @1 < 1 || @1 > 0x10 .error "Tortoise Bytecode: SPI immediate value outside 4-bit range" .endif - .dw (0x2 << 12) | (@0 << 8) | ((@1 & 0xf) << 4) | (0x9) + .dw (0x2 << 12) | (@0 << 8) | ((@1 & 0xf) << 4) | (0x8) .endmacro .macro T_MFT @@ -493,10 +493,20 @@ .if @1 < 0 || @1 > 0xf .error "Tortoise Bytecode: immediate value outside 4-bit range" .endif - .dw (0x2 << 12) | (@0 << 8) | (@1 << 4) | (0xa) + .dw (0x2 << 12) | (@0 << 8) | (@1 << 4) | (0x9) .endmacro .macro T_MTT + .if @0 < 0 || @0 > 0xf + .error "Tortoise Bytecode: invalid variable register" + .endif + .if @1 < 0 || @1 > 0xf + .error "Tortoise Bytecode: immediate value outside 4-bit range" + .endif + .dw (0x2 << 12) | (@0 << 8) | (@1 << 4) | (0xa) +.endmacro + +.macro T_DDIR .if @0 < 0 || @0 > 0xf .error "Tortoise Bytecode: invalid variable register" .endif -- 2.43.0