Ways to make code more efficient/cut down number of lines.
Kevin Harris
Posts: 15
I have short little application I'm writing for the Commodore 64 SID chip connected to a shift register for the Basic Stamp. I'm a C/Perl guy and I like to minimize the number of lines I have in my code. In researching the PBasic language it appears there isn't really a direct way to pass arguments to functions etc, so I do it the following way. Is there a way to minimize the repetition in this code and/or a way to make it more efficient. Any help is appreciated, thanks!
'This file assumes the following shift register/SID connection
'===================================================================
'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
'0 0 RES A4 A3 A2 A1 A0 D7 D6 D5 D4 D3 D2 D1 D0
'Pin assignments and other constants
CS PIN 13 'SID chip select
Shift_Clock PIN 12 'shift clock
Storage_Clock PIN 14 'latch pin of shift register
Serial_Data PIN 15 'shift data input
LSB_First CON 0
'Modifiable variables
Volume VAR Byte
SID_Cmd VAR Word
SID_Reg VAR SID_Cmd.HIGHBYTE
SID_Data VAR SID_Cmd.LOWBYTE
'Voice 1 constants
V1_Freq_Lo CON $20
V1_Freq_Hi CON $21
V1_PW_Lo CON $22
V1_PW_Hi CON $23
V1_Ctrl CON $24
V1_Atk_Dcy CON $25
V1_Sust_Rel CON $26
'Voice 2 constants
V2_Freq_Lo CON $27
V2_Freq_Hi CON $28
V2_PW_Lo CON $29
V2_PW_Hi CON $2A
V2_Ctrl CON $2B
V2_Atk_Dcy CON $2C
V2_Sust_Rel CON $2D
'Voice 3 constants
V3_Freq_Lo CON $2E
V3_Freq_Hi CON $2F
V3_PW_Lo CON $30
V3_PW_Hi CON $31
V3_Ctrl CON $32
V3_Atk_Dcy CON $33
V3_Sust_Rel CON $34
'Filter Constants
FC_Lo CON $35
FC_Hi CON $36
RES_Filt CON $37
Mode_Vol CON $38
'Misc register constants
OSC3_Rand CON $3B
ENV3 CON $3C
LOW Storage_Clock 'Initialize latch output
LOW CS 'Initialize the chip select pin
DO
GOSUB Init_SID
PAUSE 100
'Write a tone to the SID chip
SID_Reg = V1_Freq_Lo
SID_Data = 12
GOSUB Write_Cmd
Volume = 15
GOSUB Adjust_Volume
SLEEP 20000
LOOP
'Modifies the SID's volume register to be the value in the Volume variable (must be between 0 and 15)
Adjust_Volume:
IF (Volume >=0 AND Volume <= 15) THEN
SID_Reg = Mode_Vol
SID_Data = Volume
GOSUB Write_Cmd
ENDIF
RETURN
'Initialize Voice 1 and volume register values
Init_SID:
SID_Reg = V1_PW_Lo
SID_Data = 0
GOSUB Write_Cmd
SID_Reg = V1_PW_Hi
SID_Data = 8
GOSUB Write_Cmd
SID_Reg = V1_Ctrl
SID_Data = 65
GOSUB Write_Cmd
SID_Reg = V1_Atk_Dcy
SID_Data = 0
GOSUB Write_Cmd
SID_Reg = V1_Sust_Rel
SID_Data = 240
GOSUB Write_Cmd
Volume = 0
GOSUB Adjust_Volume
RETURN
' Shifts the 16 bit value of SID_Cmd out to the shift register LSB first
Write_Cmd:
SHIFTOUT Serial_Data, Shift_Clock, LSB_First, [noparse][[/noparse]SID_Cmd\16]
GOSUB Latch_Value
RETURN
'Pulses the Storage_Clock pin to latch the current shift register value to the output lines
Latch_Value:
PULSOUT Storage_Clock, 1
PAUSE 100
RETURN
'This file assumes the following shift register/SID connection
'===================================================================
'0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
'0 0 RES A4 A3 A2 A1 A0 D7 D6 D5 D4 D3 D2 D1 D0
'Pin assignments and other constants
CS PIN 13 'SID chip select
Shift_Clock PIN 12 'shift clock
Storage_Clock PIN 14 'latch pin of shift register
Serial_Data PIN 15 'shift data input
LSB_First CON 0
'Modifiable variables
Volume VAR Byte
SID_Cmd VAR Word
SID_Reg VAR SID_Cmd.HIGHBYTE
SID_Data VAR SID_Cmd.LOWBYTE
'Voice 1 constants
V1_Freq_Lo CON $20
V1_Freq_Hi CON $21
V1_PW_Lo CON $22
V1_PW_Hi CON $23
V1_Ctrl CON $24
V1_Atk_Dcy CON $25
V1_Sust_Rel CON $26
'Voice 2 constants
V2_Freq_Lo CON $27
V2_Freq_Hi CON $28
V2_PW_Lo CON $29
V2_PW_Hi CON $2A
V2_Ctrl CON $2B
V2_Atk_Dcy CON $2C
V2_Sust_Rel CON $2D
'Voice 3 constants
V3_Freq_Lo CON $2E
V3_Freq_Hi CON $2F
V3_PW_Lo CON $30
V3_PW_Hi CON $31
V3_Ctrl CON $32
V3_Atk_Dcy CON $33
V3_Sust_Rel CON $34
'Filter Constants
FC_Lo CON $35
FC_Hi CON $36
RES_Filt CON $37
Mode_Vol CON $38
'Misc register constants
OSC3_Rand CON $3B
ENV3 CON $3C
LOW Storage_Clock 'Initialize latch output
LOW CS 'Initialize the chip select pin
DO
GOSUB Init_SID
PAUSE 100
'Write a tone to the SID chip
SID_Reg = V1_Freq_Lo
SID_Data = 12
GOSUB Write_Cmd
Volume = 15
GOSUB Adjust_Volume
SLEEP 20000
LOOP
'Modifies the SID's volume register to be the value in the Volume variable (must be between 0 and 15)
Adjust_Volume:
IF (Volume >=0 AND Volume <= 15) THEN
SID_Reg = Mode_Vol
SID_Data = Volume
GOSUB Write_Cmd
ENDIF
RETURN
'Initialize Voice 1 and volume register values
Init_SID:
SID_Reg = V1_PW_Lo
SID_Data = 0
GOSUB Write_Cmd
SID_Reg = V1_PW_Hi
SID_Data = 8
GOSUB Write_Cmd
SID_Reg = V1_Ctrl
SID_Data = 65
GOSUB Write_Cmd
SID_Reg = V1_Atk_Dcy
SID_Data = 0
GOSUB Write_Cmd
SID_Reg = V1_Sust_Rel
SID_Data = 240
GOSUB Write_Cmd
Volume = 0
GOSUB Adjust_Volume
RETURN
' Shifts the 16 bit value of SID_Cmd out to the shift register LSB first
Write_Cmd:
SHIFTOUT Serial_Data, Shift_Clock, LSB_First, [noparse][[/noparse]SID_Cmd\16]
GOSUB Latch_Value
RETURN
'Pulses the Storage_Clock pin to latch the current shift register value to the output lines
Latch_Value:
PULSOUT Storage_Clock, 1
PAUSE 100
RETURN
Comments