Subversion Repositories svn.mios

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
44 tk 1
; $Id: cs_menu_mbnet.inc 44 2008-01-30 21:39:30Z tk $
1 tk 2
;
3
;  MBNET communication routines for MBSID Control Surface
4
;
5
;  See also mbnet.inc and sid_mbnet.inc
6
;
7
; ==========================================================================
8
;
9
;  Copyright 1998-2007 Thorsten Klose (tk@midibox.org)
10
;  Licensed for personal non-commercial use only.
11
;  All other rights reserved.
12
;
13
; ==========================================================================
14
 
15
 
16
					;;    <------------------>
17
TEXT_CS_MENU_MBNET_NOT_COMP  STRING 20, 0x00, "SIDx not compatible "
18
TEXT_CS_MENU_MBNET_NOT_AVAIL STRING 20, 0x00, "SIDx not available  "
19
					;;   12341234123412341234
20
 
21
TEXT_CS_MENU_MBNET_NO_RESP   STRING 20, 0x40, "(No MBNet Response) "
22
TEXT_CS_MENU_MBNET_BUSERR    STRING 20, 0x40, "(CAN Bus Errors)    "
23
TEXT_CS_MENU_MBNET_DISABLED  STRING 20, 0x40, "(CAN disabled!)     "
24
 
25
 
26
;; --------------------------------------------------------------------------
27
;;  This function is called from CS_MENU_Handler_Init to check, if a CAN node
28
;;  is available - if not, it disables the SID and prints an error message
29
;;  on LCD
30
;;  OUT: clears CS_STAT, CS_STAT_SID_AVAILABLE if SID not available
31
;; --------------------------------------------------------------------------
32
CS_MENU_MBNET_Check
33
	;; note that we also check the availability of the master here - this seems to be perverse, but it is just a debugging help for myself ;-)
34
	call	CS_MENU_MS_GetSIDNumber
35
	call	MIOS_HLP_GetBitORMask
36
	SET_BSR	MBNET_BASE
37
	andwf	MBNET_NODE_AVAIL, W, BANKED
38
	bnz	CS_MENU_MBNET_Check_NodeOk
39
 
40
	;; notify that SID not available
41
	bcf	CS_STAT, CS_STAT_SID_AVAILABLE
42
 
43
	;; branch if MBNet has been permanently disabled
44
	BRA_IFSET MBNET_STATE, MBNET_STATE_PERMANENT_OFF, BANKED, CS_MENU_MBNET_Check_NotAvail
45
 
46
	;; branch if we have determined that the node is not compatible
47
	movf	CS_MENU_SID, W
48
	call	MIOS_HLP_GetBitORMask
49
	SET_BSR	MBNET_BASE
50
	andwf	MBNET_NODE_INCOMPATIBLE, W, BANKED
51
	bz	CS_MENU_MBNET_Check_NotAvail
52
 
53
CS_MENU_MBNET_Check_Incompatible
54
	;; print message
55
	TABLE_ADDR TEXT_CS_MENU_MBNET_NOT_COMP
56
	call	MIOS_LCD_PrintString
57
	;; print node info at the bottom
58
	movlw	0x40
59
	call	MIOS_LCD_CursorSet
60
	movf	CS_MENU_SID, W
61
	rcall	CS_MENU_MBNET_PrnInfo
62
	rgoto	CS_MENU_MBNET_Check_Cont
63
 
64
CS_MENU_MBNET_Check_NotAvail
65
	;; print message
66
	TABLE_ADDR TEXT_CS_MENU_MBNET_NOT_AVAIL
67
	call	MIOS_LCD_PrintString
68
 
69
	;; branch if MBNet has been permanently disabled
70
	SET_BSR	MBNET_BASE
71
	BRA_IFSET MBNET_STATE, MBNET_STATE_PERMANENT_OFF, BANKED, CS_MENU_MBNET_Check_Disabled
72
	rgoto	CS_MENU_MBNET_Check_NotAvailP
73
CS_MENU_MBNET_Check_Disabled
74
	TABLE_ADDR TEXT_CS_MENU_MBNET_DISABLED
75
	rgoto	CS_MENU_MBNET_Check_DontPanic
76
 
77
CS_MENU_MBNET_Check_NotAvailP
78
	BRA_IFCLR MBNET_STATE, MBNET_STATE_PANIC, BANKED, CS_MENU_MBNET_Check_DontPanic
79
CS_MENU_MBNET_Check_Panic
80
	TABLE_ADDR TEXT_CS_MENU_MBNET_BUSERR
81
CS_MENU_MBNET_Check_DontPanic
82
	call	MIOS_LCD_PrintString
83
 
84
CS_MENU_MBNET_Check_Cont
85
	;; print SID number
86
	movlw	0x00 + 3
87
	call	MIOS_LCD_CursorSet
88
	incf	CS_MENU_SID, W
89
	call	MIOS_LCD_PrintBCD1
90
 
91
	return
92
 
93
CS_MENU_MBNET_Check_NodeOk
94
	return
95
 
96
 
97
;; --------------------------------------------------------------------------
98
;;  This function is called when a SID button is pressed
99
;;  It re-enables a CAN node if it has vanished from the network
100
;; --------------------------------------------------------------------------
101
CS_MENU_MBNET_Retry
102
	;; check current avail status
103
	call	CS_MENU_MS_GetSIDNumber	; now in WREG and CS_MENU_SID
104
	call	MIOS_HLP_GetBitORMask
105
	SET_BSR	MBNET_BASE
106
	andwf	MBNET_NODE_AVAIL, W, BANKED
107
	bnz	CS_MENU_MBNET_Retry_NodeOk
108
 
109
CS_MENU_MBNET_Retry_NodeVanished
110
	;; request a re-synchronisation
111
	movf	CS_MENU_SID, W
112
	call	MBNET_RequestSync
113
 
114
CS_MENU_MBNET_Retry_NodeOk
115
	return
116
 
117
 
