-
Notifications
You must be signed in to change notification settings - Fork 30
/
chat3-host.mu4
148 lines (107 loc) · 4.67 KB
/
chat3-host.mu4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
| This file is part of muforth: https://muforth.dev/
|
| Copyright 2002-2024 David Frech. (Read the LICENSE for details.)
| I couldn't take it any more - the 908 ROM monitor was driving me crazy.
| It's slow, and there is still an odd bug with the PC - the "image" of it on
| the stack frame - not getting properly initialised... but only when the
| target is first connected!
|
| I decided to try out some simple ideas. Instead of the Nibble Machines
| loader, we have this! Rather than a Bootloader, it's a Byteloader?
loading S08 Chat v3 (host)
decimal
| Mostly reorganised, eliminating HXtoSP - and therefore SetSP. Implementation
| on the target uses subroutines, rather than being one big loop. Since I keep
| changing this, I thought from now on I'd include a protocol version command -
| which will always be command #1!! It returns 32 bits of the muforth commit, in
| big-endian order.
( chat protocol v3)
: c.Idle 00 send ; ( command 0 is ignored by the chat target)
: c.Version ?spkt 01 send ; ( point to chat firmware version commit)
: c.Run ?spkt 02 send ; ( don't walk! and don't wait for the target. ;-)
: c.GetSP ?spkt 03 send recv recv hilo> ;
: c.SetHX ?spkt 04 send >lohi send send ;
: c.ReadNext ?spkt 05 send recv ;
: c.WriteNext ?spkt 06 send send ;
: c.Stream ?spkt 07 send send ; ( "stream" N bytes from memory)
: c.FlashNext ?spkt 08 send send recv ; ( flash one byte, get flash status)
| Resynchronise the protocol - just to make sure. We send two dummy bytes,
| in case we were doing a SetHX. We wait for them to transmit. Then we
| throw away any unread input. This puts us back into a known state.
: resync ?spkt c.Idle c.Idle drain flush ;
| We resync right before reading from a new memory address, and when
| getting or setting the SP. We don't do these all the time, but when we
| do, we'd like to get the right addresses! By "sprinkling" these protocol
| resets throughtout the interaction, it's difficult for the two machines
| to stay out of whack for very long. Doing a "du" or ".regs" usually
| resets things.
: c.SetAddr resync c.SetHX ;
: GetSP resync c.GetSP ;
: chat-c@ c.SetAddr c.ReadNext ;
: chat-c! c.SetAddr c.WriteNext ;
.ifndef m&
| We load this file just to run HC08 code to, eg, talk to AVR targets, so
| we won't have the version in target/S08/memory.mu4. If the other target
| hasn't loaded the equivalent, we define our own version here.
| Generic tools for reading and writing host-side memory. These give us a
| second "hand" so we can, eg, compare two images using c@+ on the one hand
| and m* on the other.
variable m ( memory pointer)
: m* ( - byte) m @ c@ 1 m +! ;
: m& ( byte) m @ c! 1 m +! ;
.then ( ifndef m&)
| This can read from and write to arbitrary buffers on the host side, not
| just pieces of the image.
: c.setup-chunk ( 'target len buffer - len)
m ! swap c.SetAddr ;
.ifdef S08
( Use streaming read.)
: c.ReadChunk ( 'target len buffer)
c.setup-chunk =if dup c.Stream for recv m& next ^ then drop ;
.ifdef usb-debug-via-serial
: c.FlashChunk complain ; ( we should be using USB to flash when running both)
.else
: flash-chunk-until-error ( len - status)
for m* c.FlashNext dup "c0 xor if rdrop ^ then drop next
"c0 ;
: c.FlashChunk ( 'target len buffer cmd - status)
"30 \eq FSTAT chat-c! ( clear flash errors)
\m flash-command chat-c! c.setup-chunk
flash-chunk-until-error
00 \m flash-command chat-c! ;
.then ( usb-debug-via-serial)
.else ( 908 version without streaming read)
: c.ReadChunk ( 'target len buffer)
c.setup-chunk for c.ReadNext m& next ;
.then
: c.WriteChunk ( 'target len buffer)
c.setup-chunk for m* c.WriteNext next ;
: c.GetRegs ( buf - sp)
GetSP dup push 6 rot c.ReadChunk pop ;
: c.SetRegs ( buf)
GetSP 6 rot c.WriteChunk ;
( Quick kludge to run code without loading the whole HC08 environment.)
: c.SetPC ( pc)
GetSP 4 + c.SetAddr >lohi c.WriteNext c.WriteNext ;
.ifdef t.Hello ( only if we've loaded the interact code)
: c.Hello
.ifdef chat-slow ( using a xtal that only allows 38400)
38400 bps
.else
115200 bps
.then
resync ( exit BDM loop, if running; resync chat protocol)
c.Version 4 c.Stream recv recv recv recv 3210>
cr ." Chat firmware version "
radix preserve hex sep preserve -sep <# 4# 4# #> type
( Copy flash routine to RAM.) copy-flash-routine ;
.ifdef S08
: chat
chat-via c.Hello c.GetRegs c.SetRegs c.Run
c.ReadChunk c.WriteChunk c.FlashChunk ;
.else
: chat
chat-via c.Hello c.GetRegs c.SetRegs c.Run
c.ReadChunk c.WriteChunk complain ;
.then
.then