Skip to content

Commit

Permalink
Changed serial and usb chat, bdm, and avrprog code to use pad
Browse files Browse the repository at this point in the history
Instead of each defining its own buffer and so on. The only place that used
b>  and  b<  was bdm-usb-host, so I moved that code there.

Also, I defined little- and big-endian versions of w@ and w! (lew@ lew! bew@
bew!) so they exist no matter what kind of target is loaded. Using these and
pad was enough to do chat and avrprog pretty cleanly.
  • Loading branch information
nimblemachines committed Feb 15, 2012
1 parent adec4d5 commit 9f8dd39
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 51 deletions.
17 changes: 4 additions & 13 deletions target/HC08/avrprog-serial-host.mu4
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,20 +10,11 @@ loading AVR SPI-over-serial programmer (host)


hex hex


4 buffer avrbuf ( for returned values) : !chat <# ; ( init hld)
variable avrptr ( pointer into avrbuf)

: !avrbuf avrbuf avrptr ! ;
: +avrptr 1 avrptr +! ;

: b> ( byte) avrptr @ c! +avrptr ;
-- : b< ( - byte) avrptr @ c@ +avrptr ;


( Commands) ( Commands)
: avr.Execute ( b3 b2 b1 b0) : avr.Execute ( b3 b2 b1 b0)
!avrbuf !chat 21 send 4 for send recv hold next ;
21 send
4 for send recv b> next ;


: avr.Bye ( End session, return to chat command loop) 00 send ; : avr.Bye ( End session, return to chat command loop) 00 send ;
: avr.ResetLow 22 send ; : avr.ResetLow 22 send ;
Expand All @@ -36,10 +27,10 @@ variable avrptr ( pointer into avrbuf)
: avr ( b0 b1 b2 b3) swap 2swap swap ( b3 b2 b1 b0) avr.Execute ; : avr ( b0 b1 b2 b3) swap 2swap swap ( b3 b2 b1 b0) avr.Execute ;


: avr2 ( b0 b1 b2 b3 - r2) ( do command; return second-to-last byte of response) : avr2 ( b0 b1 b2 b3 - r2) ( do command; return second-to-last byte of response)
avr avrbuf 2 + c@ ; avr hld @ 1+ c@ ;


: avr3 ( b0 b1 b2 b3 - r3) ( do command; return last byte of response) : avr3 ( b0 b1 b2 b3 - r3) ( do command; return last byte of response)
avr avrbuf 3 + c@ ; avr hld @ c@ ;


: prog ( enable programming) : prog ( enable programming)
.ifdef in-ram .ifdef in-ram
Expand Down
8 changes: 4 additions & 4 deletions target/HC08/avrprog-usb-host.mu4
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ hex
lohi> push lohi> pop ; lohi> push lohi> pop ;


( Commands) ( Commands)
: avr.Result 0c0 20 0 0 4 ubuf usb ; : avr.Result 0c0 20 0 0 4 pad usb ;


: avr.Execute ( b0 b1 b2 b3) >setup 2push : avr.Execute ( b0 b1 b2 b3) >setup 2push
40 21 2pop 4 ubuf usb ; 40 21 2pop 4 pad usb ;


: control-write : control-write
create , ( bRequest) create , ( bRequest)
Expand All @@ -38,10 +38,10 @@ hex
: avr ( b0 b1 b2 b3) avr.Execute avr.Result ; : avr ( b0 b1 b2 b3) avr.Execute avr.Result ;


: avr2 ( b0 b1 b2 b3 - r2) ( do command; return second-to-last byte of response) : avr2 ( b0 b1 b2 b3 - r2) ( do command; return second-to-last byte of response)
avr ubuf 2 + c@ ; avr pad 2 + c@ ;


: avr3 ( b0 b1 b2 b3 - r3) ( do command; return last byte of response) : avr3 ( b0 b1 b2 b3 - r3) ( do command; return last byte of response)
avr ubuf 3 + c@ ; avr pad 3 + c@ ;