118
;; --------------------------------------------------------------------------
119
;;  This function is called from MBNET_ConHandler when a new CAN node has
120
;;  been detected
121
;;  IN: CAN Node number (1-7) in WREG and MIOS_PARAMETER1
122
;;      CAN Node ping response in MBNET_NODE_INFO
123
;; --------------------------------------------------------------------------
124
CS_MENU_MBNET_FoundNode
125
	;; transfer node (=slave) number in CS_MENU_SID
126
	movwf	CS_MENU_SID
127
 
128
	;; request patch update
129
	andlw	0x03
130
	lfsr	FSR1, CS_MENU_SID_M_PATCH
131
	bcf	PLUSW1, 7
132
	call	CS_MENU_MS_Send_SysExDump_TxReq
133
 
134
	;; send ensemble settings
135
	movf	CS_MENU_SID, W
136
	call	SID_ENS_LoadSIDSetup
137
	call	CS_MENU_MBNET_Tx_SendEns
138
 
139
	;; re-init ensemble buffer based on selected SID
140
	call	CS_MENU_MS_UpdateEnsBuffer
141
 
142
	;; re-init display
143
	bsf	CS_STAT, CS_STAT_DISPLAY_INIT_REQ
144
 
145
	return
146
 
147
 
148
;; --------------------------------------------------------------------------
149
;;  Print the information of a given SID node
150
;;  IN: node number in WREG
151
;;  OUT: 20 characters: "Pxxx xxxx Vxxx.xxxxx"
152
;;  USES: FSR0, PRODL
153
;; --------------------------------------------------------------------------
154
CS_MENU_MBNET_PrnInfo
155
	;; calculate pointer to info array
156
	lfsr	FSR0, MBNET_NODE_INFO_BEGIN
157
	rlf	WREG, W
158
	rlf	WREG, W
159
	rlf	WREG, W
160
	andlw	0x38
161
	addwf	FSR0L, F
162
 
163
	movlw	'P'
164
	call	MIOS_LCD_PrintChar
165
	movf	POSTINC0, W
166
	clrf	MIOS_PARAMETER1
167
	call	MIOS_HLP_Dec2BCD
168
	movf	MIOS_PARAMETER2, W
169
	call	MIOS_LCD_PrintHex1
170
	movf	MIOS_PARAMETER1, W
171
	call	MIOS_LCD_PrintHex2
172
 
173
	movlw	' '
174
	call	MIOS_LCD_PrintChar
175
 
176
	movlw	4
177
	movwf	PRODL
178
CS_MENU_MBNET_PrnInfoLoop
179
	movf	POSTINC0, W
180
	skpnz
181
	movlw	'?'
182
	call	MIOS_LCD_PrintChar
183
	decfsz	PRODL, F
184
	rgoto	CS_MENU_MBNET_PrnInfoLoop
185
 
186
	movlw	' '
187
	call	MIOS_LCD_PrintChar
188
 
189
	movlw	'V'
190
	call	MIOS_LCD_PrintChar
191
	movf	POSTINC0, W
192
	clrf	MIOS_PARAMETER1
193
	call	MIOS_HLP_Dec2BCD
194
	movf	MIOS_PARAMETER2, W
195
	call	MIOS_LCD_PrintHex1
196
	movf	MIOS_PARAMETER1, W
197
	call	MIOS_LCD_PrintHex2
198
 
199
	movlw	'.'
200
	call	MIOS_LCD_PrintChar
201
 
202
	movf	POSTINC0, W
203
	movff	POSTINC0, MIOS_PARAMETER1
204
	call	MIOS_HLP_Dec2BCD
205
	movf	MIOS_PARAMETER3, W
206
	call	MIOS_LCD_PrintHex1
207
	movf	MIOS_PARAMETER2, W
208
	call	MIOS_LCD_PrintHex2
209
	movf	MIOS_PARAMETER1, W
210
	call	MIOS_LCD_PrintHex2
211
	return
212
 
213
 
214
;; --------------------------------------------------------------------------
215
;;  sends a single event to USER_MPROC_NotifyReceivedEvent of a slave
216
;;  IN: slave target in CS_MENU_SID
217
;;      MIDI event in MIOS_PARAMETER[123]
218
;; --------------------------------------------------------------------------
219
CS_MENU_MBNET_Tx_SendEvent
220
	movff	CS_MENU_SID, MBNET_SLAVE_ID	; prepare transmission
221
	SET_BSR	MBNET_BASE			; EID[LH] always 0
222
	movlw	6				; ETOS=6
223
	movwf	MBNET_EID_L, BANKED
224
	clrf	MBNET_EID_H, BANKED
225
	movlw	0				; TOS
226
	call	MBNET_Tx_Prepare			; returns pointer to DLC of Tx buffer in FSR1
227
	bnz	CS_MENU_MBNET_Tx_SendEvent_End	; skip if slave not available
228
 
229
	movlw	3				; 3 additional byte to send
230
	movwf	POSTINC1			; DLC
231
	movff	MIOS_PARAMETER1, POSTINC1	; D0
232
	movff	MIOS_PARAMETER2, POSTINC1	; D1
233
	movff	MIOS_PARAMETER3, POSTINC1	; D2
234
 
235
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
236
	skpz					; re-init display if transfer failed
237
	rgoto	CS_MENU_MBNET_Error
238
 
239
CS_MENU_MBNET_Tx_SendEvent_End
240
	return
241
 
242
 
243
;; --------------------------------------------------------------------------
244
;;  sends a sound parameter
245
;;  IN: slave target in CS_MENU_SID
246
;;      parameter address in MIOS_PARAMETER[12]
247
;;      parameter value in WREG (will be copied to MIOS_PARAMETER3)
248
;; --------------------------------------------------------------------------
249
CS_MENU_MBNET_Tx_SendPar
250
	movwf	MIOS_PARAMETER3
251
	movff	CS_MENU_SID, MBNET_SLAVE_ID	; prepare transmission
252
	SET_BSR	MBNET_BASE			; EID[LH] always 0
253
	movlw	18				; ETOS=18
254
	movwf	MBNET_EID_L, BANKED
255
	clrf	MBNET_EID_H, BANKED
256
	movlw	0				; TOS
