'sircs.bs2
'TLC 4-Jul-1999
'
'Program to output Sony Control-S (baseband SIRCS) codes for wired control of MD-515
'
'Currently outputs on P8.
'Outputs junk on P7 cos I used PULSOUT for timing pauses.
'  Why not make it useful and wire a visible LED to it. :)
'
'Receives serial data from PC as "S data address repeat"
'and sends in Control-S format.
'Handles 12-bit and 20-bit codes.
'Other lengths left as an exercise for the reader

foo var byte
dat var byte
adr var word
rpt var byte
adr5b var bit
rlp var byte
wait1 con 250

'here we go!

more:

'use serin to get data, address and repeat
'format eg "S 42 1850 3"+chr$(13)
	serin 16,16468,[wait ("S"),dec dat, dec adr, dec rpt]

'set pin to output mode
	output 8
'set pin high
	out8=1

'set address size to small if address given will fit in 5 bits
	adr5b=1
	if adr<32 then smladr
	adr5b=0
smladr:

	dat=255-dat 'invert data so 1 is short pulse, 0 is long
	adr=16383-adr 'ditto address

	for rlp=1 to rpt 'send once
	
	out8=0 'start header

	pulsout 7,950 'delay to get header length

	pulsout 8,300 'end header

	if dat.bit0 then shd0
	pulsout 7,wait1
shd0:	pulsout 8,300

	if dat.bit1 then shd1
	pulsout 7,wait1
shd1:	pulsout 8,300

	if dat.bit2 then shd2
	pulsout 7,wait1
shd2:	pulsout 8,300

	if dat.bit3 then shd3
	pulsout 7,wait1
shd3:	pulsout 8,300

	if dat.bit4 then shd4
	pulsout 7,wait1
shd4:	pulsout 8,300

	if dat.bit5 then shd5
	pulsout 7,wait1
shd5:	pulsout 8,300

	if dat.bit6 then shd6
	pulsout 7,wait1
shd6:	pulsout 8,300

'done data, now address

	if adr.bit0 then sha0
	pulsout 7,wait1
sha0:	pulsout 8,300

	if adr.bit1 then sha1
	pulsout 7,wait1
sha1:	pulsout 8,300

	if adr.bit2 then sha2
	pulsout 7,wait1
sha2:	pulsout 8,300

	if adr.bit3 then sha3
	pulsout 7,wait1
sha3:	pulsout 8,300

	if adr.bit4 then sha4
	pulsout 7,wait1
sha4:	out8=1
	if adr5b then done 'short address done
	out8=0

	if adr.bit5 then sha5
	pulsout 7,wait1
sha5:	pulsout 8,300

	if adr.bit6 then sha6
	pulsout 7,wait1
sha6:	pulsout 8,300

	if adr.bit7 then sha7
	pulsout 7,wait1
sha7:	pulsout 8,300

	if adr.bit8 then sha8
	pulsout 7,wait1
sha8:	pulsout 8,300

	if adr.bit9 then sha9
	pulsout 7,wait1
sha9:	pulsout 8,300

	if adr.bit10 then sha10
	pulsout 7,wait1
sha10:	pulsout 8,300

	if adr.bit11 then sha11
	pulsout 7,wait1
sha11:	pulsout 8,300

	if adr.bit12 then sha12
	pulsout 7,wait1
done:
sha12:	out8=1

'this doesn't exactly give 45ms frame size but it seems to work okay
pause 20

next

'set pin to input so it doesn't override real remote in wired mode
input 8

'go wait for more data
goto more
