Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for interrupt in atmega328p #66

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/compiler.fth
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 ;
Expand Down
2 changes: 1 addition & 1 deletion target/avr/asm.fth
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 17 additions & 0 deletions target/avr/interrupt.fth
Original file line number Diff line number Diff line change
@@ -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
53 changes: 48 additions & 5 deletions target/avr/nucleus.fth
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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 ;
Expand Down
2 changes: 1 addition & 1 deletion target/avr/params.fth
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions target/avr/x2.fth
Original file line number Diff line number Diff line change
@@ -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, ;
Expand Down
26 changes: 26 additions & 0 deletions test/interrupt-atmega328.fth
Original file line number Diff line number Diff line change
@@ -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!