257
	call	MBNET_Tx_Prepare			; returns pointer to DLC of Tx buffer in FSR1
258
	bnz	CS_MENU_MBNET_Tx_SendPar_End	; skip if slave not available
259
 
260
	movlw	4				; 4 bytes to send
261
	movwf	POSTINC1			; DLC
262
	movff	MIOS_PARAMETER1, POSTINC1	; D0
263
	movff	MIOS_PARAMETER2, POSTINC1	; D1
264
	movff	MIOS_PARAMETER3, POSTINC1	; D3
265
	clrf	POSTINC1			; D4 (high-byte of parameter, always 0)
266
 
267
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
268
	skpz					; re-init display if transfer failed
269
	rgoto	CS_MENU_MBNET_Error
270
 
271
CS_MENU_MBNET_Tx_SendPar_End
272
	return
273
 
274
 
275
;; --------------------------------------------------------------------------
276
;;  sends an ensemble parameter
277
;;  IN: slave target in CS_MENU_SID
278
;;      parameter address in MIOS_PARAMETER1
279
;;      parameter value in WREG (will be copied to MIOS_PARAMETER3)
280
;; --------------------------------------------------------------------------
281
CS_MENU_MBNET_Tx_SendEPar
282
	movwf	MIOS_PARAMETER3
283
	movff	CS_MENU_SID, MBNET_SLAVE_ID	; prepare transmission
284
	SET_BSR	MBNET_BASE			; EID[LH] always 0
285
	movlw	17				; ETOS=17
286
	movwf	MBNET_EID_L, BANKED
287
	clrf	MBNET_EID_H, BANKED
288
	movlw	0				; TOS
289
	call	MBNET_Tx_Prepare		; returns pointer to DLC of Tx buffer in FSR1
290
	bnz	CS_MENU_MBNET_Tx_SendEPar_End	; skip if slave not available
291
 
292
	movlw	4				; 4 bytes to send
293
	movwf	POSTINC1			; DLC
294
	movff	MIOS_PARAMETER1, POSTINC1	; D0
295
	clrf	POSTINC1			; D1 (high-byte of address, always 0)
296
	movff	MIOS_PARAMETER3, POSTINC1	; D3
297
	clrf	POSTINC1			; D4 (high-byte of parameter, always 0)
298
 
299
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
300
	skpz					; re-init display if transfer failed
301
	rgoto	CS_MENU_MBNET_Error
302
 
303
CS_MENU_MBNET_Tx_SendEPar_End
304
	return
305
 
306
 
307
;; --------------------------------------------------------------------------
308
;;  temporary routine which allows to write into bank 0 if the slave
309
;;  (currently used to change the MIDI channels)
310
;;  IN: slave target in CS_MENU_SID
311
;;      bank 0 address in MIOS_PARAMETER1
312
;;      value in WREG (will be copied to MIOS_PARAMETER3)
313
;; --------------------------------------------------------------------------
314
CS_MENU_MBNET_Tx_SendBank0
315
	movwf	MIOS_PARAMETER3
316
	movff	CS_MENU_SID, MBNET_SLAVE_ID	; prepare transmission
317
	SET_BSR	MBNET_BASE			; EID[LH] always 0
318
	movlw	28				; ETOS=28 (tmp)
319
	movwf	MBNET_EID_L, BANKED
320
	clrf	MBNET_EID_H, BANKED
321
	movlw	0				; TOS
322
	call	MBNET_Tx_Prepare		; returns pointer to DLC of Tx buffer in FSR1
323
	bnz	CS_MENU_MBNET_Tx_SendBank0_End	; skip if slave not available
324
 
325
	movlw	4				; 4 bytes to send
326
	movwf	POSTINC1			; DLC
327
	movff	MIOS_PARAMETER1, POSTINC1	; D0
328
	clrf	POSTINC1			; D1 (high-byte of address, always 0)
329
	movff	MIOS_PARAMETER3, POSTINC1	; D3
330
	clrf	POSTINC1			; D4 (high-byte of parameter, always 0)
331
 
332
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
333
	skpz					; re-init display if transfer failed
334
	rgoto	CS_MENU_MBNET_Error
335
 
336
CS_MENU_MBNET_Tx_SendBank0_End
337
	return
338
 
339
 
340
;; --------------------------------------------------------------------------
341
;;  sends a knob value
342
;;  IN: slave target in CS_MENU_SID
343
;;      knob number in MIOS_PARAMETER1
344
;;      knob value in WREG (will be copied to MIOS_PARAMETER3)
345
;; --------------------------------------------------------------------------
346
CS_MENU_MBNET_Tx_SendKnob
347
	movwf	MIOS_PARAMETER3
348
	movff	CS_MENU_SID, MBNET_SLAVE_ID	; prepare transmission
349
	SET_BSR	MBNET_BASE			; EID[LH] always 0
350
	movlw	19				; ETOS=19
351
	movwf	MBNET_EID_L, BANKED
352
	clrf	MBNET_EID_H, BANKED
353
	movlw	0				; TOS
354
	call	MBNET_Tx_Prepare			; returns pointer to DLC of Tx buffer in FSR1
355
	bnz	CS_MENU_MBNET_Tx_SendKnob_End	; skip if slave not available
356
 
357
	movlw	4				; 4 bytes to send
358
	movwf	POSTINC1			; DLC
359
	movff	MIOS_PARAMETER1, POSTINC1	; D0
360
	clrf	POSTINC1			; D1 (high-byte of address, always 0)
361
	movff	MIOS_PARAMETER3, POSTINC1	; D3
362
	clrf	POSTINC1			; D4 (high-byte of parameter, always 0)
363
 
364
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
365
	skpz					; re-init display if transfer failed
366
	rgoto	CS_MENU_MBNET_Error
367
 
368
CS_MENU_MBNET_Tx_SendKnob_End
369
	return
370
 
371
 
372
;; --------------------------------------------------------------------------
373
;;  updates a knob value
374
;;  IN: slave target in CS_MENU_SID
375
;;      knob number in MIOS_PARAMETER1
376
;; --------------------------------------------------------------------------
377
CS_MENU_MBNET_Tx_UpdateKnob
378
	movff	CS_MENU_SID, MBNET_SLAVE_ID	; prepare transmission
