From 399af2c99d69504ea041ca0373183040fa6dbdc1 Mon Sep 17 00:00:00 2001 From: Vilem Karsky Date: Sun, 28 Mar 2021 14:31:49 +0200 Subject: [PATCH 1/4] Correct error in while, implementation (without that swap lbforth throws segmentation fault. Without swap the execution token of branch?, is pushet to return stack and if, doesnt find this execution token (pushed to return stack should be here address which was placed on tos by begin, ) --- target/avr/asm.fth | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/target/avr/asm.fth b/target/avr/asm.fth index 2a14497..e3a2f1c 100644 --- a/target/avr/asm.fth +++ b/target/avr/asm.fth @@ -283,7 +283,7 @@ drop : 3swap >r rot >r 2swap 2r> >r -rot r> ; : else, ahead, 3swap then, ; -: while, >r if, r> ; +: while, swap >r if, r> ; : repeat, again, then, ; \ Runtime for ;CODE. CODE! is defined elsewhere. From b76bcb0fa6d80c87dd31f35a50879f8b5547af60 Mon Sep 17 00:00:00 2001 From: Vilem Karsky Date: Sat, 6 Mar 2021 19:46:34 +0100 Subject: [PATCH 2/4] Edited for atega328p (edited nucleus.fth for correct interrupt vectors size and changet position of return stack to end of ram and data stack to 3/4 od ram, edited params.fth for correct data ram start). Implemetend spuport for interrupt (in interrupt.fth and in nucleus.fth - swr = save working registers, rwr = restore working registers). Example is in test/interrupt-atmega328.fth --- target/avr/interrupt.fth | 17 +++++++++++++ target/avr/nucleus.fth | 53 ++++++++++++++++++++++++++++++++++++---- target/avr/params.fth | 2 +- target/avr/x2.fth | 1 + 4 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 target/avr/interrupt.fth diff --git a/target/avr/interrupt.fth b/target/avr/interrupt.fth new file mode 100644 index 0000000..049c8f9 --- /dev/null +++ b/target/avr/interrupt.fth @@ -0,0 +1,17 @@ + +also assembler + +: isr! ( xt n - ) ( compile jmp instruction to isr routine ) + 4 * t-image + ( xt adr0 - ) + dup ( xt adr0 adr0 - ) + 12 swap c! ( xt adr0 - ) + 1+ ( xt adr1 - ) + dup ( xt adr1 adr1 - ) + 148 swap c! ( xt adr1 - ) + 1+ ( xt adr2 - ) + over 1 rshift over c! ( xt adr2 - ) + 1+ ( xt adr3 - ) + swap 9 rshift swap c! +; + +previous diff --git a/target/avr/nucleus.fth b/target/avr/nucleus.fth index a3234d3..6621fe3 100644 --- a/target/avr/nucleus.fth +++ b/target/avr/nucleus.fth @@ -1,13 +1,29 @@ -code cold - ahead, \ Interrupt vectors. +code iv, + ahead, nop, \ Interrupt vectors. atmeba328p each IV = 2 instructions, 26 interrupt vectors => 52 cells (ahead, + nop, = reset; + 25IV) nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, nop, - then, + nop, nop, nop, nop, nop, nop, nop, nop, + nop, nop, + nop, nop, nop, nop, nop, nop, nop, nop, + nop, nop, nop, nop, nop, nop, nop, nop, + nop, nop, nop, nop, nop, nop, nop, nop, +end-code - 140 # r28 ldi, \ Set data stack pointer. +code cold, + then, +( 140 # r28 ldi, \ Set data stack pointer. Problem s pretekanim!!!! zmenit pocatky OBOUT stacků r29 clr, 158 # r16 ldi, - 61 r16 out, \ Set return stack pointer. + 61 r16 out, \ Set return stack pointer. ) + + 255 # r28 ldi, \ Set data stack pointer to 3/4 of ram + 6 # r29 ldi, + + 255 # r16 ldi, \ Set return stack pointer to ram end + 61 r16 out, + 8 # r16 ldi, + 62 r16 out, + ahead, \ Jump to WARM. end-code @@ -165,6 +181,33 @@ code 0< then, end-code +code swr + -y r27 st, + 63 r27 in, + -y r27 st, + -y r26 st, + -y r3 st, + -y r2 st, + -y r31 st, + -y r30 st, + ret, +end-code + +code rwr + y+ r30 ld, + y+ r31 ld, + y+ r2 ld, + y+ r3 ld, + y+ r26 ld, + y+ r27 ld, + 63 r27 out, + y+ r27 ld, + ret, +end-code + + + + : r@ r> r> dup >r swap >r ; : = - [ \ Fall through. : 0= if 0 else -1 then ; diff --git a/target/avr/params.fth b/target/avr/params.fth index 0f6e64f..b18d590 100644 --- a/target/avr/params.fth +++ b/target/avr/params.fth @@ -1,5 +1,5 @@ 1 constant t-little-endian 2 constant t-cell 0 constant program-start -hex 60 constant data-start +hex 100 constant data-start decimal diff --git a/target/avr/x2.fth b/target/avr/x2.fth index 90069ba..36d8909 100644 --- a/target/avr/x2.fth +++ b/target/avr/x2.fth @@ -1,5 +1,6 @@ also assembler h: exit ret, ; +h: exitint reti, ; h: nip 2 # r28 adiw, ; h: cell+ 2 # r26 adiw, ; h: 1+ 1 # r26 adiw, ; From ad81f956bb4d5f1da6acae19c8e2054ed6663015 Mon Sep 17 00:00:00 2001 From: Vilem Karsky Date: Sat, 6 Mar 2021 21:04:28 +0100 Subject: [PATCH 3/4] Example of interrupt --- test/interrupt-atmega328.fth | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 test/interrupt-atmega328.fth diff --git a/test/interrupt-atmega328.fth b/test/interrupt-atmega328.fth new file mode 100644 index 0000000..6838eed --- /dev/null +++ b/test/interrupt-atmega328.fth @@ -0,0 +1,26 @@ +: DDRCOUT 255 39 c! ; + +: PORTCON 255 40 c! ; + +: PORTCOFF 0 40 c! ; + +: DDRBOUT 255 36 c! ; + +: PORTBON 255 37 c! ; + +: PORTBOFF 0 37 c! ; + +: 1MS 300 begin 1- dup 0= until drop ; + +: MS begin 1MS 1- dup 0= until drop ; + +: SETUPISR 12 105 c! 2 61 c! 128 95 c! ; + +: myisr swr 37 c@ 255 xor 37 c! 500 MS rwr int; + +variable abcd + +: abc then 5 abcd ! 15 DDRCOUT DDRBOUT SETUPISR 3000 MS begin 1000 MS PORTCON 1000 MS PORTCOFF again ; + +' myisr 2 isr! + From 2446ead1f97d7845afdaf97cc07895aa881bf1a5 Mon Sep 17 00:00:00 2001 From: Vilem Karsky Date: Sat, 1 May 2021 00:15:25 +0200 Subject: [PATCH 4/4] Forgoten changes to compiler.fth --- src/compiler.fth | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/compiler.fth b/src/compiler.fth index 658ef43..6022ee0 100644 --- a/src/compiler.fth +++ b/src/compiler.fth @@ -22,6 +22,7 @@ include target/asm.fth : header, ( a u -- ) here t-word here to latest ; include target/x1.fth +include target/avr/interrupt.fth also forth ' comp, is t-compile, @@ -39,6 +40,7 @@ h: end-code previous ; only forth also meta also compiler definitions previous include target/x2.fth +h: int; [compile] exitint [compile] [ ; h: ; [compile] exit [compile] [ ; h: ['] ' t-literal ; h: [char] char t-literal ;