: prog ( enable programming) : prog ( enable programming)
.ifdef in-ram .ifdef in-ram
Expand Down
20 changes: 15 additions & 5 deletions target/HC08/bdm-usb-host.mu4
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ hex


( Commands) ( Commands)
: u.Result ( #expect) push ( fetch BDM result, or Sync pulse length) : u.Result ( #expect) push ( fetch BDM result, or Sync pulse length)
0c0 20 0 0 pop ubuf usb ; 0c0 20 0 0 pop pad usb ;


: u.Execute ( #write #expect) 2push ( execute BDM request) : u.Execute ( #write #expect) 2push ( execute BDM request)
40 21 0 pop pop ubuf usb ; 40 21 0 pop pop pad usb ;


: control-write : control-write
create , ( bRequest) create , ( bRequest)
Expand All @@ -44,16 +44,26 @@ hex


-- : b.Idle ; -- : b.Idle ;


( A simple buffer for chatty communication protocols. Words for putting
values into a buffer -- at pad -- and taking them out again.)

: !chat <# ;
: #chat hld @ pad - ;
: +hld 1 hld +! ;

: b> ( byte) hld @ c! +hld ;
: b< ( - byte) hld @ c@ +hld ;

( For S08 targets, we send up to four bytes, and expect up to two.) ( For S08 targets, we send up to four bytes, and expect up to two.)
: Send1 !ubuf b> ; : Send1 !chat b> ;
: Send2 Send1 b> ; : Send2 Send1 b> ;
: Send3 Send2 b> ; : Send3 Send2 b> ;
: Send4 Send3 b> ; : Send4 Send3 b> ;


: expect ( #expect) #ubuf over u.Execute u.Result !ubuf ; : expect ( #expect) #chat over u.Execute u.Result !chat ;


: Expect0 0 expect ; : Expect0 0 expect ;
: Expect1 1 expect b< ; : Expect1 1 expect b< ;
: Expect2 2 expect b< b< ; : Expect2 2 expect b< b< ;


: b.Sync u.Sync 2 u.Result !ubuf w< ; : b.Sync u.Sync 2 u.Result pad bew@ ;
24 changes: 6 additions & 18 deletions target/HC08/chat-usb-host.mu4
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -51,23 +51,11 @@ hex
0c0 03 0 0 2 pop usb ; 0c0 03 0 0 2 pop usb ;


variable uaddr ( current memory access address) variable uaddr ( current memory access address)
10 buffer ubuf ( for short reads and writes)
variable uptr ( pointer into ubuf)


: !ubuf ubuf uptr ! ; : usb-c@ ( addr - byte) 1 pad u.Read pad c@ ;
: +uptr 1 uptr +! ; : usb-c! ( byte addr) swap pad c! 1 pad u.Write ;
: #ubuf uptr @ ubuf - ;


: b> ( byte) uptr @ c! +uptr ; : usb-sp ( - sp) pad u.ReadSp pad bew@ ;
: b< ( - byte) uptr @ c@ +uptr ;

: w> ( word) >lohi b> b> ; ( Big endian!)
: w< ( - word) b< b< hilo> ; ( ditto!)

: usb-c@ ( addr - byte) 1 ubuf u.Read ubuf c@ ;
: usb-c! ( byte addr) swap ubuf c! 1 ubuf u.Write ;

: usb-sp ( - sp) ubuf u.ReadSp !ubuf w< ;


: u.SetAddr uaddr ! ; : u.SetAddr uaddr ! ;
: u.GetAddr uaddr @ ; : u.GetAddr uaddr @ ;
Expand All @@ -78,13 +66,13 @@ variable uptr ( pointer into ubuf)
usb-sp dup push 6 rot u.Read pop ; usb-sp dup push 6 rot u.Read pop ;


: u.SetRegs ( buf) : u.SetRegs ( buf)
usb-sp 6 rot u.Write ; usb-sp 6 rot u.Write ;


( Order of regs in memory: H CC A X PC) ( Order of regs in memory: H CC A X PC)
( XXX this is a huge kludge) ( XXX this is a huge kludge)
: u.SetPC ( pc) : u.SetPC ( pc)
usb-sp 4 + swap !ubuf w> usb-sp 4 + swap pad bew!
2 ubuf u.Write ; 2 pad u.Write ;


.ifdef t.Id ( if we've loaded interact support) .ifdef t.Id ( if we've loaded interact support)


Expand Down
12 changes: 7 additions & 5 deletions target/HC08/memory.mu4
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,22 +32,24 @@ loading HC08 memory image
: .debug-comma ; : .debug-comma ;
.then .then


( DEBUG: Define show-c! to show writes to image.) ( DEBUG: Define show-! to show writes to image.)
.ifdef show-c! .ifdef show-!
: .debug-c! cr ( byte addr) 2dup .hcell_ .h8 ; : .debug-c! cr ( byte addr) 2dup .hcell_ .h8 ;
: .debug-w! cr ( word addr) 2dup .hcell_ .hcell ;
.else .else
: .debug-c! ; : .debug-c! ;
: .debug-w! ;
.then .then


: image-c@ image+ c@ ; : image-c@ image+ c@ ;
: image-c! .debug-c! image+ c! ; : image-c! .debug-c! image+ c! ;


( HC08s are big-endian, like all Motorola/Freescale processors!) ( HC08s are big-endian, like all Motorola/Freescale processors!)
: image-! ( w a) push >lohi r@ ( hi) image-c! pop 1+ ( lo) image-c! ; : image-@ ( a - w) image+ bew@ ;
: image-@ ( a - w) dup image-c@ ( hi) swap 1+ image-c@ ( lo) hilo> ; : image-! ( w a) .debug-w! image+ bew! ;


: image-p! image+ p! ;
: image-p@ p@ image- ; : image-p@ p@ image- ;
: image-p! image+ p! ;


: target-* ( - cell) c* c* hilo> ; ( big-endian 16-bit word) : target-* ( - cell) c* c* hilo> ; ( big-endian 16-bit word)


Expand Down
15 changes: 9 additions & 6 deletions target/common/16bit.mu4
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
loading Support for handling 16-bit words loading Support for handling 16-bit words


( I'm not sure where to put this. It's not specific to a particular target, ( I'm not sure where to put this. It's not specific to a particular target,
so having it here in target/common/ makes sense. so having it here in target/common/ makes sense.)

These also aren't exactly particular to a certain target endianness,
either, though they are useful when serialisng data into a buffer that
gets sent to a target, or unserialising from a buffer received from a
target.)


( Generally helpful for little-endian targets.) ( Generally helpful for little-endian targets.)
: >hilo ( w - hi lo) dup 8 u>> swap "0ff and ; : >hilo ( w - hi lo) dup 8 u>> swap "0ff and ;
: lohi> ( lo hi - w) 8 << or ; : lohi> ( lo hi - w) 8 << or ;


( Little-endian word fetch and store.)
: lew@ ( a - w) dup c@ ( lo) swap 1+ c@ ( hi) lohi> ;
: lew! ( w a) push >hilo r@ c! ( lo) pop 1+ c! ( hi) ;

( Generally helpful for big-endian targets.) ( Generally helpful for big-endian targets.)
: >lohi ( w - lo hi) dup "0ff and swap 8 u>> ; : >lohi ( w - lo hi) dup "0ff and swap 8 u>> ;
: hilo> ( hi lo - w) swap 8 << or ; : hilo> ( hi lo - w) swap 8 << or ;

( Big-endian word fetch and store.)
: bew@ ( a - w) dup c@ ( hi) swap 1+ c@ ( lo) hilo> ;
: bew! ( w a) push >lohi r@ c! ( hi) pop 1+ c! ( lo) ;

0 comments on commit 9f8dd39

Please sign in to comment.