379
	SET_BSR	MBNET_BASE			; EID[LH] always 0
380
	movlw	20				; ETOS=20
381
	movwf	MBNET_EID_L, BANKED
382
	clrf	MBNET_EID_H, BANKED
383
	movlw	0				; TOS
384
	call	MBNET_Tx_Prepare			; returns pointer to DLC of Tx buffer in FSR1
385
	bnz	CS_MENU_MBNET_Tx_UpdateKnob_End	; skip if slave not available
386
 
387
	movlw	1				; 1 byte to send
388
	movwf	POSTINC1			; DLC
389
	movff	MIOS_PARAMETER1, POSTINC1	; D0
390
 
391
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
392
	skpz					; re-init display if transfer failed
393
	rgoto	CS_MENU_MBNET_Error
394
 
395
CS_MENU_MBNET_Tx_UpdateKnob_End
396
	return
397
 
398
 
399
;; --------------------------------------------------------------------------
400
;;  Special Command
401
;;  IN: slave target in CS_MENU_SID
402
;;      ETOS in WREG
403
;; --------------------------------------------------------------------------
404
CS_MENU_MBNET_Tx_SpecialCmd
405
	SET_BSR	MBNET_BASE			; command located in EIDL
406
	movwf	MBNET_EID_L, BANKED
407
	clrf	MBNET_EID_H, BANKED		; EIDH always 0
408
	movff	CS_MENU_SID, MBNET_SLAVE_ID	; prepare transmission
409
	movlw	0				; TOS
410
	call	MBNET_Tx_Prepare		; returns pointer to DLC of Tx buffer in FSR1
411
	bnz	CS_MENU_MBNET_Tx_SpecialCmd_End	; skip if slave not available
412
 
413
	movlw	0				; no byte to send
414
	movwf	POSTINC1			; DLC
415
 
416
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
417
	skpz					; re-init display if transfer failed
418
	rgoto	CS_MENU_MBNET_Error
419
 
420
CS_MENU_MBNET_Tx_SpecialCmd_End
421
	return
422
 
423
;; --------------------------------------------------------------------------
424
;;  ETOS=0: lock slave receiver
425
;;  IN: slave target in CS_MENU_SID
426
;; --------------------------------------------------------------------------
427
CS_MENU_MBNET_Tx_LockSlaveRx
428
	movlw	0
429
	rgoto	CS_MENU_MBNET_Tx_SpecialCmd
430
 
431
;; --------------------------------------------------------------------------
432
;;  ETOS=1: unlock slave receiver
433
;;  IN: slave target in CS_MENU_SID
434
;; --------------------------------------------------------------------------
435
CS_MENU_MBNET_Tx_UnlockSlaveRx
436
	movlw	1
437
	rgoto	CS_MENU_MBNET_Tx_SpecialCmd
438
 
439
;; --------------------------------------------------------------------------
440
;;  ETOS=16: init patch
441
;;  IN: slave target in CS_MENU_SID
442
;; --------------------------------------------------------------------------
443
CS_MENU_MBNET_Tx_InitPatch
444
	movlw	16
445
	rgoto	CS_MENU_MBNET_Tx_SpecialCmd
446
 
447
;; --------------------------------------------------------------------------
448
;;  ETOS=23: init ensemble
449
;;  IN: slave target in CS_MENU_SID
450
;; --------------------------------------------------------------------------
451
CS_MENU_MBNET_Tx_InitEns
452
	movlw	23
453
	rgoto	CS_MENU_MBNET_Tx_SpecialCmd
454
 
455
 
456
;; --------------------------------------------------------------------------
457
;;  fills the remote buffer with a patch from a slave --> stepwise <--
458
;;  fetches 32 byte blocks; immediate address stored in CS_MENU_GET_PATCH_ADDR_[LH]
459
;;  The CS_MENU_MBNET_Tx_GetPatchChk function has to be called periodically
460
;;  to check if the patch is complete
461
;;  IN: slave target in CS_MENU_SID
462
;;  OUT: ZERO flag set if patch is complete (no block has to be fetched)
463
;; --------------------------------------------------------------------------
464
CS_MENU_MBNET_Tx_GetPatchChk
465
	;; corner case: abort if master has been selected meanwhile
466
	movf	CS_MENU_SID, W
467
	bnz	CS_MENU_MBNET_Tx_GetPatchChk_Slv
468
CS_MENU_MBNET_Tx_GetPatchChk_Mst
469
	;; zero patch address so that no additional block will be requested
470
	clrf	CS_MENU_GET_PATCH_ADDR_L
471
	clrf	CS_MENU_GET_PATCH_ADDR_H
472
	andlw	0x00
473
	return
474
 
475
CS_MENU_MBNET_Tx_GetPatchChk_Slv
476
	;; check if the patch is complete (address is zero)
477
	movf	CS_MENU_GET_PATCH_ADDR_L, W
478
	iorwf	CS_MENU_GET_PATCH_ADDR_H, W
479
	bnz	CS_MENU_MBNET_Tx_GetPatch_Cont	; continue fetching
480
 
481
	;; set ZERO flag to notify, that patch is complete
482
	andlw	0x00
483
	return
484
 
485
CS_MENU_MBNET_Tx_GetPatch
486
	clrf	CS_MENU_GET_PATCH_ADDR_L
487
	clrf	CS_MENU_GET_PATCH_ADDR_H
488
 
489
CS_MENU_MBNET_Tx_GetPatch_Cont
490
	;; lock receive handler
491
	call	CS_MENU_MBNET_Tx_LockSlaveRx
492
	bnz	CS_MENU_MBNET_Tx_GetPatch_Abort	; abort if slave not available (branch to UnlockRx to give slave a chance to recover)
493
 
494
	;; we are doing a continuous read, so address only needs to be set at the beginning
495
	;; optimized Slave Patch Buffer->CAN->Master Edit Buffer transfer
496
CS_MENU_MBNET_Tx_GetPatch_LoopO
497
	movff	CS_MENU_GET_PATCH_ADDR_L, MBNET_EID_L	; EIDL contains low-byte of address
498
	movff	CS_MENU_GET_PATCH_ADDR_H, MBNET_EID_H	; EIDH contains high-byte of address
499
	movlw	MBNET_TOS_RAM_READ		; TOS for reading RAM
500
	call	MBNET_Tx_Prepare		; returns pointer to DLC of Tx buffer in FSR1
501
	bnz	CS_MENU_MBNET_Tx_GetPatch_End	; skip if slave not available
502
 
503
	movlw	0				; DLC always 0
504
	movwf	POSTINC1
505
 
506
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
507
	skpz					; re-init display if transfer failed
508
	rgoto	CS_MENU_MBNET_Error
509
 
510
	;; check that we received 8 bytes
511
	lfsr	FSR1, B5DLC
512
	movf	POSTINC1, W
513
	xorlw	8
514
	skpz
515
	rgoto	CS_MENU_MBNET_Error
516
 
517
	;; copy bytes into edit buffer
518
	movff	CS_MENU_GET_PATCH_ADDR_L, FSR0L
519
	movf	CS_MENU_GET_PATCH_ADDR_H, W
520
	andlw	0x01
521
	addlw	HIGH(SID_EDIT_BUFFER)
522
	movwf	FSR0H
523
 
524
	movff	POSTINC1, POSTINC0
525
	movff	POSTINC1, POSTINC0
526
	movff	POSTINC1, POSTINC0
527
	movff	POSTINC1, POSTINC0
528
	movff	POSTINC1, POSTINC0
529
	movff	POSTINC1, POSTINC0
530
	movff	POSTINC1, POSTINC0
531
	movff	POSTINC1, POSTINC0
532
 
533
	movlw	8
534
	addwf	CS_MENU_GET_PATCH_ADDR_L, F
535
	skpnz
536
	incf	CS_MENU_GET_PATCH_ADDR_H, F
537
 
538
	;; loop until a 32 byte block has been received
539
	movf	CS_MENU_GET_PATCH_ADDR_L, W
540
	andlw	0x1f
541
	bnz	CS_MENU_MBNET_Tx_GetPatch_LoopO
542
 
543
	;; check if last block has been received
544
	movf	CS_MENU_GET_PATCH_ADDR_L, W
545
	bnz	CS_MENU_MBNET_Tx_GetPatch_Yield
546
	movf	CS_MENU_GET_PATCH_ADDR_H, W
547
	andlw	0xfe
548
	bz	CS_MENU_MBNET_Tx_GetPatch_Yield
549
 
550
CS_MENU_MBNET_Tx_GetPatch_Abort
551
	;; zero patch address so that no additional block will be requested
552
	clrf	CS_MENU_GET_PATCH_ADDR_L
553
	clrf	CS_MENU_GET_PATCH_ADDR_H
554
 
555
CS_MENU_MBNET_Tx_GetPatch_Yield
556
	call	CS_MENU_MBNET_Tx_UnlockSlaveRx	; unlock receive handler
557
 
558
CS_MENU_MBNET_Tx_GetPatch_End
559
	;; clear ZERO flag to notify, that patch is not complete
560
	iorlw	0xff
561
	return
562
 
563
 
564
;; --------------------------------------------------------------------------
565
;;  fills the remote buffer for ensembles with data from slave
566
;;  IN: slave target in CS_MENU_SID
567
;; --------------------------------------------------------------------------
568
CS_MENU_MBNET_Tx_GetEns
569
	;; lock receive handler
570
	call	CS_MENU_MBNET_Tx_LockSlaveRx
571
	bnz	CS_MENU_MBNET_Tx_GetEns_Abort	; abort if slave not available (branch to UnlockRx to give slave a chance to recover)
572
 
573
	;; using EEADR as loop counter
574
	clrf	EEADR
575
	;; optimized BankStick->CAN->Slave Patch Buffer transfer
576
CS_MENU_MBNET_Tx_GetEns_LoopO
577
	movff	EEADR, MBNET_EID_L		; EIDL contains low-byte of address
578
	movlw	0xff				; EIDH contains high-byte of address, 0xff for ensembles!
579
	movff	WREG, MBNET_EID_H
580
	movlw	MBNET_TOS_RAM_READ		; TOS for reading RAM
581
	call	MBNET_Tx_Prepare		; returns pointer to DLC of Tx buffer in FSR1
582
	bnz	CS_MENU_MBNET_Tx_GetEns_End	; skip if slave not available
583
 
584
	movlw	0				; DLC always 0
585
	movwf	POSTINC1
586
 
587
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
588
	skpz					; re-init display if transfer failed
589
	rgoto	CS_MENU_MBNET_Error
590
 
591
	;; check that we received 8 bytes
592
	lfsr	FSR1, B5DLC
593
	movf	POSTINC1, W
594
	xorlw	8
595
	skpz
596
	rgoto	CS_MENU_MBNET_Error
597
 
598
	;; copy bytes into ensemble edit buffer
599
	lfsr	FSR0, SID_ENS_BUFFER
600
	movf	EEADR, W
601
	addwf	FSR0L, F
602
 
603
	movff	POSTINC1, POSTINC0
604
	movff	POSTINC1, POSTINC0
605
	movff	POSTINC1, POSTINC0
606
	movff	POSTINC1, POSTINC0
607
	movff	POSTINC1, POSTINC0
608
	movff	POSTINC1, POSTINC0
609
	movff	POSTINC1, POSTINC0
610
	movff	POSTINC1, POSTINC0
611
 
612
	movlw	8
613
	addwf	EEADR, F
614
 
615
	;; loop until 64 byte have been sent (2 iterations)
616
	BRA_IFCLR EEADR, 6, ACCESS, CS_MENU_MBNET_Tx_GetEns_LoopO
617
 
618
CS_MENU_MBNET_Tx_GetEns_Abort
619
	call	CS_MENU_MBNET_Tx_UnlockSlaveRx	; unlock receive handler
620
 
621
CS_MENU_MBNET_Tx_GetEns_End
622
	return
623
 
624
 
625
;; --------------------------------------------------------------------------
626
;;  ETOS=21: update shadow buffer
627
;;  IN: slave target in CS_MENU_SID
628
;; --------------------------------------------------------------------------
629
CS_MENU_MBNET_Tx_UpdateShadow
630
	movlw	21
631
	rgoto	CS_MENU_MBNET_Tx_SpecialCmd
632
 
633
;; --------------------------------------------------------------------------
634
;;  ETOS=22: disable SID engine
635
;;  IN: slave target in CS_MENU_SID
636
;; --------------------------------------------------------------------------
637
CS_MENU_MBNET_Tx_DisableEngine
638
	movlw	22
639
	rgoto	CS_MENU_MBNET_Tx_SpecialCmd
640
 
641
 
642
;; --------------------------------------------------------------------------
643
;;  ETOS=24: All Notes On command
644
;;  IN: slave target in CS_MENU_SID
645
;;      current instrument in CS_MENU_SELECTED_INS
646
;;      current L/R selection in CS_MENU_SELECTED_SID_LR
647
;; --------------------------------------------------------------------------
648
CS_MENU_MBNET_Tx_NotesOn
649
	movff	CS_MENU_SID, MBNET_SLAVE_ID	; prepare transmission
650
	SET_BSR	MBNET_BASE			; EID[LH] always 0
651
	movlw	24				; ETOS=24
652
	movwf	MBNET_EID_L, BANKED
653
	clrf	MBNET_EID_H, BANKED
654
	movlw	0				; TOS
655
	call	MBNET_Tx_Prepare		; returns pointer to DLC of Tx buffer in FSR1
656
	bnz	CS_MENU_MBNET_Tx_NotesOn_End	; skip if slave not available
657
 
658
	movlw	2				; 2 bytes to send
659
	movwf	POSTINC1			; DLC
660
	movff	CS_MENU_SELECTED_INS, POSTINC1	; D0
661
	movff	CS_MENU_SELECTED_SID_LR, POSTINC1 ; D1
662
 
663
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
664
	skpz					; re-init display if transfer failed
665
	rgoto	CS_MENU_MBNET_Error
666
 
667
CS_MENU_MBNET_Tx_NotesOn_End
668
	return
669
 
670
;; --------------------------------------------------------------------------
671
;;  ETOS=25: All Notes Off command
672
;;  IN: slave target in CS_MENU_SID
673
;; --------------------------------------------------------------------------
674
CS_MENU_MBNET_Tx_NotesOff
675
	movlw	25
676
	rgoto	CS_MENU_MBNET_Tx_SpecialCmd
677
 
678
;; --------------------------------------------------------------------------
679
;;  ETOS=26: Voice ReInit command
680
;;  IN: slave target in CS_MENU_SID
681
;; --------------------------------------------------------------------------
682
CS_MENU_MBNET_Tx_VoiceReInit
683
	movlw	26
684
	rgoto	CS_MENU_MBNET_Tx_SpecialCmd
685
 
686
;; --------------------------------------------------------------------------
687
;;  ETOS=27: Superpoly ReInit command
688
;;  IN: slave target in CS_MENU_SID
689
;; --------------------------------------------------------------------------
690
CS_MENU_MBNET_Tx_SP_ReInit
691
	movlw	27
692
	rgoto	CS_MENU_MBNET_Tx_SpecialCmd
693
 
694
;; --------------------------------------------------------------------------
695
;;  sends a patch to a given slave
696
;;  IN: slave target in CS_MENU_SID
697
;;      BankStick patch and bank in SID_PATCH/SID_BANK
698
;; --------------------------------------------------------------------------
699
CS_MENU_MBNET_Tx_SendPatch
700
	;; lock receive handler
701
	call	CS_MENU_MBNET_Tx_LockSlaveRx
702
	bnz	CS_MENU_MBNET_Tx_SendPatch_Abort	; abort if slave not available (branch to UnlockRx to give slave a chance to recover)
703
 
704
	;; branch to special sending routine if patch not valid, or Patch #0 selected
705
	call	SID_PBANK_CheckPatchValid
706
	bnz	CS_MENU_MBNET_Tx_SendPatch_Int
707
	movf	SID_PATCH, W
708
	bz	CS_MENU_MBNET_Tx_SendPatch_Int
709
 
710
CS_MENU_MBNET_Tx_SendPatch_Ext
711
	;; select BankStick address
712
	;; we are doing a continuous read, so address only needs to be set at the beginning
713
	clrf	EEADR
714
	clrf	EEADRH
715
	call	SID_PBANK_SetBankStickAddress
716
 
717
	call	MIOS_IIC_Start			; start IIC
718
 
719
	rlf	MIOS_BOX_CFG1, W
720
	andlw	0x0e
721
	iorlw	0xa0				; set address
722
	call	MIOS_IIC_ByteSend
723
	movf	MIOS_PARAMETER2, W		; send high address
724
	call	MIOS_IIC_ByteSend
725
	movf	MIOS_PARAMETER1, W		; send low address
726
	call	MIOS_IIC_ByteSend
727
 
728
	call	MIOS_IIC_Start			; start IIC again
729
	rlf	MIOS_BOX_CFG1, W
730
	andlw	0x0e
731
	iorlw	0xa1
732
	call	MIOS_IIC_ByteSend
733
 
734
	;; optimized BankStick->CAN->Slave Patch Buffer transfer
735
	lfsr	FSR0, SID_EDIT_BUFFER		; written if slave is selected on control surface
736
CS_MENU_MBNET_Tx_SendPatchLoopO
737
	movff	EEADR, MBNET_EID_L		; EIDL contains low-byte of address
738
	movff	EEADRH, MBNET_EID_H		; EIDH contains high-byte of address
739
	movlw	MBNET_TOS_RAM_WRITE		; TOS for writing into RAM
740
	call	MBNET_Tx_Prepare		; returns pointer to DLC of Tx buffer in FSR1
741
	bnz	CS_MENU_MBNET_Tx_SendPatch_End	; skip if slave not available
742
 
743
	movlw	8				; DLC always 8
744
	movwf	POSTINC1
745
 
746
	;; read 8 bytes from BankStick and write them into data buffer
747
CS_MENU_MBNET_Tx_SendPatchLoopI
748
	;; read byte and copy into transmit buffer
749
 	call	MIOS_IIC_ByteReceive
750
	movwf	POSTINC1
751
	movwf	PRODL
752
 
753
	;; copy into edit buffer if SID is selected
754
	movf	CS_MENU_EDIT_BUFFER_SID, W
755
	xorwf	CS_MENU_SID, W
756
	skpnz
757
	movff	PRODL, POSTINC0
758
 
759
	;; increment address
760
 	incf	EEADR, F
761
	skpnz
762
	incf	EEADRH, F
763
 
764
	;; send acknowledge
765
	;; on the last address, a Nak *must* be sent, otherwise the next EEPROM write will fail
766
	movf	EEADR, W
767
	bnz	CS_MENU_MBNET_Tx_SendPatchLoopIL
768
	movf	EEADRH, W
769
	andlw	0xfe
770
	bz	CS_MENU_MBNET_Tx_SendPatchLoopIL
771
	call	MIOS_IIC_NakSend
772
	rgoto	CS_MENU_MBNET_Tx_SendPatchLoopIN
773
CS_MENU_MBNET_Tx_SendPatchLoopIL
774
	call	MIOS_IIC_AckSend
775
	;; 	rgoto	CS_MENU_MBNET_Tx_SendPatchLoopIN
776
CS_MENU_MBNET_Tx_SendPatchLoopIN
777
	movf	EEADR, W
778
	andlw	0x07
779
	bnz	CS_MENU_MBNET_Tx_SendPatchLoopI
780
 
781
	;; optimized "interlaced" transfer - send package while fetching next part from BankStick
782
	movf	EEADRH, W
783
	bnz	CS_MENU_MBNET_Tx_SendPatchLoopI1
784
	movf	EEADR, W
785
	xorlw	0x08
786
	bz	CS_MENU_MBNET_Tx_SendPatchLoopI0
787
CS_MENU_MBNET_Tx_SendPatchLoopI1
788
	call	MBNET_Tx_Perform_Finish		; waits for an acknowledge from slave
789
	skpz					; re-init display if transfer failed
790
	rgoto	CS_MENU_MBNET_Error
791
CS_MENU_MBNET_Tx_SendPatchLoopI0
792
	call	MBNET_Tx_Perform_Start		; performs the transfer
793
 
794
	;; loop until last byte has been sent
795
	movf	EEADR, W
796
	bnz	CS_MENU_MBNET_Tx_SendPatchLoopO
797
	movf	EEADRH, W
798
	andlw	0xfe
799
	bz	CS_MENU_MBNET_Tx_SendPatchLoopO
800
 
801
	;; wait for the last block
802
	call	MBNET_Tx_Perform_Finish		; waits for an acknowledge from slave
803
	skpz					; re-init display if transfer failed
804
	rgoto	CS_MENU_MBNET_Error
805
 
806
	call	MIOS_IIC_Stop			; stop IIC
807
 
808
	call	CS_MENU_MBNET_Tx_InitPatch	; init patch
809
 
810
CS_MENU_MBNET_Tx_SendPatch_Abort
811
CS_MENU_MBNET_Tx_SendPatch_Int_C
812
	call	CS_MENU_MBNET_Tx_UnlockSlaveRx	; unlock receive handler
813
 
814
CS_MENU_MBNET_Tx_SendPatch_End
815
	return
816
 
817
 
818
 
819
	;; special sending routine for internal patch
820
CS_MENU_MBNET_Tx_SendPatch_Int
821
	clrf	EEADR
822
	clrf	EEADRH
823
 
824
	;; optimized EEPROM->CAN->Slave Patch Buffer transfer
825
	lfsr	FSR0, SID_EDIT_BUFFER		; written if slave is selected on control surface
826
CS_MENU_MBNET_Tx_SendPatch_IntLO
827
	movff	EEADR, MBNET_EID_L		; EIDL contains low-byte of address
828
	movff	EEADRH, MBNET_EID_H		; EIDH contains high-byte of address
829
	movlw	MBNET_TOS_RAM_WRITE		; TOS for writing into RAM
830
	call	MBNET_Tx_Prepare			; returns pointer to DLC of Tx buffer in FSR1
831
	bnz	CS_MENU_MBNET_Tx_SendPatch_End	; skip if slave not available
832
 
833
	movlw	8				; DLC always 8
834
	movwf	POSTINC1
835
 
836
	;; read 8 bytes from EEPROM and write them into data buffer
837
CS_MENU_MBNET_Tx_SendPatch_IntLI
838
	;; read byte and copy into transmit buffer
839
	call	MIOS_EEPROM_Read
840
	movwf	POSTINC1
841
	movwf	PRODL
842
 
843
	;; increment EEADRH if EEADR is zero (not done in MIOS_EEPROM_Read due to compatibility reasons)
844
	movf	EEADR, W
845
	skpnz
846
	incf	EEADRH, F
847
 
848
	;; copy into edit buffer if SID is selected
849
	movf	CS_MENU_EDIT_BUFFER_SID, W
850
	xorwf	CS_MENU_SID, W
851
	skpnz
852
	movff	PRODL, POSTINC0
853
 
854
	movf	EEADR, W
855
	andlw	0x07
856
	bnz	CS_MENU_MBNET_Tx_SendPatch_IntLI
857
 
858
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
859
	skpz					; re-init display if transfer failed
860
	rgoto	CS_MENU_MBNET_Error
861
 
862
	;; loop until last byte has been sent
863
	movf	EEADR, W
864
	bnz	CS_MENU_MBNET_Tx_SendPatch_IntLO
865
	movf	EEADRH, W
866
	andlw	0xfe
867
	bz	CS_MENU_MBNET_Tx_SendPatch_IntLO
868
 
869
	call	CS_MENU_MBNET_Tx_InitPatch	; init patch
870
 
871
	;; continue above
872
	rgoto	CS_MENU_MBNET_Tx_SendPatch_Int_C
873
 
874
 
875
;; --------------------------------------------------------------------------
876
;;  sends the edit buffer to a given slave
877
;;  IN: slave target in CS_MENU_SID
878
;; --------------------------------------------------------------------------
879
CS_MENU_MBNET_Tx_SendEdit
880
	;; lock receive handler
881
	call	CS_MENU_MBNET_Tx_LockSlaveRx
882
	bnz	CS_MENU_MBNET_Tx_SendEdit_Abort	; abort if slave not available (branch to UnlockRx to give slave a chance to recover)
883
 
884
	;; optimized Edit buffer->CAN->Slave Patch Buffer transfer
885
	lfsr	FSR0, SID_EDIT_BUFFER		; source location
886
	clrf	EEADR				; used as loop counter
887
	clrf	EEADRH
888
CS_MENU_MBNET_Tx_SendEdit_IntLO
889
	movff	EEADR, MBNET_EID_L		; EIDL contains low-byte of address
890
	movff	EEADRH, MBNET_EID_H		; EIDH contains high-byte of address
891
	movlw	MBNET_TOS_RAM_WRITE		; TOS for writing into RAM
892
	call	MBNET_Tx_Prepare		; returns pointer to DLC of Tx buffer in FSR1
893
	bnz	CS_MENU_MBNET_Tx_SendEdit_End	; skip if slave not available
894
 
895
	movlw	8				; DLC always 8
896
	movwf	POSTINC1
897
 
898
	;; read 8 bytes from RAM buffer and write them into data buffer
899
CS_MENU_MBNET_Tx_SendEdit_IntLI
900
	;; read byte and copy into transmit buffer
901
	movff	POSTINC0, POSTINC1
902
	incf	EEADR, F
903
	skpnz
904
	incf	EEADRH, F
905
 
906
	movf	EEADR, W
907
	andlw	0x07
908
	bnz	CS_MENU_MBNET_Tx_SendEdit_IntLI
909
 
910
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
911
	skpz					; re-init display if transfer failed
912
	rgoto	CS_MENU_MBNET_Error
913
 
914
	;; loop until last byte has been sent
915
	movf	EEADR, W
916
	bnz	CS_MENU_MBNET_Tx_SendEdit_IntLO
917
	movf	EEADRH, W
918
	andlw	0xfe
919
	bz	CS_MENU_MBNET_Tx_SendEdit_IntLO
920
 
921
	call	CS_MENU_MBNET_Tx_InitPatch	; init patch
922
 
923
CS_MENU_MBNET_Tx_SendEdit_Abort
924
	call	CS_MENU_MBNET_Tx_UnlockSlaveRx	; unlock receive handler
925
CS_MENU_MBNET_Tx_SendEdit_End
926
	return
927
 
928
 
929
;; --------------------------------------------------------------------------
930
;;  sends the ensemble buffer to a given slave
931
;;  IN: slave target in CS_MENU_SID
932
;; --------------------------------------------------------------------------
933
CS_MENU_MBNET_Tx_SendEns
934
	;; lock receive handler
935
	call	CS_MENU_MBNET_Tx_LockSlaveRx
936
	bnz	CS_MENU_MBNET_Tx_SendEns_Abort	; abort if slave not available (branch to UnlockRx to give slave a chance to recover)
937
 
938
	;; optimized Edit buffer->CAN->Slave Ensemble Buffer transfer
939
	lfsr	FSR0, SID_ENS_BUFFER		; source location
940
	clrf	EEADR				; used as loop counter
941
CS_MENU_MBNET_Tx_SendEns_IntLO
942
	movff	EEADR, MBNET_EID_L		; EIDL contains low-byte of address
943
	movlw	0xff				; EIDH contains high-byte of address, 0xff for ensembles!
944
	movff	WREG, MBNET_EID_H
945
	movlw	MBNET_TOS_RAM_WRITE		; TOS for writing into RAM
946
	call	MBNET_Tx_Prepare		; returns pointer to DLC of Tx buffer in FSR1
947
	bnz	CS_MENU_MBNET_Tx_SendEns_End	; skip if slave not available
948
 
949
	movlw	8				; DLC always 8
950
	movwf	POSTINC1
951
 
952
	;; read 8 bytes from EEPROM and write them into data buffer
953
CS_MENU_MBNET_Tx_SendEns_IntLI
954
	;; read byte and copy into transmit buffer
955
	movff	POSTINC0, POSTINC1
956
	incf	EEADR, F
957
 
958
	movf	EEADR, W
959
	andlw	0x07
960
	bnz	CS_MENU_MBNET_Tx_SendEns_IntLI
961
 
962
	call	MBNET_Tx_Perform		; performs the transfer and waits for an acknowledge from slave
963
	skpz					; re-init display if transfer failed
964
	rgoto	CS_MENU_MBNET_Error
965
 
966
	;; loop until last byte has been sent
967
	movf	EEADR, W
968
	andlw	0x3f
969
 	bnz	CS_MENU_MBNET_Tx_SendEns_IntLO
970
 
971
 	call	CS_MENU_MBNET_Tx_InitEns	; init ensemble
972
 
973
CS_MENU_MBNET_Tx_SendEns_Abort
974
	call	CS_MENU_MBNET_Tx_UnlockSlaveRx	; unlock receive handler
975
CS_MENU_MBNET_Tx_SendEns_End
976
	return
977
 
978
 
979
;; --------------------------------------------------------------------------
980
;;  An error has happened during a CAN transfer: re-init display
981
;;  (MBNET_AVAIL flag will be cleared by driver in mbnet.inc)
982
;; --------------------------------------------------------------------------
983
CS_MENU_MBNET_Error
984
	;; re-init display
985
	bsf	CS_STAT, CS_STAT_DISPLAY_INIT_REQ
986
 
987
	;; *must* return value != 0 (ZERO flag not set!)
988
	iorlw	0xff
989
	return