各種言語によるプログラミング例

1995 長嶋洋一


BASIC その1 フラクタルの実験

10 'save "fract",a
20 DEFINT A-N:DEFDBL P-Z:KEY OFF:SCREEN 1
30 INPUT "Input Center (X) = ",P1:INPUT "Input Center (Y) = ",P2:INPUT "Expand Scale = ",P3:INPUT "Plot Step = ",JX
40 CLS:LINE (0,29)-(1023,767),1,B
50 LOCATE 1,8,0:PRINT "Center = (";P1;",";P2;")    Scale =";P3;"   Step =";JX;
60 X0=P1-P3:Y0=P2+P3:X1=P3/512#
70 FOR IX=1 TO 1023 STEP JX:X2=X0+CDBL(IX)*X1
80 FOR IY=143 TO 881 STEP JX:Y2=Y0-CDBL(IY)*X1
90 GOSUB 120:IF F1=1 THEN PSET (IX,IY-114)
100 NEXT IY:LOCATE 1,73,0:PRINT IX;:NEXT IX:LOCATE 1,73,0:PRINT "     ";
110 A$=INKEY$:IF A$<>" " THEN 110 ELSE SCREEN 0:KEY ON:LOCATE 1,1,1:END
120 F1=0:Z1=0#:Z2=0#:FOR K1=1 TO 100:ZZ=Z1*Z1-Z2*Z2+X2:Z2=2#*Z1*Z2+Y2:Z1=ZZ
130 IF Z1*Z1+Z2*Z2>4# THEN RETURN
140 NEXT K1:F1=1:RETURN

BASIC その2 キー入力の練習

10 'SAVE "lesson.bas",A
20  MAXX=9:DIM BUFF$(100)
30  KEY OFF:CLS
40  LOCATE 1,5:COLOR 0:INPUT "SELECT LESSON NO.(1-9) = ",C$
50  LEVEL=VAL(C$):IF LEVEL>MAXX OR LEVEL<1 OR LEVEL<>INT(LEVEL) THEN 30
60  CLS:RESTORE 270:CTR=1
70  READ DUM$:IF LEFT$(DUM$,2)<>"**" THEN 70
80  IF VAL(RIGHT$(DUM$,LEN(DUM$)-2))<>LEVEL THEN 70
90  READ BUFF$(CTR):IF LEFT$(BUFF$(CTR),2)<>"**" THEN CTR=CTR+1:GOTO 90
100  FOR LOOP=1 TO CTR-1
110  A$=BUFF$(LOOP):COUNT=LEN(A$)
120  IF MAXX>6 THEN A$=A$+STRING$(70-LEN(A$)," "):GOTO 150
130  A$=A$+" "+A$:IF LEN(A$)<70 THEN 130 ELSE A$=LEFT$(A$,70)
140  IF RIGHT$(A$,1)=" " THEN COUNT=69 ELSE COUNT=70
150  LOCATE 17,5:COLOR 0:PRINT A$:LOCATE 19,5:COLOR 0:PRINT STRING$(60," ")
160  FOR I=1 TO COUNT:'### MAIN LOOP ###
170  LOCATE 17,4+I:COLOR 1:PRINT MID$(A$,I,1)
180  LOCATE 19,4+I:COLOR 0
190  C$=INKEY$:IF C$="" THEN 190 ELSE PRINT C$
200  IF C$=CHR$(7) THEN 30
210  IF C$=CHR$(14) THEN 260
220  IF C$=CHR$(2) AND LOOP>1 THEN LOOP=LOOP-1:GOTO 110
230  IF C$=MID$(A$,I,1) THEN 250
240  IF INKEY$=CHR$(8) THEN 180 ELSE 240
250  LOCATE 17,4+I:COLOR 0:PRINT MID$(A$,I,1):NEXT I
260  NEXT LOOP:BEEP:GOTO 30
270  DATA "**1"
280  DATA "asdf;lkj"
290  DATA "ask","lad","all","fall","lass","asks","lads","falls"
300  DATA "alas","add","flask","salad"
310  DATA "**2"
320  DATA "asdfgf ;lkjhj"
330  DATA "had","ash","has","gall","hash","jags","hags"
340  DATA "glass","gala","lag","jag","gas","lash","flag","sash","half"
350  DATA "slash","flags","gash","hall","lags","glad","slag","dash"
360  DATA "shall","flash","halls"
370  DATA "**3"
380  DATA "aqaswsdedfrf ;p;lolkikjuj"
390  DATA "sails","liked","jokes","forks","lakes","parks","larks","fills"
400  DATA "sales","dirks","fails","skill","apples","fields","squads"
410  DATA "floors","killed","raised","depress","workers","refusal"
420  DATA "dollars","awkward","defiles"
430  DATA "**4"
440  DATA "tfrf yjuj"
450  DATA "tall","sorry","yours","trial","shout","yield","others"
460  DATA "gladly","though","takes","yeast","trout","forest","joyful"
470  DATA "faults","artful","styles","lastly","truly","shifts","spirit"
480  DATA "eighty","firstly","laughed","faithful","yourself","spiteful"
490  DATA "hit the key","type this well","read this paper"
500  DATA "he will tell you","write with this","there is a pupil"
510  DATA "type these words","there are houses","he wrote a letter"
520  DATA "she is a good typist","here is a typewriter"
530  DATA "there are pretty dolls"
540  DATA "**5"
550  DATA "aszsdxdfcfvf ;l,lkmkjnjbj"
560  DATA "lacks","calls","formal","zeal","next","black","summer","cabled"
570  DATA "arrival","local","native","cancel","visits","manual","several"
580  DATA "object","drizzle","examined","nations","meaning","anxious"
590  DATA "because","numbers","activity","subject","examples","umbrella"
600  DATA "**6"
610  DATA "James","Frank","India","England","London","Spain","Paris"
620  DATA "America","Tokyo"
630  DATA "I am now ready to begin typing."
640  DATA "We are happy to have you in the typing class."
650  DATA "It is good for all of us to know how to type."
660  DATA "He likes to sing all the songs that he hears."
670  DATA "Keep your arms, elbows, and wrists still while typing."
680  DATA "It is very easy to type these short words."
690  DATA "The first thing for me to do is to type with ease."
700  DATA "I can do this work well if I just think that I can."
710  DATA "Curve your fingers; hold them lightly on the guide keys."
720  DATA "You do not get much done by starting tomorrow."
730  DATA "If you do your best, you will develop your typing skill."
740  DATA "Speed will look after itself if you play your part well."
750  DATA "Do not be afraid to make errors while you are practising."
760  DATA "Be alert while typing and hit the keys firmly and evenly."
770  DATA "**7"
780  DATA "If he is in, tell him that I want to see him in an hour"
790  DATA " or so."
800  DATA "If you do your best work each day, you will soon be a"
810  DATA " good typist."
820  DATA "You must know what you are to do; then do it as nicely"
830  DATA " as you can."
840  DATA "I can read very fast. He can write very well. She can"
850  DATA " type quickly."
860  DATA "If it is fine, I shall go with you. I hope we can enjoy"
870  DATA " it very much."
880  DATA "Hit one key at a time. Do not pause after you hit the"
890  DATA " key."
900  DATA "If I do my lesson well, my typing skill will grow in due"
910  DATA " course."
920  DATA "We must have faith that we can do well anything we want"
930  DATA " to learn."
940  DATA "Typing is a new kind of work for me and a new way for me"
950  DATA " to learn it."
960  DATA "Do more than you say you will do. This is a good plan to"
970  DATA " follow all the time."
980  DATA "It is not easy at first to think the word and type the"
990  DATA " word."
1000  DATA "Faith that we can do well is needed by all of us to do"
1010  DATA " things well."
1020  DATA "Try to hit the key with a sharp blow; then lift the"
1030  DATA " finger at once."
1040  DATA "You will find that there are a good many fields in which"
1050  DATA " you can use your typing skill."
1060  DATA "Hold the hands and arms quiet; hold them low so that your"
1070  DATA " fingers can flick the keys and get away quickly."
1080  DATA "**8"
1090  DATA "abcde fghij klmno pqrst uvwxyz abcdefghijklmnopqrstuvwxyz"
1100  DATA "Dick asserted that the quick brown fox jumped over the"
1110  DATA " lazy dog."
1120  DATA "A very hot, quick blaze can be made from just pine twigs"
1130  DATA " and boxes."
1140  DATA "Fred Carver requests we keep the dozen large boxes of"
1150  DATA " juicy lemons."
1160  DATA "A zinc plate, six inches square, would make a very good"
1170  DATA " job for you."
1180  DATA "The public was amazed to view the quick and dexterous"
1190  DATA " work of James on the foggy day."
1200  DATA "The bright cat quickly jumped off a box and seized seven"
1210  DATA " wee mice."
1220  DATA "Jim goes forward expectantly but very quickly realizes"
1230  DATA " his mistake."
1240  DATA "A fat lazy boy, named Jack Davis, slept quietly on a big"
1250  DATA " haedware box."
1260  DATA "Six dozen of heavy blankets were added to the equipment"
1270  DATA " for George and Jack."
1280  DATA "A large number of our citizens have signed a petition"
1290  DATA " which makes request for a more just system of taxation."
1300  DATA "Both of the young girls from Japan were vexed at his"
1310  DATA " quizzical look."
1320  DATA "Jim Faraday may achieve success by exercise of superior"
1330  DATA " judgment, zeal, and quick work."
1340  DATA "When the big black fox jumped over the fence, the little"
1350  DATA " squirrel gazed very curiously."
1360  DATA "You will probably have to explain if you did not use zeal"
1370  DATA " and judgment and work as quietly as you could."
1380  DATA "Just one hour of quiet, steady, exact work, is of far"
1390  DATA " greater value than a dozen hours of spasmodic bustling."
1400  DATA "**9"
1410  DATA "It cannot be too often repeated that it is not helps but"
1420  DATA " obstacles, not facilities but difficulties, that make men."
1430  DATA "The main purpose of the study of chemistry in school is to"
1440  DATA " teach the scientific method; that is, the appeal to experiment."
1450  DATA "Re Paper Release Lever: By means of the paper release lever"
1460  DATA " the paper in the machine is adjusted when it is fed twisted."
1470  DATA "Opportunity is coy. The careless, the slow, the lazy, the"
1480  DATA " unobservant, fail to see it, or clutch at it when it has gone."
1490  DATA "Poverty is a great evil in any station of life; but poverty"
1500  DATA " is never felt so severely as by those who have seen better days."
1510  DATA "Enclosed is a statement for your September account. This"
1520  DATA " is due on the 11th October, but has evidently escaped your notice."
1530  DATA "**END"
1540  IF FLG<>0 THEN FLG=0:GOTO 1600 ELSE FLG=1
1550   LOCATE 5,4:COLOR 7:PRINT "1   2   3   4   5   6   7   8   9   0   -"
1560   LOCATE 5,6:COLOR 7:PRINT "  Q   W   E   R   T   Y   U   I   O   P  "
1570   LOCATE 5,8:COLOR 7:PRINT "    A   S   D   F   G   H   J   K   L   ;   :"
1580   LOCATE 5,10:COLOR 7:PRINT "      Z   X   C   V   B   N   M   ,   .   /  "
1590   RETURN
1600   LOCATE 5,4:COLOR 7:PRINT "!"
1610   LOCATE 9,4:COLOR 7:PRINT CHR$(34)
1620   LOCATE 13,4:COLOR 7:PRINT "#   $   %   &   '   (   )       ="
1630   LOCATE 5,6:COLOR 7:PRINT "  Q   W   E   R   T   Y   U   I   O   P  "
1640   LOCATE 5,8:COLOR 7:PRINT "    A   S   D   F   G   H   J   K   L   +   *"
1650   LOCATE 5,10:COLOR 7:PRINT "      Z   X   C   V   B   N   M   <   >   ?  "
1660   RETURN

BASIC その3 カードゲーム

1000 'save "e:card1.bas",a
1010   DEFINT A-N:A=&H40:DIM B(2048):CLS
1020    OUT (&H37),&HD:OUT (&H46),&H82:OUT (&H46),&HF:OUT (&H37),&HC
1030    OUT (A),&H80:GOTO 1170 ' << Initialize >>
1040 '### Data Write : Data=[DT],Add(L)=[AL],Add(H)=[AH] ###
1050    DD=128+(DT MOD 16):OUT (A),DD:OUT (A),DD-128:OUT (A),DD ' D0-D3
1060    DD=144+INT(DT/16): OUT (A),DD:OUT (A),DD-128:OUT (A),DD ' D4-D7
1070    DD=160+(AL MOD 16):OUT (A),DD:OUT (A),DD-128:OUT (A),DD ' A0-A3
1080    DD=176+INT(AL/16): OUT (A),DD:OUT (A),DD-128:OUT (A),DD ' A4-A7
1090    DD=192+(AH MOD 16):OUT (A),DD:OUT (A),DD-128:OUT (A),DD ' A8-A11
1100    OUT (&H46),&HE:OUT (&H46),&HF:RETURN ' Strobe
1110 '### Data Read : Add(L)=[AL],Add(H)=[AH] -> Data=[DX] ###
1120    DD=160+(AL MOD 16):OUT (A),DD:OUT (A),DD-128:OUT (A),DD ' A0-A3
1130    DD=176+INT(AL/16): OUT (A),DD:OUT (A),DD-128:OUT (A),DD ' A4-A7
1140    DD=192+(AH MOD 16):OUT (A),DD:OUT (A),DD-128:OUT (A),DD ' A8-A11
1150   DX=0:FOR I=0 TO 7:OUT (A),128+16*I:J=(INP(&H42) AND &H4)/4
1160    DX=DX+J*2^I:NEXT I:RETURN
1170 '### Main Routine ###
1180  INPUT "Data Display : Input High Address ( 0 - 7 )";AH
1190   FOR AL=0 TO 255
1200    GOSUB 1110:PRINT HEX$(DX)+" ";
1210   NEXT AL

BASIC その4 音源のシミュレーション

10 'save "coresim",a
20  KEY OFF:SCREEN 2:LOCATE ,,0:DIM MT(256)
30   ISX=3:ISY=2:IXMIN=150:IXMAX=IXMIN+255*ISX:IYMIN=170:IYMAX=IYMIN+255*ISY
40   CT=1:GOSUB 90
50  Z$=INKEY$:IF Z$="" THEN 50
60    IF Z$=" " THEN CT=CT+1:GOSUB 90
70    IF Z$<>CHR$(&H1B) THEN GOTO 50
80       SCREEN 0,0,0:LOCATE 1,1,1:KEY ON:END
90   ON CT GOSUB 100,200,300,400,500,600,700,800,80:RETURN
100 '***** Normal Power-Mantissa *****
110   CLS:LINE (IXMIN-ISX,IYMIN-ISY)-(IXMAX+ISX,IYMAX+ISY),1,B
120    LOCATE 3,25:PRINT "<< 1:Normal Power-Mantissa >>"
130   FOR I=0 TO 255
140    MANTISSA=I MOD 16
150    POWER=INT(I/16)
160    MT(I)=INT(256*(2^(-POWER))*(MANTISSA/16))
170    CIRCLE (IXMIN+I*ISX,IYMAX-MT(I)*ISY),2
180    PAINT (IXMIN+I*ISX,IYMAX-MT(I)*ISY)
190   NEXT I:RETURN
200 '***** Mantissa:Bit Swap *****
210   CLS:LINE (IXMIN-ISX,IYMIN-ISY)-(IXMAX+ISX,IYMAX+ISY),1,B
220    LOCATE 3,25:PRINT "<< 2:Mantissa-Bit Swap >>"
230   FOR I=0 TO 255
240    MANTISSA=I MOD 16
250    POWER=INT(I/16)
260    MT(I)=INT(256*(2^(-POWER))*((15-MANTISSA)/16))
270    CIRCLE (IXMIN+I*ISX,IYMAX-MT(I)*ISY),2
280    PAINT (IXMIN+I*ISX,IYMAX-MT(I)*ISY)
290   NEXT I:RETURN
300 '*****  *****
310   CLS:LINE (IXMIN-ISX,IYMIN-ISY)-(IXMAX+ISX,IYMAX+ISY),1,B
320    LOCATE 3,25:PRINT "<< 3:Power-Swap >>"
330   FOR I=0 TO 255
340    MANTISSA=I MOD 16
350    POWER=INT(I/16)
360    MT(I)=INT(256*(2^(-(15-POWER)))*(MANTISSA/16))
370    CIRCLE (IXMIN+I*ISX,IYMAX-MT(I)*ISY),2
380    PAINT (IXMIN+I*ISX,IYMAX-MT(I)*ISY)
390   NEXT I:RETURN
400 '*****  *****
410   CLS:LINE (IXMIN-ISX,IYMIN-ISY)-(IXMAX+ISX,IYMAX+ISY),1,B
420    LOCATE 3,25:PRINT "<< 4: >>"
430   FOR I=0 TO 255
440    MANTISSA=I MOD 16
450    POWER=INT(I/16)
460    MT(I)=INT(256*(2^(-POWER))*(MANTISSA/16))
470    CIRCLE (IXMIN+I*ISX,IYMAX-MT(I)*ISY),2
480    PAINT (IXMIN+I*ISX,IYMAX-MT(I)*ISY)
490   NEXT I:RETURN
500 '*****  *****
510   CLS:LINE (IXMIN-ISX,IYMIN-ISY)-(IXMAX+ISX,IYMAX+ISY),1,B
520    LOCATE 3,25:PRINT "<< 5: >>"
530   FOR I=0 TO 255
540    MANTISSA=I MOD 16
550    POWER=INT(I/16)
560    MT(I)=INT(256*(2^(-POWER))*(MANTISSA/16))
570    CIRCLE (IXMIN+I*ISX,IYMAX-MT(I)*ISY),2
580    PAINT (IXMIN+I*ISX,IYMAX-MT(I)*ISY)
590   NEXT I:RETURN
600 '*****  *****
610   CLS:LINE (IXMIN-ISX,IYMIN-ISY)-(IXMAX+ISX,IYMAX+ISY),1,B
620    LOCATE 3,25:PRINT "<< 6: >>"
630   FOR I=0 TO 255
640    MANTISSA=I MOD 16
650    POWER=INT(I/16)
660    MT(I)=INT(256*(2^(-POWER))*(MANTISSA/16))
670    CIRCLE (IXMIN+I*ISX,IYMAX-MT(I)*ISY),2
680    PAINT (IXMIN+I*ISX,IYMAX-MT(I)*ISY)
690   NEXT I:RETURN
700 '*****  *****
710   CLS:LINE (IXMIN-ISX,IYMIN-ISY)-(IXMAX+ISX,IYMAX+ISY),1,B
720    LOCATE 3,25:PRINT "<< 7: >>"
730   FOR I=0 TO 255
740    MANTISSA=I MOD 16
750    POWER=INT(I/16)
760    MT(I)=INT(256*(2^(-POWER))*(MANTISSA/16))
770    CIRCLE (IXMIN+I*ISX,IYMAX-MT(I)*ISY),2
780    PAINT (IXMIN+I*ISX,IYMAX-MT(I)*ISY)
790   NEXT I:RETURN
800 '*****  *****
810   CLS:LINE (IXMIN-ISX,IYMIN-ISY)-(IXMAX+ISX,IYMAX+ISY),1,B
820    LOCATE 3,25:PRINT "<< 8: >>"
830   FOR I=0 TO 255
840    MANTISSA=I MOD 16
850    POWER=INT(I/16)
860    MT(I)=INT(256*(2^(-POWER))*(MANTISSA/16))
870    CIRCLE (IXMIN+I*ISX,IYMAX-MT(I)*ISY),2
880    PAINT (IXMIN+I*ISX,IYMAX-MT(I)*ISY)
890   NEXT I:RETURN

BASIC その5 ディレクトリリスト作成

10 'save "dirmap.bas",a
20   DIM X$(200),Y(200):CT=1
30   OPEN "dirlist.mes" FOR INPUT AS #1
40  INPUT #1,A$:IF LEN(A$)=0 THEN 170
50   J=1:K=LEN(A$)
60  IF J>=K THEN 170 ELSE B=ASC(MID$(A$,J,1))
70   IF (128

BASIC その6 ニュートン近似

1000 'save "divide.bas",a
1010    S#=(15#+30#)*100#/2#
1020    PRINT"    Total =";S#
1030 PRINT:PRINT"##### 2-Divide #####"
1040  FOR Q=58.11385 TO 58.11392 STEP .00001
1050    P#=CDBL(Q)
1060    Y#=15#*P#/100#
1070    T#=15#*P#+Y#*P#/2#
1080    PRINT"    Point =";Q;" , S =";T#;" , Diff.=";S#/2#-T#
1090  NEXT Q
1100 PRINT:PRINT"##### 3-Divide (1) #####"
1110  FOR Q=41.42132 TO 41.42139 STEP .00001
1120    P#=CDBL(Q)
1130    Y#=15#*P#/100#
1140    T#=15#*P#+Y#*P#/2#
1150    PRINT"    Point =";Q;" , S =";T#;" , Diff.=";S#/3#-T#
1160  NEXT Q
1170 PRINT:PRINT"##### 3-Divide (2) #####"
1180  FOR Q=73.20506 TO 73.2051 STEP .00001
1190    P#=CDBL(Q)
1200    Y#=15#*P#/100#
1210    T#=15#*P#+Y#*P#/2#
1220    PRINT"    Point =";Q;" , S =";T#;" , Diff.=";2#*S#/3#-T#
1230  NEXT Q
1240 PRINT

BASIC その7 FFTのテスト

10 'save "ffttest.bas",a
20 CLS:GOSUB 100
25 FOR I=0 TO 255
30 XR(I)=INT(128*SIN(I*3.1415926535#/128) )
35 XI(I)=0
40 NEXT I
50 GOSUB 200
60 'FOR I=0 TO 255:PRINT (XR(I)^2+XI(I)^2)^(1/2);:NEXT I
65 FOR I=0 TO 255:PRINT XR(I);:NEXT I
95 END
100 DEFINT G-Q:I=0:J=0:K=0:P=0:L=0:H=0:G=0:Q=0:A=0
110 N=256:M=LOG(N)/LOG(2):DIM XR(N),XI(N),S(N/2),C(N/2):B=3.1415956535#/N
130 FOR I=0 TO N/2:S(I)=SIN(A):C(I)=COS(I):A=A+B:NEXT I:RETURN
200 L=N:H=1
210 FOR G=1 TO M:L=L/2:K=0
220  FOR Q=1 TO H:P=0
230   FOR I=K TO L+K-1:J=I+L
240    A=XR(I)-XR(J):B=XI(I)-XI(J)
250    XR(I)=XR(I)+XR(J):XI(I)=XI(I)+XI(J)
260    IF P=0 THEN XR(J)=A:XI(J)=B:GOTO 280
270    XR(J)=A*C(P)+B*S(P):XI(J)=B*C(P)-A*S(P)
280   P=P+H:NEXT I
290  K=K+L+L:NEXT Q
300 H=H+H:NEXT G
320 J=N/2:FOR I=1 TO N-1:K=N
340 IF J=K THEN J=J-K:GOTO 350
360 J=J+K:NEXT I:RETURN

BASIC その8 ファイルの分割

1000 'save "file-cut2",a
1010  OPEN "f:z.z" FOR INPUT AS #1
1020  CLS:PRINT "<";TIME$;"> Start !"
1040  NO=0
1050  '### Loop ###
1060  NO=NO+1
1070  A$=HEX$(NO):PRINT "<";TIME$;"> FILE NAME = [ zz-";A$;".z ]"
1080  OPEN "c:zz-"+A$+".z" FOR OUTPUT AS #2
1090  I=0
1100  IF EOF(1) THEN CLOSE #1:CLOSE #2:END
1101  INPUT #1,E$
1102  PRINT #2,E$
1110  I=I+1:IF I<500 THEN 1100
1150  CLOSE #2
1200  GOTO 1050

BASIC その9 位相累算周波数データの計算

10 'save "fno.bas",a
20 CLS:DIM X$(12):FOR I=0 TO 11:X$(I)="        db      ":NEXT I
30 DIM Y$(12):FOR I=0 TO 11:Y$(I)="        db      ":NEXT I
40 DIM Z$(12):FOR I=0 TO 11:Z$(I)="        db      ":NEXT I
50 G#=3520#*(2#^(6.5#/12#)):H#=(2#^(1#/12#/32#)):A#=(2#^(1#/12#)):FOR I=0 TO 11
60 RESTORE 160:FOR K=1 TO (12-I):READ A$:NEXT K:PRINT "===== [ ";A$;" ] ====="
70 B#=1#/A#:FOR N=0 TO I:B#=B#*A#:NEXT N
80 FOR L=0 TO 31:PRINT "    Offset (";:E$=HEX$(L):IF LEN(E$)=1 THEN E$="0"+E$
90 PRINT E$;") :";:P#=1#/H#:FOR N=0 TO L:P#=P#*H#:NEXT N:U$=HEX$(L):IF LEN(U$)=1 THEN U$="0"+U$
100 O#=G#*P#*B#:PRINT "  f =";:PRINT USING "#####.######";O#;:PRINT "Hz";
110 Q#=2#^24#*O#/62500#:C$=HEX$(INT(Q#)/256):PRINT "  Data = ";C$;
120 D$=HEX$(INT(Q#)-256*INT(Q#/256)):IF LEN(D$)=1 THEN D$="0"+D$
130 PRINT D$;:PRINT:X$(I)=X$(I)+"0"+LEFT$(C$,2)+"h,"
140 Y$(I)=Y$(I)+"0"+RIGHT$(C$,2)+"h,":Z$(I)=Z$(I)+"0"+D$+"h,"
150 NEXT L:PRINT :NEXT I
160 DATA "Eb-9","D -9","C#-9","C -9","B -8","Bb-8","A -8","Ab-8","G -8","F#-8","F -8","E -8"
170 OPEN "fnodata.s14" FOR OUTPUT AS #1
180 FOR I=0 TO 11:PRINT #1,X$(I):NEXT I:PRINT #1," "
190 FOR I=0 TO 11:PRINT #1,Y$(I):NEXT I:PRINT #1," "
200 FOR I=0 TO 11:PRINT #1,Z$(I):NEXT I:PRINT #1," "
210 CLOSE #1:END

BASIC その10 SMFファイルコンバータ

10 'save "fileconv.bas",a
20    DEFINT A-Z
30    DATA 1,2,3,4,99,99,6,99,99,99,99,99,99,99,7,8
40    G$="demo":QT=4
50   PHASE=0:'First=0 !!!
60      IF PHASE=9 THEN 1520:'!!!!! Debug Only !!!!!
70    GOSUB 960:GOSUB 440:GOSUB 100
80    SHELL "copy f:dummy.dum a:"+G$+".kmy"
90  END
100  '*****  TESTER File  *****************************************
110    OPEN "f:dummy.dum" FOR OUTPUT AS #2
120     FOR I=1 TO 32:PRINT #2,CHR$(0);:NEXT I
130      FF=0:F1=0:F2=0:GOSUB 420
140      OPEN "f:tr1.buf" FOR INPUT AS #1
150       FD=LOF(1)-1:GOSUB 420:CLOSE #1
160      OPEN "f:tr2.buf" FOR INPUT AS #1
170       FD=LOF(1)-1:GOSUB 420:CLOSE #1
180      OPEN "f:tr3.buf" FOR INPUT AS #1
190       FD=LOF(1)-1:GOSUB 420:CLOSE #1
200      OPEN "f:tr4.buf" FOR INPUT AS #1
210       FD=LOF(1)-1:GOSUB 420:CLOSE #1
220      OPEN "f:tr5.buf" FOR INPUT AS #1
230       FD=LOF(1)-1:GOSUB 420:CLOSE #1
240      OPEN "f:tr6.buf" FOR INPUT AS #1
250       FD=LOF(1)-1:GOSUB 420:CLOSE #1
260      OPEN "f:tr7.buf" FOR INPUT AS #1
270       FD=LOF(1)-1:GOSUB 420:CLOSE #1
280     FOR I=1 TO 16:PRINT #2,CHR$(0);:NEXT I
290      OPEN "f:tr1.buf" FOR INPUT AS #1:GOSUB 380
300      OPEN "f:tr2.buf" FOR INPUT AS #1:GOSUB 380
310      OPEN "f:tr3.buf" FOR INPUT AS #1:GOSUB 380
320      OPEN "f:tr4.buf" FOR INPUT AS #1:GOSUB 380
330      OPEN "f:tr5.buf" FOR INPUT AS #1:GOSUB 380
340      OPEN "f:tr6.buf" FOR INPUT AS #1:GOSUB 380
350      OPEN "f:tr7.buf" FOR INPUT AS #1:GOSUB 380
360      OPEN "f:tr8.buf" FOR INPUT AS #1:GOSUB 380
370   CLOSE #2:RETURN
380    FOR I=1 TO ((LOF(1)-1)/2)
390     E1$=INPUT$(1,#1):E2$=INPUT$(1,#1)
400     PRINT #2,CHR$(VAL("&H"+E1$+E2$));:PRINT E1$+E2$+" ";:NEXT I
410     CLOSE #1:RETURN
420    FF=FF+FD/2:F2=FF MOD 256:F1=INT(FF/256)
430     PRINT #2,CHR$(F2);:PRINT #2,CHR$(F1);:PRINT "#";:RETURN
440  '*****  TESTER Format  *****************************************
450     DIM D(5000,4)
460    IF PHASE>2 THEN RETURN
470     OPEN "f:tr1.trk" FOR INPUT AS #1
480     OPEN "f:tr1.buf" FOR OUTPUT AS #2
490      GOSUB 720
500     OPEN "f:tr2.trk" FOR INPUT AS #1
510     OPEN "f:tr2.buf" FOR OUTPUT AS #2
520      GOSUB 720
530     OPEN "f:tr3.trk" FOR INPUT AS #1
540     OPEN "f:tr3.buf" FOR OUTPUT AS #2
550      GOSUB 720
560     OPEN "f:tr4.trk" FOR INPUT AS #1
570     OPEN "f:tr4.buf" FOR OUTPUT AS #2
580      GOSUB 720
590     OPEN "f:tr5.trk" FOR INPUT AS #1
600     OPEN "f:tr5.buf" FOR OUTPUT AS #2
610      GOSUB 720
620     OPEN "f:tr6.trk" FOR INPUT AS #1
630     OPEN "f:tr6.buf" FOR OUTPUT AS #2
640      GOSUB 720
650     OPEN "f:tr7.trk" FOR INPUT AS #1
660     OPEN "f:tr7.buf" FOR OUTPUT AS #2
670      GOSUB 720
680     OPEN "f:tr8.trk" FOR INPUT AS #1
690     OPEN "f:tr8.buf" FOR OUTPUT AS #2
700      GOSUB 720
710    RETURN
720    I=1:MK=0
730     IF EOF(1) THEN 800 ELSE INPUT #1,TM,ST,KN,VL
740       PRINT HEX$(TM),HEX$(ST),HEX$(KN),HEX$(VL):MK=1:TB=0
750      IF ST>&HBF THEN TB=TM:D(I,2)=&HC0:D(I,3)=KN:I=I+1:GOTO 730
760      IF VL>0 THEN D(I,1)=TM+TB:D(I,2)=KN:D(I,4)=VL:I=I+1:GOTO 730
770      FOR J=I-1 TO 1 STEP -1
780        IF D(J,2)=KN THEN D(J,3)=TM+TB:GOTO 730
790       NEXT J:PRINT "Error !!!":END
800     IF MK=0 THEN 880
810     FOR J=1 TO I-2
820       IF D(J,2)=&HC0 THEN 840
830        D1=D(J,2):D2=D(J+1,1)-D(J,1):D3=D(J,3)-D(J,1):D4=D(J,4):GOTO 860
840       D1=&HC0:D2=D(J,3):D3=0:D4=0:GOSUB 890
850        D1=&H80:D2=D(J+1,1)-D(J,1):D3=0:D4=0
860      PRINT "*";:GOSUB 890:NEXT J
870       D1=D(J,2):D2=D(J,3)-D(J,1):D3=D(J,3)-D(J,1):D4=D(J,4):GOSUB 890
880       D1=&HD0:D2=0:D3=0:D4=0:GOSUB 890:CLOSE:RETURN
890     IF D2>255 THEN DN=D2-255:DM=D1:D1=&H80:D2=&HFF:D3=0:D4=0:GOSUB 910:D1=DM:D2=DN:GOTO 890
900      IF D3>255 THEN D3=D3 AND 255:PRINT " $$$ ";
910     IF D1<16 THEN PRINT #2,"0"+HEX$(D1); ELSE PRINT #2,HEX$(D1);
920      IF D2<16 THEN PRINT #2,"0"+HEX$(D2); ELSE PRINT #2,HEX$(D2);
930      IF D3<16 THEN PRINT #2,"0"+HEX$(D3); ELSE PRINT #2,HEX$(D3);
940      IF D4<16 THEN PRINT #2,"0"+HEX$(D4); ELSE PRINT #2,HEX$(D4);
950     RETURN
960  '*****  Init File Read  *****************************************
970    IF PHASE>0 THEN 1020
980      'CHDIR "a:\data"
990      'SHELL "copy a:"+G$+".dat f:"+G$+".dat"
1000      'SHELL "hdcut f:"+G$+".dat f:"+G$+".cut"
1010       SHELL "bin2asc f:"+G$+".cut f:"+G$+".asc":CLS
1020    IF PHASE>1 THEN 1510
1030     DIM AS(16):RESTORE 30
1040     FOR I=1 TO 16:READ AS(I):NEXT I
1050     OPEN "f:"+G$+".asc" FOR INPUT AS #10
1060     OPEN "f:tr1.trk" FOR OUTPUT AS #1
1070     OPEN "f:tr2.trk" FOR OUTPUT AS #2
1080     OPEN "f:tr3.trk" FOR OUTPUT AS #3
1090     OPEN "f:tr4.trk" FOR OUTPUT AS #4
1100     OPEN "f:tr5.trk" FOR OUTPUT AS #5
1110     OPEN "f:tr6.trk" FOR OUTPUT AS #6
1120     OPEN "f:tr7.trk" FOR OUTPUT AS #7
1130     OPEN "f:tr8.trk" FOR OUTPUT AS #8
1140     ZM=0
1150   IF EOF(10) THEN 1510 ELSE IP1$=INPUT$(1,#10):IP2$=INPUT$(1,#10)
1160     DT1=16*VAL("&H"+IP1$)+VAL("&H"+IP2$):ZM=ZM+DT1
1170     IF DT1=&HF8 THEN 1510
1180     IP1$=INPUT$(1,#10):IP2$=INPUT$(1,#10)
1190     GOSUB 1220
1200     PRINT ZM,HEX$(DT1),HEX$(DT2),HEX$(DT3),HEX$(DT4)
1210     GOTO 1150
1220   DT2=16*VAL("&H"+IP1$)+VAL("&H"+IP2$)
1230     IF DT2<&H80 AND ST>&HBF AND ST<&HD0 THEN 1360
1240     IF DT2<&H80 AND ST>&H8F AND ST<&HA0 THEN 1380
1250     IF DT2>&HBF AND DT2<&HD0 THEN 1330
1260     IF DT2>&H8F AND DT2<&HA0 THEN 1280
1270     PRINT "Error":END
1280    ST=DT2:IP1$=INPUT$(1,#10):IP2$=INPUT$(1,#10)
1290      DT3=16*VAL("&H"+IP1$)+VAL("&H"+IP2$)
1300      IP1$=INPUT$(1,#10):IP2$=INPUT$(1,#10)
1310      DT4=16*VAL("&H"+IP1$)+VAL("&H"+IP2$)
1320      GOSUB 1410:RETURN
1330    ST=DT2:IP1$=INPUT$(1,#10):IP2$=INPUT$(1,#10)
1340      DT3=16*VAL("&H"+IP1$)+VAL("&H"+IP2$):DT4=0
1350      GOSUB 1410:RETURN
1360    DT3=DT2:DT2=ST:DT4=0
1370      GOSUB 1410:RETURN
1380    DT3=DT2:DT2=ST:IP1$=INPUT$(1,#10):IP2$=INPUT$(1,#10)
1390      DT4=16*VAL("&H"+IP1$)+VAL("&H"+IP2$)
1400      GOSUB 1410:RETURN
1410    CH=DT2 AND &HF:IF AS(CH+1)>16 THEN RETURN
1420      ON AS(CH+1) GOTO 1430,1440,1450,1460,1470,1480,1490,1500
1430       PRINT #1,ZM/QT,DT2,DT3,DT4:RETURN
1440       PRINT #2,ZM/QT,DT2,DT3,DT4:RETURN
1450       PRINT #3,ZM/QT,DT2,DT3,DT4:RETURN
1460       PRINT #4,ZM/QT,DT2,DT3,DT4:RETURN
1470       PRINT #5,ZM/QT,DT2,DT3,DT4:RETURN
1480       PRINT #6,ZM/QT,DT2,DT3,DT4:RETURN
1490       PRINT #7,ZM/QT,DT2,DT3,DT4:RETURN
1500       PRINT #8,ZM/QT,DT2,DT3,DT4:RETURN
1510   CLOSE:RETURN
1520  '*****  Debug Only  *****************************************
1530 OPEN "f:tr6.trk" FOR INPUT AS #1
1540 IF EOF(1) THEN END
1550 INPUT #1,A,B,C,D
1560 PRINT HEX$(A),HEX$(B),HEX$(C),HEX$(D)
1570 GOTO 1540

BASIC その11 DSP周波数データの計算

10 'save "dsp-fno.bas",a
20 KEY OFF
30 CLS:DIM X$(12):FOR I=0 TO 11:X$(I)="        db      ":NEXT I
40 DIM Y$(12):FOR I=0 TO 11:Y$(I)="        db      ":NEXT I
50 DIM Z$(12):FOR I=0 TO 11:Z$(I)="        db      ":NEXT I
60 G#=3520#*(2#^(10.5#/12#)):H#=(2#^(1#/12#/32#)):A#=(2#^(1#/12#)):FOR I=0 TO 11
70 PRINT :PRINT :PRINT :PRINT :PRINT
80 RESTORE 210:FOR K=1 TO (12-I):READ A$:NEXT K:PRINT "===== [ ";A$;" ] ====="
90 B#=1#/A#:FOR N=0 TO I:B#=B#*A#:NEXT N
100 FOR L=0 TO 31:PRINT "    Offset (";:E$=HEX$(L):IF LEN(E$)=1 THEN E$="0"+E$
110 PRINT E$;") :";:P#=1#/H#:FOR N=0 TO L:P#=P#*H#:NEXT N:U$=HEX$(L):IF LEN(U$)=1 THEN U$="0"+U$
120 O#=G#*P#*B#:PRINT "  f =";:PRINT USING "#####.######";O#;:PRINT "Hz";
130 Q#=2#^24#*O#/62500#:C$=HEX$(INT(Q#)/256):PRINT "  Data = ";C$;
140 D$=HEX$(INT(Q#)-256*INT(Q#/256)):IF LEN(D$)=1 THEN D$="0"+D$
150 PRINT D$;:PRINT:X$(I)=X$(I)+"0"+LEFT$(C$,2)+"h,"
160 Y$(I)=Y$(I)+"0"+RIGHT$(C$,2)+"h,":Z$(I)=Z$(I)+"0"+D$+"h,"
170 IF L MOD 16<>15 THEN 190
180 IF INKEY$="" THEN 180 ELSE CLS
190 NEXT L:PRINT
200 NEXT I
210 DATA "G -9","F#-9","F -9","E -9"
220 DATA "Eb-9","D -9","C#-9","C -9","B -8","Bb-8","A -8","Ab-8"

BASIC その12 DSPのパラメータ編集

10 'save "dspedit.bas",a
20  DATA 2,0,1,1,1,3,3,3,3,3,3,3,3,3,3,3
30 CLS:DIM F$(64):GOSUB 2460:GOSUB 2280:GOSUB 2590:'<--- File Access
40      '''A$="c:dsdemo2.dat":'********* D U M M Y ************
50   PRINT:PRINT :GOSUB 1370:CLS:GOTO 740
60 VIEW PRINT 1 TO 23:LOCATE 15,5:PRINT "<< PHASE #7 : Head Block Set >>"
70   DIM HEAD(64):FOR I=1 TO 16:HEAD(I-1)=ASC(MID$(NSM$,I,1)):NEXT I
80   VIEW PRINT 17 TO 22:FOR I=0 TO 15:PRINT HEAD(I);:NEXT I
90   FOR I=16 TO 33:HEAD(I)=0:NEXT I:HEAD(&H14)=4:HEAD(&H15)=120
100   IF TIMMAX=576 THEN HEAD(&H13)=3 ELSE HEAD(&H13)=4
110   HEAD(&H22)=(4*EVECTB+12) MOD 256
120   HEAD(&H23)=INT((4*EVECTB+12)/256)
130   HEAD(&H24)=((4*(EVECTB+EVECTC)+24) MOD 256)
140   HEAD(&H25)=INT((4*(EVECTB+EVECTC)+24)/256)
150   FOR I=0 TO 12
160    HEAD(&H26+2*I)=((4*(EVECTB+EVECTC+EVECTR)+36+4*I) MOD 256)
170    HEAD(&H27+2*I)=INT((4*(EVECTB+EVECTC+EVECTR)+36+4*I)/256)
180   NEXT I
190   PRINT:FOR I=16 TO 63:PRINT HEAD(I);:NEXT I
200   DIM ENDMK(4):ENDMK(1)=&HFC:ENDMK(2)=0:ENDMK(3)=0:ENDMK(4)=0
210   DIM LOPMK(4):LOPMK(1)=&HE1:LOPMK(2)=0:LOPMK(3)=0:LOPMK(4)=0
220  OPEN NKM$ FOR OUTPUT AS #3
230   FOR I=0 TO 63:PRINT #3,CHR$(HEAD(I));:NEXT I
240   FOR I=1 TO EVECTB:FOR K=1 TO 4:PRINT #3,CHR$(CH1(K,I));:NEXT K:NEXT I
250    TSUM=0:FOR I=1 TO EVECTB:TSUM=TSUM+CH1(2,I):NEXT I
260     PRINT #3,CHR$(&HF8);
270     PRINT #3,CHR$(INT(TIMMAX/IQ)-TSUM);
280     PRINT #3,CHR$(0);:PRINT #3,CHR$(0);
290    FOR K=1 TO 4:PRINT #3,CHR$(LOPMK(K));:NEXT K
300    FOR K=1 TO 4:PRINT #3,CHR$(ENDMK(K));:NEXT K
310   FOR I=1 TO EVECTC:FOR K=1 TO 4:PRINT #3,CHR$(CH2(K,I));:NEXT K:NEXT I
320    TSUM=0:FOR I=1 TO EVECTC:TSUM=TSUM+CH2(2,I):NEXT I
330     PRINT #3,CHR$(&HF8);
340     PRINT #3,CHR$(INT(TIMMAX/IQ)-TSUM);
350     PRINT #3,CHR$(0);:PRINT #3,CHR$(0);
360    FOR K=1 TO 4:PRINT #3,CHR$(LOPMK(K));:NEXT K
370    FOR K=1 TO 4:PRINT #3,CHR$(ENDMK(K));:NEXT K
380   FOR I=1 TO EVECTR:FOR K=1 TO 4:PRINT #3,CHR$(CH3(K,I));:NEXT K:NEXT I
390    TSUM=0:FOR I=1 TO EVECTR:TSUM=TSUM+CH3(2,I):NEXT I
400     PRINT #3,CHR$(&HF8);
410     PRINT #3,CHR$(INT(TIMMAX/IQ)-TSUM);
420     PRINT #3,CHR$(0);:PRINT #3,CHR$(0);
430    FOR K=1 TO 4:PRINT #3,CHR$(LOPMK(K));:NEXT K
440    FOR I=1 TO 14:FOR K=1 TO 4:PRINT #3,CHR$(ENDMK(K));:NEXT K:NEXT I
450  CLOSE #3
460    FOR I=1 TO 8:PRINT:NEXT I
470    VIEW PRINT 1 TO 23:LOCATE 15,5:PRINT "                               "
480   LOCATE 12,15:PRINT "*****   is completely finished."
490   PRINT
500          END:'''  VIEW PRINT 1 TO 23:KEY ON:END:'***************
510 VIEW PRINT 1 TO 23:LOCATE 15,5:PRINT "<< PHASE #6 : 24's Normalize >>"
520   VIEW PRINT 17 TO 22:PRINT "Max Time Count =";TIMMAX
530   IF PRM6=1 THEN LPRINT:LPRINT "***** Quantize Normalise *****"
540   IF PRM6=1 THEN LPRINT:LPRINT ""
550   FOR I=1 TO EVECTB:CH1(2,I)=INT(CH1(2,I)/IQ):CH1(3,I)=INT(CH1(3,I)/IQ):NEXT I
560    FOR J=1 TO EVECTB:IF PRM6=0 THEN 580
570     LPRINT HEX$(CH1(1,J));"  ";CH1(2,J);CH1(3,J);"  ";HEX$(CH1(4,J))
580     PRINT HEX$(CH1(1,J));"  ";CH1(2,J);CH1(3,J);"  ";HEX$(CH1(4,J))
590    NEXT J
600   IF PRM6=1 THEN LPRINT:LPRINT ""
610   FOR I=1 TO EVECTC:CH2(2,I)=INT(CH2(2,I)/IQ):CH2(3,I)=INT(CH2(3,I)/IQ):NEXT I
620    FOR J=1 TO EVECTC:IF PRM6=0 THEN 640
630     LPRINT HEX$(CH2(1,J));"  ";CH2(2,J);CH2(3,J);"  ";HEX$(CH2(4,J))
640     PRINT HEX$(CH2(1,J));"  ";CH2(2,J);CH2(3,J);"  ";HEX$(CH2(4,J))
650    NEXT J
660   IF PRM6=1 THEN LPRINT:LPRINT ""
670   FOR I=1 TO EVECTR:CH3(2,I)=INT(CH3(2,I)/IQ):CH3(3,I)=INT(CH3(3,I)/IQ):NEXT I
680    FOR J=1 TO EVECTR:IF PRM6=0 THEN 700
690     LPRINT HEX$(CH3(1,J));"  ";CH3(2,J);CH3(3,J);"  ";HEX$(CH3(4,J))
700     PRINT HEX$(CH3(1,J));"  ";CH3(2,J);CH3(3,J);"  ";HEX$(CH3(4,J))
710    NEXT J
720   FOR I=1 TO 6:PRINT :NEXT I
730   GOTO 60
740 OPEN A$ FOR INPUT AS #1:LL=LOF(1):LOCATE 1,10:PRINT "File Name = ";A$;
750   PRINT "   ( Data Length =";LL;"Bytes )":DIM D(LL)
760   IF PRM6+PRM5+PRM4+PRM3+PRM2+PRM1<>0 THEN LPRINT "File Name = ";A$:LPRINT
770   LOCATE 3,10:PRINT"Ch.Assign : Bass   < 1st Track > =  ";
780   FOR I=0 TO 15:IF AS(I)=O THEN PRINT HEX$(I+1);"  ";
790   NEXT I:LOCATE 4,10:PRINT"            Chord  < 2nd Track > =  ";
800   FOR I=0 TO 15:IF AS(I)=1 THEN PRINT HEX$(I+1);"  ";
810   NEXT I:LOCATE 5,10:PRINT"            Rhythm < 3rd Track > =  ";
820   FOR I=0 TO 15:IF AS(I)=2 THEN PRINT HEX$(I+1);"  ";
830   NEXT I
840 GOTO 1460:'-----------------------------------------------------
850   IF PRM4=0 THEN 880
860   LPRINT "    Source = [";EVC(0);EVC(1);EVC(2);"]"
870   LPRINT "    Result = [";MAXB;MAXC;MAXR;"]"
880   VIEW PRINT 1 TO 23:LOCATE 7,10:PRINT "Double Event Check : (";
890   PRINT EVC(0);EVC(1);EVC(2);") --> (";MAXB;MAXC;MAXR;")"
900 LOCATE 13,5:PRINT "                               "
910   LOCATE 15,5:PRINT "<< PHASE #5 : KAMIYA Convert >>"
920   LOCATE 9,10:PRINT "Making File = ";NKM$;"   ( Song Name = [";NSM$;"] )"
930   DIM CH1(4,MAXB),CH2(4,MAXC),CH3(4,MAXR):VIEW PRINT 17 TO 22
940   CNTB=2:CNTC=2:CNTR=2
950   CH1(1,1)=BASS(2,1):CH1(2,1)=0:CH1(3,1)=0:CH1(4,1)=BASS(3,1)
960   CH2(1,1)=CHORD(2,1):CH2(2,1)=0:CH2(3,1)=0:CH2(4,1)=CHORD(3,1)
970   CH3(1,1)=RHYTHM(2,1):CH3(2,1)=0:CH3(3,1)=0:CH3(4,1)=RHYTHM(3,1)
980    FOR I=2 TO MAXB:IF BASS(3,I)=0 THEN 1030
990      CH1(1,CNTB)=BASS(2,I):CH1(2,CNTB)=BASS(1,I)
1000      CH1(3,CNTB)=BASS(1,I):CH1(4,CNTB)=BASS(3,I)
1010      CH1(2,CNTB-1)=BASS(1,I)-CH1(2,CNTB-1)
1020      JJ=CNTB:CNTB=CNTB+1:GOTO 1050
1030     FOR J=CNTB-1 TO -1 STEP -1:IF BASS(2,I)=CH1(1,J) THEN 1040 ELSE NEXT J
1040      CH1(3,J)=BASS(1,I)-CH1(3,J)
1050    NEXT I:CH1(2,JJ)=BASS(1,I-1)-CH1(2,JJ):EVECTB=JJ
1060     FOR J=1 TO JJ:IF PRM5=0 THEN 1080
1070      LPRINT HEX$(CH1(1,J));"  ";CH1(2,J);CH1(3,J);"  ";HEX$(CH1(4,J))
1080      PRINT HEX$(CH1(1,J));"  ";CH1(2,J);CH1(3,J);"  ";HEX$(CH1(4,J))
1090     NEXT J
1100    FOR I=2 TO MAXC:IF CHORD(3,I)=0 THEN 1150
1110      CH2(1,CNTC)=CHORD(2,I):CH2(2,CNTC)=CHORD(1,I)
1120      CH2(3,CNTC)=CHORD(1,I):CH2(4,CNTC)=CHORD(3,I)
1130      CH2(2,CNTC-1)=CHORD(1,I)-CH2(2,CNTC-1)
1140      JJ=CNTC:CNTC=CNTC+1:GOTO 1170
1150     FOR J=CNTC-1 TO -1 STEP -1:IF CHORD(2,I)=CH2(1,J) THEN 1160 ELSE NEXT J
1160      CH2(3,J)=CHORD(1,I)-CH2(3,J)
1170    NEXT I:CH2(2,JJ)=CHORD(1,I-1)-CH2(2,JJ):EVECTC=JJ
1180     FOR J=1 TO JJ:IF PRM5=0 THEN 1200
1190      LPRINT HEX$(CH2(1,J));"  ";CH2(2,J);CH2(3,J);"  ";HEX$(CH2(4,J))
1200      PRINT HEX$(CH2(1,J));"  ";CH2(2,J);CH2(3,J);"  ";HEX$(CH2(4,J))
1210     NEXT J
1220    FOR I=2 TO MAXR:IF RHYTHM(3,I)=0 THEN 1270
1230      CH3(1,CNTR)=RHYTHM(2,I):CH3(2,CNTR)=RHYTHM(1,I)
1240      CH3(3,CNTR)=RHYTHM(1,I):CH3(4,CNTR)=RHYTHM(3,I)
1250      CH3(2,CNTR-1)=RHYTHM(1,I)-CH3(2,CNTR-1)
1260      JJ=CNTR:CNTR=CNTR+1:GOTO 1290
1270     FOR J=CNTR-1 TO -1 STEP -1:IF RHYTHM(2,I)=CH3(1,J) THEN 1280 ELSE NEXT J
1280      CH3(3,J)=RHYTHM(1,I)-CH3(3,J)
1290    NEXT I:CH3(2,JJ)=RHYTHM(1,I-1)-CH3(2,JJ):EVECTR=JJ
1300     FOR J=1 TO JJ:IF PRM5=0 THEN 1320
1310      LPRINT HEX$(CH3(1,J));"  ";CH3(2,J);CH3(3,J);"  ";HEX$(CH3(4,J))
1320      PRINT HEX$(CH3(1,J));"  ";CH3(2,J);CH3(3,J);"  ";HEX$(CH3(4,J))
1330     NEXT J
1340    FOR I=1 TO 6:PRINT :NEXT I
1350 GOTO 510
1360  ' -------------------------------------------
1370 PRINT "Selected Data File Name = ";A$:PRINT
1380   PRINT "   Output [TESTER] File Name = ";LEFT$(A$,2);
1390   INPUT "",NKM$:IF NKM$<>"" THEN NKM$=LEFT$(A$,2)+NKM$+".KMY":PRINT:GOTO 1430
1400   FOR I=1 TO 10:PP$=MID$(A$,I,1):IF PP$="." THEN 1420
1410   NKM$=NKM$+PP$:NEXT I
1420   NKM$=NKM$+".KMY":PRINT
1430   INPUT "   Song Name = ",NSM$:IF LEN(NSM$)>16 THEN NSM$=LEFT$(NSM$,16)
1440   IF LEN(NSM$)<16 THEN NSM$=NSM$+STRING$(16-LEN(NSM$)," ")
1450   PRINT :RETURN
1460 LOCATE 13,5:PRINT "<< PHASE #1 : Data File Read >>":VIEW PRINT 15 TO 22
1470   FOR I=1 TO LL:B$=INPUT$(1,#1):C=ASC(B$):D$="":IF C<16 THEN D$="0"
1480    D(I)=C:PRINT D$+HEX$(C);" ";:IF I MOD 25=0 THEN PRINT
1490    IF PRM1=1 THEN LPRINT D$+HEX$(C);" ";:IF I MOD 25=0 THEN LPRINT
1500   NEXT I:CLOSE #1:FOR I=1 TO 8:PRINT :NEXT I
1510 VIEW PRINT 1 TO 23:LOCATE 13,5:PRINT "<< PHASE #2 : Format Convert >>"
1520   VIEW PRINT 15 TO 22:DIM FM(INT(LL/3),4):TIM=0:EVE=1:LOP=0
1530   GOSUB 2430:TIM=TIM+TB:GOSUB 2350:FM(EVE,1)=TIM:FM(EVE,2)=ST
1540   FM(EVE,3)=D(LOP):IF ST>&HBF AND ST<&HD0 THEN FM(EVE,4)=0:GOTO 1560
1550   LOP=LOP+1:FM(EVE,4)=D(LOP)
1560   IF PRM2=0 THEN 1600 ELSE IF EVE=1 THEN LPRINT
1570   LPRINT "  Event NO. =";EVE;" ---  Time =";FM(EVE,1);" ---  Data = [ ";
1580   FOR K=2 TO 4:PD=FM(EVE,K):DM$="":IF PD<16 THEN DM$="0"
1590    LPRINT DM$+HEX$(PD)+" ";:NEXT K:LPRINT "]"
1600   PRINT "  Event NO. =";EVE;" ---  Time =";FM(EVE,1);" ---  Data = [ ";
1610   FOR K=2 TO 4:PD=FM(EVE,K):DM$="":IF PD<16 THEN DM$="0"
1620    PRINT DM$+HEX$(PD)+" ";:NEXT K:PRINT "]"
1630   EVE=EVE+1:GOTO 1530
1640   TIMMAX=TIM:FOR I=1 TO 8:PRINT :NEXT I:VIEW PRINT 1 TO 23
1650 LOCATE 13,5:PRINT "<< PHASE #3 : Channel Assign >>":VIEW PRINT 15 TO 22
1660   DIM RD(3,4,EVE),EVC(3):TOF=0:FOR I=0 TO 3:EVC(I)=0:NEXT I
1670   DIM NM$(3):NM$(0)="Bass  ":NM$(1)="Chord ":NM$(2)="Rhythm"
1680   FOR I=1 TO EVE-1:IF FM(I,2)>&H9F THEN TOF=TOF+FM(I,1):GOTO 1790
1690    CY=AS(FM(I,2) MOD 16):IF CY>2 THEN TOF=TOF+FM(I,1):GOTO 1790
1700    EVC(CY)=EVC(CY)+1:FOR K=1 TO 4:RD(CY,K,EVC(CY))=FM(I,K):NEXT K
1710    IF (FM(I,4)<>0 AND &H8F>":VIEW PRINT 15 TO 22
1810   DIM BASS(3,EVC(0)):CTT=0:FOR I=1 TO EVC(0):IF RD(0,4,I)=0 THEN 1860
1820    FOR J=I-1 TO 1 STEP -1
1830     IF RD(0,3,J)=RD(0,3,I) AND RD(0,4,J)=0 THEN 1900
1840     IF RD(0,3,J)=RD(0,3,I) AND RD(0,4,J)<>0 THEN 1950
1850    NEXT J:GOTO 1900
1860    FOR J=I-1 TO 1 STEP -1
1870     IF RD(0,3,J)=RD(0,3,I) AND RD(0,4,J)<>0 THEN 1900
1880     IF RD(0,3,J)=RD(0,3,I) AND RD(0,4,J)=0 THEN 1950
1890    NEXT J
1900    CTT=CTT+1
1910    BASS(1,CTT)=RD(0,1,I):BASS(2,CTT)=RD(0,3,I):BASS(3,CTT)=RD(0,4,I)
1920    FOR K=1 TO 3:PRINT HEX$(BASS(K,CTT));"  ";:NEXT K
1930    IF PRM4=0 THEN 1950
1940    FOR K=1 TO 3:LPRINT HEX$(BASS(K,CTT));"  ";:NEXT K:LPRINT "/  ";
1950    NEXT I:MAXB=CTT
1960   DIM CHORD(3,EVC(1)):CTT=0:FOR I=1 TO EVC(1):IF RD(1,4,I)=0 THEN 2010
1970    FOR J=I-1 TO 1 STEP -1
1980     IF RD(1,3,J)=RD(1,3,I) AND RD(1,4,J)=0 THEN 2050
1990     IF RD(1,3,J)=RD(1,3,I) AND RD(1,4,J)<>0 THEN 2100
2000    NEXT J:GOTO 2050
2010    FOR J=I-1 TO 1 STEP -1
2020     IF RD(1,3,J)=RD(1,3,I) AND RD(1,4,J)<>0 THEN 2050
2030     IF RD(1,3,J)=RD(1,3,I) AND RD(1,4,J)=0 THEN 2100
2040    NEXT J
2050    CTT=CTT+1
2060    CHORD(1,CTT)=RD(1,1,I):CHORD(2,CTT)=RD(1,3,I):CHORD(3,CTT)=RD(1,4,I)
2070    FOR K=1 TO 3:PRINT HEX$(CHORD(K,CTT));"  ";:NEXT K
2080    IF PRM4=0 THEN 2100
2090    FOR K=1 TO 3:LPRINT HEX$(CHORD(K,CTT));"  ";:NEXT K:LPRINT "/  ";
2100    NEXT I:MAXC=CTT
2110   DIM RHYTHM(3,EVC(2)):CTT=0:FOR I=1 TO EVC(2):IF RD(2,4,I)=0 THEN 2160
2120    FOR J=I-1 TO 1 STEP -1
2130     IF RD(2,3,J)=RD(2,3,I) AND RD(2,4,J)=0 THEN 2200
2140     IF RD(2,3,J)=RD(2,3,I) AND RD(2,4,J)<>0 THEN 2250
2150    NEXT J:GOTO 2200
2160    FOR J=I-1 TO 1 STEP -1
2170     IF RD(2,3,J)=RD(2,3,I) AND RD(2,4,J)<>0 THEN 2200
2180     IF RD(2,3,J)=RD(2,3,I) AND RD(2,4,J)=0 THEN 2250
2190    NEXT J
2200    CTT=CTT+1
2210    RHYTHM(1,CTT)=RD(2,1,I):RHYTHM(2,CTT)=RD(2,3,I):RHYTHM(3,CTT)=RD(2,4,I)
2220    FOR K=1 TO 3:PRINT HEX$(RHYTHM(K,CTT));"  ";:NEXT K
2230    IF PRM4=0 THEN 2250
2240    FOR K=1 TO 3:LPRINT HEX$(RHYTHM(K,CTT));"  ";:NEXT K:LPRINT "/  ";
2250    NEXT I:MAXR=CTT
2260 FOR I=1 TO 8:PRINT :NEXT I:GOTO 850
2270    '--------------------------------------------------------------
2280 DIM AS(16):PRINT "MIDI Channel Assign ( Default=Below ) :":PRINT
2290  RESTORE 20:FOR I=0 TO 15:READ AS(I):NEXT I
2300  PRINT "    [0] = Bass  ,  [1] = Chord  ,  [2] = Rhythm":PRINT
2310  FOR I=0 TO 15:PRINT "         Ch(";HEX$(I);") =";AS(I);:NEXT I:PRINT
2320  PRINT:INPUT "                 Ok (Y/N) ? ",A$:IF A$<>"n" THEN PRINT:RETURN
2330  PRINT:FOR I=0 TO 15:PRINT "    Channel [";HEX$(I);:INPUT "] ---> ",AS(I)
2340  NEXT I:PRINT:PRINT "New Assigned Channel :":PRINT :GOTO 2300
2350 LOP=LOP+1:ST=D(LOP):IF 127 dirlist.mes":OPEN "dirlist.mes" FOR INPUT AS #2
2620  INPUT #2,F$(J):IF F$(J)="" THEN 2620 ELSE AA=ASC(F$(J))
2630  IF AA>64 AND AA<91 THEN F$(J)=LEFT$(F$(J),8):J=J+1
2640  IF AA>57 OR AA<48 THEN 2620 ELSE CLOSE #2:J=J-1
2650  CLS:PRINT "Select File :":K=J
2660  FOR I=1 TO J:LOCATE 3+INT((I-1)/5),8+14*((I-1) MOD 5):PRINT F$(I):NEXT I
2670  LOCATE 3+INT((J-1)/5),6+14*((J-1) MOD 5):PRINT "<";
2680  LOCATE 3+INT((J-1)/5),17+14*((J-1) MOD 5):PRINT ">";
2690  LOCATE 3+INT((J-1)/5),5+14*((J-1) MOD 5)
2700  B$=INKEY$:IF B$="" THEN 2700 ELSE IF B$=CHR$(13) THEN 2760
2710  IF LEN(B$)=1 THEN 2700
2720  IF ASC(RIGHT$(B$,1))=80 AND J=0 AND J1 THEN GOSUB 2770:J=J-1:GOTO 2670
2750  IF ASC(RIGHT$(B$,1))=72 AND J>5 THEN GOSUB 2770:J=J-5:GOTO 2670 ELSE GOTO 2700
2760  A$=DR$+":"+F$(J)+".DAT":RETURN
2770 LOCATE 3+INT((J-1)/5),6+14*((J-1) MOD 5):PRINT " ";
2780  LOCATE 3+INT((J-1)/5),17+14*((J-1) MOD 5):PRINT " ";:RETURN

BASIC その13 Mac用コンバータ

10 'save "macconv.bas",a
20   CHDIR "a:\data":DIM F$(150):J=1
30  SHELL "dir a:*.dat > c:dirlist.mes"
40   OPEN "c:dirlist.mes" FOR INPUT AS #1
50   IF EOF(1) THEN 80
60    INPUT #1,F$(J)
70    F$(J)=LEFT$(F$(J),8):J=J+1:GOTO 50
80   CLOSE #1
90  FOR I=5 TO J-3:PRINT F$(I);" ";:NEXT I
100     IF INKEY$="" THEN 100:'*******************
110  FOR I=5 TO J-3
120     G$="":FOR K=1 TO 8
130      H$=MID$(F$(I),K,1):IF H$<>" " THEN G$=G$+H$
140     NEXT K
150    SHELL "trauto a:"+G$+".dat a:"+G$+".cut"
160    SHELL "bin2asc a:"+G$+".cut a:"+G$+".asc"
170  NEXT I

BASIC その14 MIDIのテスト

10 'save "miditest.bas",a
20 OPEN "com1:9600,n,8,1" AS #1
30 START$=":90":CLEAN$=":B07B00"
40 PRINT #1,START$;
50 FOR I=100 TO 30 STEP -1
60 PRINT #1,HEX$(I)+"7F";
70 FOR J=1 TO 50:NEXT J
80 NEXT I
90 PRINT #1,CLEAN$;

BASIC その15 非線形変換の実験

10 'SAVE "nonline",A
20 KEY OFF:SCREEN 1:DIM A(256),B(256),X(4),Y(4)
30 X(1)=50:Y(1)=186:X(2)=400:Y(2)=360:X(3)=750:Y(3)=215:X(4)=750:Y(4)=564
40 FOR I=1 TO 4:LINE (X(I),Y(I)-128)-(X(I)+257,Y(I)+129),1,B:NEXT I
50 FOR I=1 TO 4:LINE (X(I),Y(I))-(X(I)+257,Y(I)),1,,&HCCCC:NEXT I
60 FOR I=1 TO 2:LINE (X(I)+257,Y(I))-(X(I+1),Y(I+1)),1,,&HFCFC:NEXT I
70 LINE (X(2)+257,Y(2))-(X(4),Y(4)),1,,&HFCFC
80 LOCATE 2,9,0:PRINT "Source Wave";
90 LOCATE 3,63:PRINT "Result Wave";
100 LOCATE 8,36:PRINT "Trans Wave";
110 LOCATE 15,63:PRINT "Reverse Wave";
120 FOR I=1 TO 256:GOSUB 170:PSET (X(1)+I,Y(1)+128-A(I)):NEXT I
130 FOR I=1 TO 256:GOSUB 180:PSET (X(2)+I,Y(2)+128-B(I)):NEXT I
140 FOR I=1 TO 256:PSET (X(3)+I,Y(3)+128-B(A(I))):NEXT I
150 FOR I=1 TO 256:PSET (X(4)+I,Y(4)+128-A(B(I))):NEXT I
160 A$=INKEY$:IF A$<>" " THEN 160 ELSE SCREEN 0:KEY ON:LOCATE 1,1,1:END
170 A(I)=INT(127.999*SIN(I*3.1415926535#/128)+128):RETURN
180 B(I)=I:RETURN

BASIC その16 小物プログラム集

10 'save "outconv.bas",a
20   CHDIR "a:\data":DIM F$(150):J=1
30  SHELL "dir a:*.kmy > c:dirlist.mes"
40   OPEN "c:dirlist.mes" FOR INPUT AS #1
50   IF EOF(1) THEN 80
60    INPUT #1,F$(J)
70    F$(J)=LEFT$(F$(J),8):J=J+1:GOTO 50
80   CLOSE #1
90  FOR I=5 TO J-3:PRINT F$(I);" ";:NEXT I
100     IF INKEY$="" THEN 100:'*******************
110  CHDIR "a:\"
120  FOR I=5 TO J-3
130     G$="":FOR K=1 TO 8
140      H$=MID$(F$(I),K,1):IF H$<>" " THEN G$=G$+H$
150     NEXT K
160    SHELL "bin2asc a:\data\"+G$+".kmy a:\"+G$+".asc /w"
170  NEXT I


10 'save "pccont1.bas",a
20 'save "a:control3.bas",a
30   GOSUB *PARAINIT:GOSUB *DISPINIT:GOSUB *PORTINIT
40 *MAIN:A$=INKEY$:IF A$="" THEN *MAIN
50   IF A$=CHR$(&H1B) THEN *FINISH
60   IF A$=CHR$(30) THEN GOSUB *UPMOVE
70   IF A$=" " OR A$=CHR$(31) THEN GOSUB *DOWNMOVE
80   IF A$=CHR$(253) OR A$=CHR$(13) THEN ON MASKOLD GOSUB *CABLE,*DUMMY,*DUMMY,*DUMMY,*DUMMY,*DUMMY,*DUMMY,*DUMMY,*DUMMY,*DUMMY,*DUMMY,*DUMMY
90   Z$=A$:GOSUB *DATADISP
100   GOTO *MAIN
110 *FINISH:CLS 2:CONSOLE 0,25,1,0,1:LOCATE ,,1:LIST 20-*FINISH:END
120 *INIT
130   RETURN
140 *DUMMY:RETURN
150 *CABLE:GOSUB *CLRSCRN:RESTORE *DATA2:READ NEX$:FOR I=1 TO 7:READ DD$:PRINT DD$:NEXT I:PRINT NEX$;
160 *CABLE1:DDIN$=INKEY$:IF DDIN$="" THEN *CABLE1 ELSE PRINT:FOR I=1 TO 7:READ DD$:PRINT DD$:NEXT I:PRINT NEX$;
170 *CABLE2:DDIN$=INKEY$:IF DDIN$="" THEN *CABLE2 ELSE GOSUB *CLRSCRN:RETURN
180 *DATADISP:PRINT HEX$(ASC(Z$));" ";:RETURN
190 *UPMOVE
200   MASKNEW=MASKNEW-1:IF MASKNEW=0 THEN MASKNEW=KINDS
210   GOSUB *CURSORWRITE:RETURN
220 *DOWNMOVE
230   MASKNEW=MASKNEW+1:IF MASKNEW>KINDS THEN MASKNEW=1
240   GOSUB *CURSORWRITE:RETURN
250 *CURSORWRITE
260   COLOR@ (12,MASKOLD+2)-(LEN(B$(MASKOLD))+13,MASKOLD+2),0
270   COLOR@ (12,MASKNEW+2)-(LEN(B$(MASKNEW))+13,MASKNEW+2),4
280   MASKOLD=MASKNEW:RETURN
290 *DISPINIT:WIDTH 80,25:CONSOLE ,,0,0,1
300   LOCATE 12,1:PRINT "C O N T R O L    P A N E L"
310   LOCATE 50,1:PRINT "produced by Y.Nagashima"
320   COLOR@ (2,1)-(77,1),4
330   LOCATE 5,3:PRINT "[MENU]"
340   FOR I=1 TO KINDS
350    LOCATE 13,I+2:PRINT B$(I)
360   NEXT I
370   MASKOLD=1:MASKNEW=1:GOSUB *CURSORWRITE
380   LOCATE 0,16:PRINT "-----< Data Area >"+STRING$(62,"-")
390   CONSOLE 17,8:LOCATE 0,17,0:RETURN
400 *CLRSCRN:PRINT:PRINT:PRINT:PRINT:PRINT:PRINT:PRINT:PRINT:PRINT:LOCATE 0,17:RETURN
410 *PORTINIT:OPEN "com1:n81nn" AS#1:RETURN
420 *PARAINIT:KINDS=12:DIM B$(KINDS)
430   RESTORE *DATA1:FOR I=1 TO KINDS:READ B$(I):NEXT I:RETURN
440 *DATA1
450   DATA "MIDI Cable Connection List"
460   DATA "Initial System Setting","Parameter List Display"
470   DATA "Keyboard Parameter Change","MIDI SW Matrix Change"
480   DATA "The  Parameter Set","K-1m Exclusive Set"
490   DATA "Multi Track Sequencer","Program Change Control"
500   DATA "Dummy"
510   DATA "Load Parameter File","Save Current Parameter/Setting"
520 *DATA2
530   DATA "                                    --------- ( Hit Any Key ! )"
540   DATA "   (1) [LT] RS232C -----> [BOX] RS232C Port : DSUB"
550   DATA "   (2) [Keyboard] Power Out -----> [BOX] Poewr In : MIDI-5"
560   DATA "   (3) [BOX] MIDI Out -----> [Keyboard] MIDI In (1) : MIDI"
570   DATA "   (4) [Keyboard] MIDI Out (1) -----> [BOX] MIDI In : MIDI"
575   DATA "   (5) [BOX] Line Out (R/L) -----> Mixer / Amp : LINE"
580   DATA "   (6) [Ext.KBD] MIDI Out -----> [Keyboard] MIDI In (2) : MIDI"
590   DATA "   (7) [Keyboard] MIDI Out (2) -----> [Ext.KBD] MIDI In : MIDI"
610   DATA "   (8) [Ext.KBD] Line Out (R/L) -----> Mixer / Amp : LINE"
620   DATA "   (9) [K-1m] MIDI Out -----> [Keyboard] MIDI In (2) : MIDI"
630   DATA "   (10) [Keyboard] MIDI Out (2) -----> [K-1m] MIDI In : MIDI"
640   DATA "   (11) [K-1m] Line Out (R/L) -----> Mixer / Amp : LINE"
650   DATA "   (12)"
660   DATA "   (13)"
670   DATA "   (14)"


10 'save "phase.bas",a
20 WEI=1.5:LIM=0:GOSUB 60
30 LIM=LIM+1:IF LIM>256*50 THEN 150
40 AAA=A(LIM MOD 256)+WEI*A((LIM+PH*INT(LIM/256)) MOD 256)
50 PSET (LIM MOD 2560,256+384*INT(LIM/2560)-INT(AAA/(1+WEI))):GOTO 30
60 CLS:INPUT "Input Phase Speed [0-255] --- ",PH
70 KEY OFF:SCREEN 1:DIM A(256)
80 WINDOW SCREEN (-9,-180)-(256*10+9,256*5+128*4+9):FOR I=0 TO 255
90 A(I)=INT(128+127.999*SIN(I*3.1415926535#/128)):NEXT I
100 FOR J=0 TO 4:FOR I=0 TO 256*9 STEP 256
110 LINE (I,J*384)-(I+256,J*384+256),1,B,&H1111:NEXT I
120 LINE (0,J*384)-(2560,J*384+256),1,B
130 LINE (0,J*384+128)-(2560,J*384+128),1,B,&H3333:NEXT J
140 LOCATE 1,40,0:PRINT "Phase = [";PH;"]";:RETURN
150 A$=INKEY$:IF A$<>" " THEN 150 ELSE SCREEN 0:KEY ON:LOCATE 1,1,1:END


10 'save "rain.bas",a
20  CT=20:KEY OFF:SCREEN 1:LOCATE 1,1,0
30  DIM R(3,20):FOR I=1 TO CT:R(1,I)=0:NEXT I
40  DIM RR(20):FOR I=1 TO 20:RR(I)=10*I:NEXT I
50 RANDOMIZE TIMER
60  FOR I=1 TO CT:IF R(1,I)=0 THEN 120
70   R(1,I)=R(1,I)+1:IF R(1,I)>20 THEN R(1,I)=0:GOTO 120
80   IF R(1,I)<6 THEN CIRCLE (R(2,I),R(3,I)),RR(R(1,I)),1:GOTO 120
90   IF R(1,I)>15 THEN CIRCLE (R(2,I),R(3,I)),RR(R(1,I)-5),0:GOTO 120
100   CIRCLE (R(2,I),R(3,I)),RR(R(1,I)),1
110   CIRCLE (R(2,I),R(3,I)),RR(R(1,I)-5),0
120  NEXT I
130  RND1=INT((CT-.01)*RND(1))+1:IF R(1,RND1)<>0 THEN 160
140   RND2=INT(1023.99*RND(2))+1:RND3=INT(767.99*RND(3))+1
150   R(1,RND1)=1:R(2,RND1)=RND2:R(3,RND1)=RND3:CIRCLE (RND2,RND3),10,1
160 IF INKEY$<>" " THEN 50 ELSE SCREEN 0:END


1000 'save "rnd",a
1010  DIM A(55,2)
1020  RANDOMIZE (33)
1030  FOR I=1 TO 55 : A(I,2)=0 : NEXT I
1040  OPEN "f:z.z" FOR OUTPUT AS #1
1050  FOR I=1 TO 55
1060  NO=INT(55*RND(1))+1
1070  IF A(NO,2)=1 THEN 1060
1080  A(NO,2)=1
1090  A$=HEX$(NO)
1100  PRINT A$;" ";
1110  PRINT #1,",0x"+A$
1120  NEXT I
1130  CLOSE #1


1000 'save "rnd2",a
1010  DIM A(256,2)
1020  RANDOMIZE (33)
1030  FOR I=0 TO 255 : A(I,2)=0 : NEXT I
1040  OPEN "f:z.z" FOR OUTPUT AS #1
1050  FOR I=0 TO 25
1060  NO=INT(255.99*RND(1))
1070  IF A(NO,2)=1 THEN 1060
1080  IF NO=26 THEN 1060
1090  A(NO,2)=1
1100  A$=HEX$(NO)
1110  PRINT A$;" ";
1120  PRINT #1,",0x"+A$
1130  NEXT I
1140  NO=26
1150  A(NO,2)=1
1160  A$=HEX$(NO)
1170  PRINT A$;" ";
1180  PRINT #1,",0x"+A$
1190 FOR I=27 TO 255
1200 NO=INT(255.99*RND(1))
1210 IF A(NO,2)=1 THEN 1200
1220 A(NO,2)=1
1230 A$=HEX$(NO)
1240  PRINT A$;" ";
1250  PRINT #1,",0x"+A$
1260  NEXT I
1270  CLOSE #1


10 'save "sine.bas",a
20  CLS:DIM X$(256):FOR I=0 TO 255:X$(I)=",":NEXT I
30  FOR I=0 TO 127
40  X=INT(127.999*SIN(I*3.141592*360/256/180))
45  X$(I)=STR$(X)
50  PRINT X$(I);" ";
60  NEXT I
130  FOR I=0 TO 127
140  X=128+INT(127.999*SIN(I*3.141592*360/256/180))
145  X$(I+128)=STR$(X)
150  PRINT X$(I+128);" ";
160  NEXT I
170 'OPEN "f:sine.dat" FOR OUTPUT AS #1
180 'FOR I=0 TO 255:PRINT #1,X$(I):NEXT I
210 'CLOSE #1:END

BASIC その17 簡易CAD

10 'save "tinycad",a
20  CLEAR ,&HC000:DEFINT I-N:DIM IC(33),DP(16)
30  LIM=200:IST=10:GR$="LINE":JX=330:JY=230:IUM=0:UM$="LIST File"
40  ISC=0:IDM=1:IGR=0
50  GOSUB 1580:GOSUB 1600
60  '### Main Menu ###
70  GOSUB 1530:GOSUB 1160:GOSUB 350
80  GOSUB 940:IPH=0
90  Z$=INKEY$:IF Z$="" THEN 90 ELSE IF LEN(Z$)=2 THEN Z$=RIGHT$(Z$,1)
100  IF Z$=CHR$(72) THEN GOSUB 300:GOTO 90
110  IF Z$=CHR$(80) THEN GOSUB 310:GOTO 90
120  IF Z$=CHR$(77) THEN GOSUB 320:GOTO 90
130  IF Z$=CHR$(75) THEN GOSUB 330:GOTO 90
140  IF Z$=CHR$(13) AND IPH=0 THEN GOSUB 1000:IPH=1:GOTO 90
150  IF Z$=CHR$(13) AND IPH=1 THEN ITI=1:GOSUB 1010:IPH=0:GOTO 90
160  IF Z$=" " AND ISC=0 THEN ISC=1:GOTO 80
170  IF Z$=" " AND ISC=1 THEN ISC=0:GOTO 80
180  IF Z$="R" OR Z$="r" THEN 70
190  IF Z$="P" OR Z$="p" THEN GOSUB 1500:GOTO 80
200  IF Z$="L" OR Z$="l" THEN GOSUB 1440:GOTO 80
210  IF Z$="S" OR Z$="s" THEN GOSUB 1320:GOTO 80
220  IF Z$="F" OR Z$="f" THEN GOSUB 1300:GOTO 80
230  IF Z$="M" OR Z$="m" THEN GOSUB 690:GOTO 70
240  IF Z$="U" OR Z$="u" THEN 550
250  IF (Z$="D" OR Z$="d") AND IDM=0 THEN IDM=1:GOTO 70
260  IF (Z$="D" OR Z$="d") AND IDM=1 THEN IDM=0:GOTO 70
270  IF Z$="E" OR Z$="e" THEN GOSUB 360:GOTO 70
280  IF Z$="C" OR Z$="c" THEN 1400
290  IF Z$=CHR$(27) THEN 1570 ELSE 90
300  IF JY<20+IST THEN RETURN ELSE PUT (JX,JY),IC:JY=JY-IST:GOTO 340
310  IF JY>490-IST THEN RETURN ELSE PUT (JX,JY),IC:JY=JY+IST:GOTO 340
320  IF JX>699-IST THEN RETURN ELSE PUT (JX,JY),IC:JX=JX+IST:GOTO 340
330  IF JX<10+IST THEN RETURN ELSE PUT (JX,JY),IC:JX=JX-IST:GOTO 340
340  GOSUB 350:LOCATE 1,70:PRINT JX;JY:RETURN
350  PUT (JX,JY),IC:RETURN
360  LOCATE 1,1:PRINT SPACE$(80);:LOCATE 1,1:PRINT "<< ERASE >>";
370  KER=LIM:PRINT "      [N]ext , [E]rase , [Q]uit  "
380  IF KER=0 THEN RETURN ELSE LINE (11,21)-(698,489),0,BF
390  ADD=&HC000+16*(KER-1):KDT=PEEK(ADD):IF KDT<>2 THEN 420
400  LX=PEEK(ADD+1):LY=PEEK(ADD+2):LMX=PEEK(ADD+3):MD$="":FOR LM=1 TO LMX
410  MD$=MD$+CHR$(PEEK(ADD+3+LM)):NEXT LM:LOCATE LY,LX:PRINT MD$;:GOTO 510
420  IF KDT<>1 THEN KER=KER-1:IF KER=0 THEN RETURN ELSE GOTO 390
430  IGR=PEEK(ADD+1):FOR IK=1 TO 8:DP(IK)=PEEK(ADD+IK+1):NEXT IK
440  IX1=256*DP(1)+DP(2):IY1=256*DP(3)+DP(4)
450  MX=256*DP(5)+DP(6):MY=256*DP(7)+DP(8):IR=256*DP(5)+DP(6)
460  IF IGR=0 THEN LINE (IX1,IY1)-(MX,MY),1:GOTO 510
470  IF IGR=1 THEN LINE (IX1,IY1)-(MX,MY),1,B:GOTO 510
480  IF IGR=2 THEN LINE (IX1,IY1)-(MX,MY),1,BF:GOTO 510
490  IF IGR=3 THEN CIRCLE (IX1,IY1),IR,1:GOTO 510
500  IF IGR=4 THEN LINE (MX-1,MY-1)-(MX+1,MY+1),1,BF:CIRCLE (MX,MY),2,1
510  B$=INKEY$:IF B$="" THEN 510
520  IF B$=" " OR B$="N" OR B$="n" THEN KER=KER-1:GOTO 380
530  IF B$=CHR$(27) OR B$="E" OR B$="e" THEN POKE ADD,0:KER=KER-1:GOTO 380
540  IF B$=CHR$(13) OR B$="Q" OR B$="q" THEN RETURN ELSE GOTO 510
550  LOCATE 1,1:PRINT SPACE$(80);:LOCATE 1,1:PRINT "<< Utilities >>";
560  LOCATE 1,20:PRINT "Select Mode  =  ";UM$;"  ";
570  B$=INKEY$:IF B$="" THEN 570
580  IF B$=CHR$(27) THEN 70
590  IF B$=CHR$(13) THEN 630 ELSE IUM=(IUM+1) MOD 4
600  IF IUM=0 THEN UM$="LIST File" ELSE IF IUM=1 THEN UM$="LOAD File"
610  IF IUM=2 THEN UM$="SAVE File" ELSE IF IUM=3 THEN UM$="KILL File"
620  GOTO 560
630  IF IUM=0 THEN 670 ELSE INPUT "   Name = ";UN$
640  IF IUM=2 THEN BSAVE UN$+".cad",&HC000,&H4000:GOTO 80
650  IF IUM=1 THEN BLOAD UN$+".cad",&HC000:GOTO 70
660  KILL UN$+".cad":GOTO 80
670  LINE (0,0)-(719,511),0,BF:LOCATE 4,1:FILES
680  B$=INKEY$:IF B$="" THEN 680 ELSE GOTO 70
690  GOSUB 350:LOCATE 1,1:PRINT SPACE$(80);:LOCATE 1,1
700  LOCATE 1,1,0:PRINT " [ Message Print ]  --- "
710  LOCATE 1,26:PRINT "Point Setting !!"
720  LX=INT((JX+4)/9):LY=INT((JY+10)/20):IF LX<3 THEN LX=3
730  IF LX>77 THEN LX=77 ELSE IF LY<3 THEN LY=3 ELSE IF LY>24 THEN LY=24
740  GOSUB 860
750  Y$=INKEY$:IF Y$="" THEN 750 ELSE IF LEN(Y$)=2 THEN Y$=RIGHT$(Y$,1)
760  IF Y$=CHR$(72) THEN GOSUB 810:GOTO 750
770  IF Y$=CHR$(80) THEN GOSUB 820:GOTO 750
780  IF Y$=CHR$(77) THEN GOSUB 830:GOTO 750
790  IF Y$=CHR$(75) THEN GOSUB 840:GOTO 750
800  IF Y$=CHR$(13) THEN 870
810  IF LY=3 THEN RETURN ELSE GOSUB 860:LY=LY-1:GOSUB 860:RETURN
820  IF LY=24 THEN RETURN ELSE GOSUB 860:LY=LY+1:GOSUB 860:RETURN
830  IF LX=77 THEN RETURN ELSE GOSUB 860:LX=LX+1:GOSUB 860:RETURN
840  IF LX=3 THEN RETURN ELSE GOSUB 860:LX=LX-1:GOSUB 860:RETURN
850  IF Y$=CHR$(27) THEN RETURN ELSE 750
860  PUT (9*LX-4,20*LY-10),IC:RETURN
870  LOCATE 1,26:PRINT "Input Data  =                "
880  LOCATE 1,40:INPUT MD$:IF LEN(MD$)>12 THEN BEEP:GOTO 870 ELSE ITI=1
890  ADD=&HC000+16*(ITI-1):KDT=PEEK(ADD):IF KDT=0 THEN 910
900  ITI=ITI+1:IF ITI>LIM THEN BEEP:RETURN ELSE 890
910  POKE ADD+1,LX:POKE ADD+2,LY:POKE ADD+3,LEN(MD$):FOR LM=1 TO LEN(MD$)
920  ME$=MID$(MD$,LM,1):AM=ASC(ME$):IF AM>255 THEN BEEP:GOTO 870
930  POKE ADD+3+LM,AM:NEXT LM:POKE ADD,2:RETURN
940  LOCATE 1,1:PRINT SPACE$(80);:IF ISC=1 THEN 970
950  LOCATE 1,1,0:PRINT "[R]edraw  [S]elect  ";
960  PRINT "[M]essage  [E]rase  [P]arameter";:GOTO 990
970  LOCATE 1,1,0:PRINT "[U]tility  [F]reeze  [D]otMesh  [L]imit  ";
980  PRINT "[C]lear"
990  LOCATE 1,70:PRINT JX;JY:RETURN
1000  IX1=JX:IY1=JY:RETURN
1010  ADD=&HC000+16*(ITI-1):KDT=PEEK(ADD):IF KDT=0 THEN 1030
1020  ITI=ITI+1:IF ITI>LIM THEN BEEP:RETURN ELSE 1010
1030  POKE ADD,1:POKE ADD+1,IGR:ON IGR+1 GOSUB 1040,1090,1100,1110,1150:RETURN
1040  LINE (IX1,IY1)-(JX,JY),1
1050  POKE ADD+2,INT(IX1/256):POKE ADD+3,(IX1 MOD 256)
1060  POKE ADD+4,INT(IY1/256):POKE ADD+5,(IY1 MOD 256)
1070  POKE ADD+6,INT(JX/256):POKE ADD+7,(JX MOD 256)
1080  POKE ADD+8,INT(JY/256):POKE ADD+9,(JY MOD 256):RETURN
1090  LINE (IX1,IY1)-(JX,JY),1,B:GOTO 1050
1100  LINE (IX1,IY1)-(JX,JY),1,BF:GOTO 1050
1110  IR=INT(SQR((IX1-JX)*(IX1-JX)+(IY1-JY)*(IY1-JY))):CIRCLE (IX1,IY1),IR,1
1120  POKE ADD+2,INT(IX1/256):POKE ADD+3,(IX1 MOD 256)
1130  POKE ADD+4,INT(IY1/256):POKE ADD+5,(IY1 MOD 256)
1140  POKE ADD+6,INT(IR/256):POKE ADD+7,(IR MOD 256):RETURN
1150  LINE (JX-1,JY-1)-(JX+1,JY+1),1,BF:CIRCLE (JX,JY),2,1:GOTO 1070
1160  FOR ITI=1 TO LIM:ADD=&HC000+16*(ITI-1):KDT=PEEK(ADD):IF KDT<>2 THEN 1190
1170  LX=PEEK(ADD+1):LY=PEEK(ADD+2):LMX=PEEK(ADD+3):MD$="":FOR LM=1 TO LMX
1180  MD$=MD$+CHR$(PEEK(ADD+3+LM)):NEXT LM:LOCATE LY,LX:PRINT MD$;
1190  NEXT ITI
1200  FOR ITI=1 TO LIM:ADD=&HC000+16*(ITI-1):KDT=PEEK(ADD):IF KDT<>1 THEN 1290
1210  IGR=PEEK(ADD+1):FOR IK=1 TO 8:DP(IK)=PEEK(ADD+IK+1):NEXT IK
1220  IX1=256*DP(1)+DP(2):IY1=256*DP(3)+DP(4)
1230  JX=256*DP(5)+DP(6):JY=256*DP(7)+DP(8):IR=256*DP(5)+DP(6)
1240  IF IGR=0 THEN LINE (IX1,IY1)-(JX,JY),1:GOTO 1290
1250  IF IGR=1 THEN LINE (IX1,IY1)-(JX,JY),1,B:GOTO 1290
1260  IF IGR=2 THEN LINE (IX1,IY1)-(JX,JY),1,BF:GOTO 1290
1270  IF IGR=3 THEN CIRCLE (IX1,IY1),IR,1:JX=IX1:JY=IY1:GOTO 1290
1280  IF IGR=4 THEN LINE (JX-1,JY-1)-(JX+1,JY+1),1,BF:CIRCLE (JX,JY),2,1
1290  NEXT ITI:IGR=0:GR$="LINE":RETURN
1300  LOCATE 1,1:PRINT SPACE$(80);:PUT (JX,JY),IC
1310  B$=INKEY$:IF B$="" THEN 1310 ELSE PUT (JX,JY),IC:RETURN
1320  LOCATE 1,1:PRINT SPACE$(80);:LOCATE 1,1:PRINT "<< Graph Select >>";
1330  LOCATE 1,26:PRINT "Select Graph = ";GR$;"       ";
1340  B$=INKEY$:IF B$="" THEN 1340
1350  IF B$=CHR$(13) THEN RETURN ELSE IGR=(IGR+1) MOD 5
1360  IF IGR=0 THEN GR$="LINE" ELSE IF IGR=1 THEN GR$="SQUARE"
1370  IF IGR=2 THEN GR$="BLOCK" ELSE IF IGR=3 THEN GR$="CIRCLE"
1380  IF IGR=4 THEN GR$="DOT"
1390  GOTO 1330
1400  LOCATE 1,1:PRINT SPACE$(80);:LOCATE 1,1:PRINT "### All Data Clear ###";
1410  LOCATE 1,30:PRINT "CAUTION !!!  ALL DATA DELETE OK? (Y/N)"
1420  B$=INKEY$:IF B$="" THEN 1420
1430  IF B$="Y" OR B$="y" THEN GOSUB 1590:GOTO 70 ELSE GOTO 80
1440  LOCATE 1,1:PRINT SPACE$(80);:LOCATE 1,1:PRINT "<< Data Count Limit >>";
1450  KEF=0:FOR ITI=1 TO LIM:ADD=&HC000+16*(ITI-1)
1460  IF PEEK(ADD)<>0 THEN KEF=KEF+1
1470  NEXT ITI:US=INT(100*KEF/LIM)
1480  LOCATE 1,30:PRINT "Using =";KEF;" (";US;"% )  , Limit =";LIM;:INPUT B$
1490  IF B$="" THEN RETURN ELSE LIM=VAL(B$):GOTO 1440
1500  LOCATE 1,1:PRINT SPACE$(80);:LOCATE 1,1:PRINT "<< Parameter Define >>";
1510  LOCATE 1,30:PRINT "ICON Step Speed =";IST;:INPUT B$
1520  IF B$="" THEN RETURN ELSE IST=VAL(B$):RETURN
1530  LINE (0,0)-(719,511),0,BF:LINE (10,20)-(699,490),1,B,&HCCCC
1540  IF IDM=0 THEN RETURN
1550  FOR IDX=20 TO 680 STEP 20:FOR IDY=40 TO 480 STEP 20
1560  PSET (IDX,IDY):NEXT IDY:NEXT IDX:RETURN
1570  COLOR 0,0,0:SCREEN 0,0,0:LOCATE 1,1,1:KEY ON:END:'####< END >####
1580  KEY OFF:SCREEN 2:LINE (0,480)-(100,511),0,BF:LOCATE ,,0
1590  FOR XI=&HC000 TO &HFFFF STEP 16:POKE XI,0:NEXT XI:RETURN
1600  RESTORE 1670:DEF SEG=&HA000:FOR LIN=0 TO 20
1610  ADD=(LIN MOD 2)*&H8000+128*INT(LIN/2):IDAT=0:FOR IBIT=0 TO 7:READ IDOT
1620  IDAT=IDAT+IDOT*2^(7-IBIT):NEXT IBIT:POKE ADD,IDAT:ADD=ADD+1:IDAT=0
1630  FOR IBIT=0 TO 7:READ IDOT:IDAT=IDAT+IDOT*2^(7-IBIT):NEXT IBIT
1640  POKE ADD,IDAT:ADD=ADD+1:IDAT=0:FOR IBIT=0 TO 4:READ IDOT
1650  IDAT=IDAT+IDOT*2^(7-IBIT):NEXT IBIT:POKE ADD,IDAT:NEXT LIN :DEF SEG
1660  GET (0,0)-(20,20),IC:LINE (0,0)-(20,20),0,BF:RETURN
1670  DATA 1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0:'<1>
1680  DATA 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0:'<2>
1690  DATA 0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0:'<3>
1700  DATA 0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0:'<4>
1710  DATA 0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0:'<5>
1720  DATA 0,1,0,0,0,1,1,1,1,0,0,0,1,1,1,0,0,0,0,0,0:'<6>
1730  DATA 0,1,0,0,0,1,1,0,0,1,0,1,0,0,0,1,0,0,0,0,0:'<7>
1740  DATA 0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0:'<8>
1750  DATA 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0:'<9>
1760  DATA 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0:'<10>
1770  DATA 0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0:'<11>
1780  DATA 0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0:'<12>
1790  DATA 0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0:'<13>
1800  DATA 0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0:'<14>
1810  DATA 0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0:'<15>
1820  DATA 0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,1,1,0,0,0:'<16>
1830  DATA 0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,1,1,0,0:'<17>
1840  DATA 0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,1,1,0:'<18>
1850  DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1:'<19>
1860  DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0:'<20>
1870  DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0:'<21>

BASIC その18 音源転送シミュレータ

10 'save "transim",a
20   DEFINT A-Z:DIM MT(256),MX(256):GOSUB 470:GOSUB 1090:GOSUB 930
30 '***** Main Loop *****
40   GOSUB 100:GOSUB 180:Z$=INKEY$:IF Z$="" THEN 30
50    IF Z$="f" OR Z$="F" THEN GOSUB 880
60    IF Z$="s" OR Z$="S" THEN GOSUB 810
70    IF Z$="l" OR Z$="L" THEN GOSUB 740
80    IF Z$="t" OR Z$="T" THEN GOSUB 530
90    IF Z$<>CHR$(&H1B) THEN 30 ELSE  SCREEN 0,0,0:LOCATE 1,1,1:KEY ON:END
100 '***** Mouse Point Check *****
110   AX%=3:GOSUB 1210:JJX=CX%:JJY=DX%:IF JJX=JX AND JJY=JY THEN RETURN
120    JX=JJX:JY=JJY
130    II=(JX-IXMIN)/ISX:JJ=(IYMAX-JY)/ISY
140    XX$=HEX$(II):IF LEN(XX$)=1 THEN XX$="0"+XX$
150    YY$=HEX$(JJ):IF LEN(YY$)=1 THEN YY$="0"+YY$
160    LOCATE 3,45:PRINT "Address = [";XX$;"] , Data = [";YY$;"] "
170   RETURN
180 '***** Plot Point Check *****
190   AX%=5:BX%=0:GOSUB 1210:IF BX%>0 THEN 210
200   AX%=5:BX%=1:GOSUB 1210:IF BX%=0 THEN RETURN
210    JX=CX%:JY=DX%
220    II=(JX-IXMIN)/ISX:JJ=(IYMAX-JY)/ISY
230   IF II=0 OR II=255 THEN MT(II)=JJ:GOTO 930
240    AX%=2:GOSUB 1210                         'HIDE POINT CURSOR
250    KX=IXMIN+II*ISX:KY=IYMAX-JJ*ISY
260   FOR I=II+1 TO 254
270     IF MT(I)<256 THEN 290
280    NEXT I
290    RX=IXMIN+I*ISX:RY=IYMAX-MT(I)*ISY
300   FOR I=II-1 TO 1 STEP -1
310     IF MT(I)<256 THEN 330
320    NEXT I
330    LX=IXMIN+I*ISX:LY=IYMAX-MT(I)*ISY
340  IF MT(II)<256 THEN 410 ELSE MT(II)=JJ
350   LINE (LX,LY)-(RX,RY),0
360    CIRCLE (KX,KY),2
370    PAINT (KX,KY)
380    LINE (LX,LY)-(KX,KY)
390    LINE (KX,KY)-(RX,RY)
400    GOTO 460
410   NY=IYMAX-MT(II)*ISY:MT(II)=999
420    LINE (LX,LY)-(KX,NY),0
430    LINE (KX,NY)-(RX,RY),0
440    LINE (KX-2,NY-2)-(KX+2,NY+2),0,BF
450    LINE (LX,LY)-(RX,RY)
460   AX%=1:GOSUB 1210:RETURN                  'SHOW POINT CURSOR
470 '***** Initialize Subroutine *****
480   KEY OFF:SCREEN 2:LOCATE ,,0
490    MT(0)=0:FOR I=1 TO 254:MT(I)=999:NEXT I:MT(255)=255
500    ISX=3:ISY=2:IXMIN=130:IXMAX=IXMIN+255*ISX:IYMIN=130:IYMAX=IYMIN+255*ISY
510    JX=IXMIN:JY=IYMAX
520   RETURN
530 '***** Transfer Subroutine *****
540    CLS:LOCATE 10,20:PRINT "W A I T    A    M O M E N T    ! ! !"
550    OPEN "trnsdmy.tns" FOR OUTPUT AS #1
560     FOR I=0 TO 255:PRINT #1,MT(I):NEXT I:CLOSE #1
570    KZ=1:III=0
580     IF MT(KZ)>255 THEN 620
590      FOR J=III TO KZ-1
600       MX(J)=INT(MT(III)+(J-III)*((MT(KZ)-MT(III))/(KZ-III)))
610      NEXT J:III=KZ
620     KZ=KZ+1:IF KZ<256 THEN 580 ELSE MX(255)=MT(255)
630    OPEN "trnsdmy.asc" FOR OUTPUT AS #2
640     FOR I=0 TO 255
650      O1$=HEX$(I):IF LEN(O1$)=1 THEN O1$="0"+O1$
660      O2$=HEX$(MX(I)):IF LEN(O2$)=1 THEN O2$="0"+O2$
670      PRINT #2,"TNS"+CHR$(&HD)+":"+O1$+":"+O2$+":";
680     NEXT I:CLOSE #2
690    SHELL "slowcopy.exe trnsdmy.asc"
700     KEY OFF:SCREEN 2:LOCATE ,,0
710    OPEN "trnsdmy.tns" FOR INPUT AS #1
720     FOR I=0 TO 255:INPUT #1,MT(I):NEXT I:CLOSE #1
730   CLS:GOSUB 930:RETURN
740 '***** Load Subroutine *****
750   CLS:PRINT :PRINT "     <<< Data File List Up >>>":PRINT :FILES "*.tns"
760    PRINT :INPUT "        Input Load File NO. (TRNS***) --- ",YD$
770    IF LEN(YD$)>3 THEN YD$=LEFT$(YD$,3)
780    OPEN "trns"+YD$+".tns" FOR INPUT AS #1
790    FOR I=0 TO 255:INPUT #1,MT(I):NEXT I:CLOSE #1
800   CLS:GOSUB 930:RETURN
810 '***** Save Subroutine *****
820   CLS:PRINT :PRINT "     <<< Data File List Up >>>":PRINT :FILES "*.tns"
830    PRINT :INPUT "        Input Save File NO. (TRNS***) --- ",YD$
840    IF LEN(YD$)>3 THEN YD$=LEFT$(YD$,3)
850    OPEN "trns"+YD$+".tns" FOR OUTPUT AS #1
860    FOR I=0 TO 255:PRINT #1,MT(I):NEXT I:CLOSE #1
870   CLS:GOSUB 930:RETURN
880 '***** Files Subroutine *****
890   CLS:PRINT :PRINT "     <<< Data File List Up >>>":PRINT :FILES "*.tns"
900    PRINT :PRINT "        (Hit Any Key !)";
910    IF INKEY$="" THEN 910
920   CLS:GOSUB 930:RETURN
930 '***** Screen Redraw *****
940   AX%=2:GOSUB 1210                        'HIDE POINT CURSOR
950   LINE (IXMIN-ISX,IYMIN-ISY)-(IXMAX+ISX+16,IYMAX+ISY+16),0,BF
960    LOCATE 1,3:PRINT "*****  TG Wave Conversion Simulate System *****"
970    LOCATE 24,30:PRINT "[F]iles  [L]oad  [S]ave  [T]ransfer";
980   LINE (IXMIN-ISX,IYMIN-ISY)-(IXMAX+ISX,IYMAX+ISY),1,B
990   FOR I=0 TO 255:IF MT(I)>255 THEN 1020
1000    CIRCLE (IXMIN+I*ISX,IYMAX-MT(I)*ISY),2
1010    PAINT (IXMIN+I*ISX,IYMAX-MT(I)*ISY)
1020   NEXT I:KX=IXMIN:KY=IYMAX-MT(0)*ISY:KZ=1
1030    IF MT(KZ)>255 THEN 1060
1040    LX=IXMIN+KZ*ISX:LY=IYMAX-MT(KZ)*ISY
1050    LINE (KX,KY)-(LX,LY):KX=LX:KY=LY
1060    KZ=KZ+1:IF KZ<256 THEN 1030
1070   AX%=1:GOSUB 1210                         'SHOW POINT CURSOR
1080   RETURN
1090 '***** Mouse Initialize *****
1100  DEF SEG=0
1110   MSEG=256*PEEK(51*4+3)+PEEK(51*4+2)      'GET INT33H VECTOR (SEG)
1120   MOUSE=256*PEEK(51*4+1)+PEEK(51*4)+2     'GET INT33H VECTOR (OFFSET)
1130  DEF SEG=MSEG
1140   AX%=30:BX%=3:GOSUB 1210                  'MOUSE MODE SET
1150   AX%=0:GOSUB 1210                         'RESET MOUSE
1160   AX%=15:CX%=8:DX%=8:GOSUB 1210            'SET MOUSE MOTION RATIO
1170   AX%=4:CX%=JX:DX%=JY:GOSUB 1210           'SET MOUSE CURSOR POSITION
1180   AX%=7:CX%=IXMIN:DX%=IXMAX:GOSUB 1210
1190   AX%=8:CX%=IYMIN:DX%=IYMAX:GOSUB 1210
1200  RETURN
1210  CALL MOUSE(AX%,BX%,CX%,DX%):RETURN

BASIC その19 音楽情報科学研究会発表関係

1000 'save "jmacs-1.bas",a
1010  GOTO 1080
1020  DATA 2.2,2.2,2.5,2.4,2.1,2.6,3.4,2.4,5.3,5.0,2.1,11,5.8,5.3,6.3
1030  DATA 6.2,5.2,7.5,6.3,5.0,8.0,6.4,5.4,7.2,6.7,5.2,8.0,7.8,6.5,10
1040  DATA 9.5,8.0,10.8,0.40,0.38,0.44,2.4,0.65,5.2,2.8,1.0,5.7,2.8,2.4,3.3
1050  DATA 3.4,2.8,3.8,4.3,3.25,5.25,6.5,6.4,7.0,6.6,4.2,9.2,2.1,1.9,2.2
1060  DATA 2.5,2.1,2.7,2.9,2.8,3.0,3.2,2.4,5.2,4.3,3.0,5.3,8.1,3,12
1070  DATA 10,9,11,20,16,22,20,15,25,22,17,25
1080  KEY OFF:SCREEN 1,0:LOCATE 1,20,0:PRINT "MIDI Instruments Delay Data"
1090  LOCATE 3,1:PRINT "NO.1":LOCATE 6,1:PRINT "   5"
1100  LOCATE 9,1:PRINT "  10":LOCATE 12,1:PRINT "  15"
1110  LOCATE 15,1:PRINT "  20":LOCATE 18,1:PRINT "  25"
1120  LOCATE 21,1:PRINT "  29":LOCATE 23,7
1130  FOR I=0 TO 5:LOCATE 23,5+13*I:PRINT I*5:NEXT I
1140  LOCATE 23,73:PRINT "(msec)";
1150  RESTORE 1020
1160  FOR I=1 TO 29:READ A,B,C
1170  CIRCLE (70+A*855/25,50+I*19),9,,,,6
1180  CIRCLE (70+A*855/25,50+I*19),7,,,,7
1190  CIRCLE (70+B*855/25,50+I*19),9,,,,5
1200  CIRCLE (70+C*855/25,50+I*19),9,,,,5
1210  LINE (70+B*855/25,50+I*19)-(70+C*855/25,50+I*19)
1220  NEXT I
1230  LINE (70,40)-(1023,625),,B
1240  IF INKEY$="" THEN 1240 ELSE SCREEN 0,0:LOCATE 1,1,1


1000 'save "jmacs-2.bas",a
1010 '===== Delay None , Amplitude Changes =====
1020  DATA 100,100,100,0,0,0,100,90,100,0,0,0,100,80,100,0,0,0
1030  DATA 100,70,100,0,0,0,100,60,100,0,0,0,100,100,90,0,0,0
1040  DATA 100,100,80,0,0,0,100,100,70,0,0,0,100,100,60,0,0,0
1050  DATA 100,90,80,0,0,0,100,80,60,0,0,0,100,70,50,0,0,0
1060  DATA 80,90,100,0,0,0,80,100,90,0,0,0,70,100,80,0,0,0
1070 '===== 3rd Delay Only Change =====
1080  DATA 100,100,100,0,1,0,100,100,100,0,2,0,100,100,100,0,3,0
1090  DATA 100,100,100,0,5,0,100,100,100,0,10,0,100,100,100,0,20,0
1100  DATA 100,90,80,0,1,0,100,90,80,0,2,0,100,90,80,0,3,0
1110  DATA 100,90,80,0,5,0,100,90,80,0,10,0,100,90,80,0,20,0
1120  DATA 100,80,60,0,1,0,100,80,60,0,2,0,100,80,60,0,3,0
1130  DATA 100,80,60,0,5,0,100,80,60,0,10,0,100,80,60,0,20,0
1140 '===== 5th Delay Only Change =====
1150  DATA 100,100,100,0,0,1,100,100,100,0,0,2,100,100,100,0,0,3
1160  DATA 100,100,100,0,0,5,100,100,100,0,0,10,100,100,100,0,0,20
1170  DATA 100,90,80,0,0,1,100,90,80,0,0,2,100,90,80,0,0,3
1180  DATA 100,90,80,0,0,5,100,90,80,0,0,10,100,90,80,0,0,20
1190  DATA 100,80,60,0,0,1,100,80,60,0,0,2,100,80,60,0,0,3
1200  DATA 100,80,60,0,0,5,100,80,60,0,0,10,100,80,60,0,0,20
1210 '===== 3rd/5th Delay Same Change =====
1220  DATA 100,100,100,0,1,1,100,100,100,0,2,2,100,100,100,0,3,3
1230  DATA 100,100,100,0,5,5,100,100,100,0,10,10,100,100,100,0,20,20
1240  DATA 100,90,80,0,1,1,100,90,80,0,2,2,100,90,80,0,3,3
1250  DATA 100,90,80,0,5,5,100,90,80,0,10,10,100,90,80,0,20,20
1260  DATA 100,80,60,0,1,1,100,80,60,0,2,2,100,80,60,0,3,3
1270  DATA 100,80,60,0,5,5,100,80,60,0,10,10,100,80,60,0,20,20
1280 '===== 3rd/5th Delay Different Change =====
1290  DATA 100,90,80,0,1,2,100,90,80,0,1,3,100,90,80,0,1,5
1300  DATA 100,90,80,0,1,10,100,90,80,0,1,15,100,90,80,0,1,20
1310  DATA 100,90,80,0,2,1,100,90,80,0,2,3,100,90,80,0,2,5
1320  DATA 100,90,80,0,2,10,100,90,80,0,2,15,100,90,80,0,2,20
1330  DATA 100,90,80,0,3,1,100,90,80,0,3,2,100,90,80,0,3,5
1340  DATA 100,90,80,0,3,10,100,90,80,0,3,15,100,90,80,0,3,20
1350  DATA 100,90,80,0,5,1,100,90,80,0,5,2,100,90,80,0,5,3
1360  DATA 100,90,80,0,5,10,100,90,80,0,5,15,100,90,80,0,5,20
1370  DATA 100,90,80,0,10,1,100,90,80,0,10,2,100,90,80,0,10,3
1380  DATA 100,90,80,0,10,5,100,90,80,0,10,15,100,90,80,0,10,20
1390  DATA 100,90,80,0,15,1,100,90,80,0,15,2,100,90,80,0,15,3
1400  DATA 100,90,80,0,15,5,100,90,80,0,15,10,100,90,80,0,15,20
1410  DATA 100,90,80,0,20,1,100,90,80,0,20,2,100,90,80,0,20,3
1420  DATA 100,90,80,0,20,5,100,90,80,0,20,10,100,90,80,0,20,15
1430  KEY OFF:SCREEN 1,0:LOCATE 1,1,0:DIM M(6,3):RESTORE 1020
1440  FOR LOOP=1 TO 37
1450   CLS:FOR J=1 TO 3: FOR I=1 TO 6:READ M(I,J):NEXT I:NEXT J
1460   FOR I=0 TO 2
1470    A=M(1,I+1):B=M(2,I+1):C=M(3,I+1):D=M(4,I+1):E=M(5,I+1):F=M(6,I+1)
1480    A$=RIGHT$(STR$(A),LEN(STR$(A))-1)
1490    B$=RIGHT$(STR$(B),LEN(STR$(B))-1)
1500    C$=RIGHT$(STR$(C),LEN(STR$(C))-1)
1510    LOCATE 8*I+1,5
1520    PRINT "1st(";A$;"%) =";D;"msec  ,  ";
1530    PRINT "3rd(";B$;"%) =";E;"msec  ,  ";
1540    PRINT "5th(";C$;"%) =";F;"msec";
1550    LINE (0,30+I*232)-(1023,210+I*232),,B
1560    LINE (0,120+I*232)-(1023,120+I*232),,,&H3333
1570    G=(A+B+C)/3:A=A*100/G:B=B*100/G:C=C*100/G
1580    XOLD=0:YOLD=120+I*232:ZPT=YOLD
1590    FOR J=0 TO 1022 STEP 2
1600     T=.05*J:X=J
1610     IF D";T#;"Hz",
1190   PRINT USING "+##.##############";V#;:PRINT " Cent"
1200   PRINT D$+"Data";H;"-->";U#;"Hz",
1210   PRINT USING "+##.##############";W#;:PRINT " Cent"
1220   PRINT #1,V#,W#
1230  NEXT I:CLOSE #1


1000 'save "jmacs-4.bas",a
1010  OPEN "pt200.dat" FOR OUTPUT AS #1
1020  CLS:X$="<< Intel [8254] Type : 16Bit Program Counter >>"
1030  Y$="       ( Master Clock = 1.8000 MHz )"
1040  PRINT X$:PRINT:PRINT Y$:PRINT:A$="A BbB C C#D EbE F F#G G#"
1050  H#=(2#^(1#/12#)):C#=1200#/LOG(2#)
1060  FOR I=1 TO 88
1070   J=(I-1) MOD 12
1080   K=INT((I+8)/12)
1090   B$=MID$(A$,2*J+1,2)
1100   IF I<10 THEN C$="Key " ELSE C$="Key"
1110   P#=CDBL(27.5*2^(INT((I-1)/12)))*H#^(CDBL(J))
1120   Q#=1800000#/P#:L=INT(Q#):H=L+1:R#=CDBL(L):S#=CDBL(H)
1130   T#=1800000#/R#:U#=1800000#/S#
1140   D$="      ":IF L<1000 THEN D$=D$+" "
1150   IF L<100 THEN D$=D$+" "
1160   V#=C#*LOG(T#/P#):W#=C#*LOG(U#/P#)
1170   PRINT "  "+C$;I;" [ "+B$;K;"] ";P#;"Hz"
1180   PRINT D$+"Data";L;"-->";T#;"Hz",
1190   PRINT USING "+##.##############";V#;:PRINT " Cent"
1200   PRINT D$+"Data";H;"-->";U#;"Hz",
1210   PRINT USING "+##.##############";W#;:PRINT " Cent"
1220   PRINT #1,V#,W#
1230  NEXT I:CLOSE #1


1000 'save "jmacs-5.bas",a
1010  OPEN "fm.dat" FOR OUTPUT AS #1
1020  CLS:X$="<< FM [YM3812] Type : 10Bit F-Number + 3Bit Octave >>"
1030  Y$="       ( Sampling Clock = 12.5kHz , 256 Word/Wave )"
1040  PRINT X$:PRINT:PRINT Y$:PRINT:A$="A BbB C C#D EbE F F#G G#"
1050  H#=(2#^(1#/12#)):C#=1200#/LOG(2#)
1060  FOR I=1 TO 88
1070   J=(I-1) MOD 12
1080   K=INT((I+8)/12)
1090   M=INT((I+7)/12)
1100   B$=MID$(A$,2*J+1,2)
1110   IF I<10 THEN C$="Key " ELSE C$="Key"
1120   P#=CDBL(27.5*2^(INT((I-1)/12)))*H#^(CDBL(J))
1130   Q#=P#*256#/12.5#/1000#*1024#/2#^CDBL(M)
1140   L=INT(Q#):H=L+1:R#=CDBL(L):S#=CDBL(H)
1150   T#=R#/256#*12.5#*1000#/1024#*2#^CDBL(M)
1160   U#=S#/256#*12.5#*1000#/1024#*2#^CDBL(M)
1170   D$="       "
1180   V#=C#*LOG(T#/P#):W#=C#*LOG(U#/P#)
1190   PRINT "  "+C$;I;" [ "+B$;K;"] ";P#;"Hz",:IF J=0 THEN PRINT "",
1200   PRINT "    ( Block =";M;")"
1210   PRINT D$+"Data";L;"-->";T#;"Hz",
1220   PRINT USING "+##.##############";V#;:PRINT " Cent"
1230   PRINT D$+"Data";H;"-->";U#;"Hz",
1240   PRINT USING "+##.##############";W#;:PRINT " Cent"
1250   PRINT #1,V#,W#
1260  NEXT I:CLOSE #1


1000 'save "jmacs-6.bas",a
1010  OPEN "skb.dat" FOR OUTPUT AS #1
1020  CLS:X$="<< PCM [KTG101] Type : 24Bit F-Number Accumulator >>"
1030  Y$="       ( Sampling Clock = 62.5kHz , 256 Word/Wave )"
1040  PRINT X$:PRINT:PRINT Y$:PRINT:A$="A BbB C C#D EbE F F#G G#"
1050  H#=(2#^(1#/12#)):C#=1200#/LOG(2#)
1060  FOR I=1 TO 88
1070   J=(I-1) MOD 12
1080   K=INT((I+8)/12)
1090   B$=MID$(A$,2*J+1,2)
1100   IF I<10 THEN C$="Key " ELSE C$="Key"
1110   P#=CDBL(27.5*2^(INT((I-1)/12)))*H#^(CDBL(J))
1120   Q#=P#*256#*16#/1000000#*65536#:L=INT(Q#):H=L+1:R#=CDBL(L):S#=CDBL(H)
1130   T#=R#/256#/16#*1000000#/65536#
1140   U#=S#/256#/16#*1000000#/65536#
1150   D$="   ":IF L<1000000! THEN D$=D$+" "
1160   IF L<100000! THEN D$=D$+" "
1170   IF L<10000 THEN D$=D$+" "
1180   V#=C#*LOG(T#/P#):W#=C#*LOG(U#/P#)
1190   PRINT "  "+C$;I;" [ "+B$;K;"] ";P#;"Hz"
1200   PRINT D$+"Data";L;"-->";T#;"Hz",
1210   PRINT USING "+##.##############";V#;:PRINT " Cent"
1220   PRINT D$+"Data";H;"-->";U#;"Hz",
1230   PRINT USING "+##.##############";W#;:PRINT " Cent"
1240   PRINT #1,V#,W#
1250  NEXT I:CLOSE #1


1000 'save "jmacs-7.bas",a
1010  A$="Pitch Error : <1> PSG [AY-3-8910]/[AY-3-8912] 12Bit Counter"
1020  OPEN "psg.dat" FOR INPUT AS #1
1030  KEY OFF:SCREEN 1,0:LOCATE 1,10,0:PRINT A$
1040  LOCATE 2,1:PRINT "(Cent)":LOCATE 3,1:PRINT "  +60"
1050  LOCATE 6,1:PRINT "  +40":LOCATE 9,1:PRINT "  +20"
1060  LOCATE 12,1:PRINT "    0":LOCATE 15,1:PRINT "  -20"
1070  LOCATE 18,1:PRINT "  -40":LOCATE 21,1:PRINT "  -60"
1080  LOCATE 23,1:PRINT "Key : 1     10";
1090  LOCATE 23,21:PRINT "20";:LOCATE 23,29:PRINT "30";
1100  LOCATE 23,37:PRINT "40";:LOCATE 23,45:PRINT "50";
1110  LOCATE 23,53:PRINT "60";:LOCATE 23,61:PRINT "70";
1120  LOCATE 23,69:PRINT "80";:LOCATE 23,76:PRINT "88";
1130  FOR I=1 TO 88:INPUT #1,X#,Y#
1140   X1=73+I*920/88
1150   Y1=333-X#*293/65
1160   Y2=333-Y#*293/65
1170   CIRCLE (X1,Y1),3,,,,.5
1180   CIRCLE (X1,Y2),3,,,,.5
1190   LINE (X1,Y1)-(X1,Y2)
1200  NEXT I
1210  LINE (75,333)-(1000,333),,,&H3333
1220  LINE (75,40)-(1000,625),,B
1230  IF INKEY$="" THEN 1230 ELSE SCREEN 0,0:LOCATE 1,1,1


1000 'save "jmacs-8.bas",a
1010  A$="Pitch Error : <2> Intel [8254] Type 16Bit Program Counter"
1020  OPEN "pt200.dat" FOR INPUT AS #1
1030  KEY OFF:SCREEN 1,0:LOCATE 1,10,0:PRINT A$
1040  LOCATE 2,1:PRINT "(Cent)":LOCATE 3,1:PRINT " +4.5"
1050  LOCATE 6,1:PRINT " +3.0":LOCATE 9,1:PRINT " +1.5"
1060  LOCATE 12,1:PRINT "    0":LOCATE 15,1:PRINT " -1.5"
1070  LOCATE 18,1:PRINT " -3.0":LOCATE 21,1:PRINT " -4.5"
1080  LOCATE 23,1:PRINT "Key : 1     10";
1090  LOCATE 23,21:PRINT "20";:LOCATE 23,29:PRINT "30";
1100  LOCATE 23,37:PRINT "40";:LOCATE 23,45:PRINT "50";
1110  LOCATE 23,53:PRINT "60";:LOCATE 23,61:PRINT "70";
1120  LOCATE 23,69:PRINT "80";:LOCATE 23,76:PRINT "88";
1130  FOR I=1 TO 88:INPUT #1,X#,Y#
1140   X1=73+I*920/88
1150   Y1=333-X#*293/4.7
1160   Y2=333-Y#*293/4.7
1170   CIRCLE (X1,Y1),3,,,,.5
1180   CIRCLE (X1,Y2),3,,,,.5
1190   LINE (X1,Y1)-(X1,Y2)
1200  NEXT I
1210  LINE (75,333)-(1000,333),,,&H3333
1220  LINE (75,40)-(1000,625),,B
1230  IF INKEY$="" THEN 1230 ELSE SCREEN 0,0:LOCATE 1,1,1


1000 'save "jmacs-9.bas",a
1010  A$="Pitch Error : <3> FM [YM3812] Type 10Bit F-Number + 3Bit Octave"
1020  OPEN "fm.dat" FOR INPUT AS #1
1030  KEY OFF:SCREEN 1,0:LOCATE 1,10,0:PRINT A$
1040  LOCATE 2,1:PRINT "(Cent)":LOCATE 3,1:PRINT " +4.5"
1050  LOCATE 6,1:PRINT " +3.0":LOCATE 9,1:PRINT " +1.5"
1060  LOCATE 12,1:PRINT "    0":LOCATE 15,1:PRINT " -1.5"
1070  LOCATE 18,1:PRINT " -3.0":LOCATE 21,1:PRINT " -4.5"
1080  LOCATE 23,1:PRINT "Key : 1     10";
1090  LOCATE 23,21:PRINT "20";:LOCATE 23,29:PRINT "30";
1100  LOCATE 23,37:PRINT "40";:LOCATE 23,45:PRINT "50";
1110  LOCATE 23,53:PRINT "60";:LOCATE 23,61:PRINT "70";
1120  LOCATE 23,69:PRINT "80";:LOCATE 23,76:PRINT "88";
1130  FOR I=1 TO 88:INPUT #1,X#,Y#
1140   X1=73+I*920/88
1150   Y1=333-X#*293/4.7
1160   Y2=333-Y#*293/4.7
1170   CIRCLE (X1,Y1),3,,,,.5
1180   CIRCLE (X1,Y2),3,,,,.5
1190   LINE (X1,Y1)-(X1,Y2)
1200  NEXT I
1210  LINE (75,333)-(1000,333),,,&H3333
1220  LINE (75,40)-(1000,625),,B
1230  IF INKEY$="" THEN 1230 ELSE SCREEN 0,0:LOCATE 1,1,1


1000 'save "jmacs-10.bas",a
1010  A$="Pitch Error : <4> PCM [KTG***] Type 24Bit F-Number "
1020  OPEN "skb.dat" FOR INPUT AS #1
1030  KEY OFF:SCREEN 1,0:LOCATE 1,10,0:PRINT A$
1040  LOCATE 2,1:PRINT "(Cent)":LOCATE 3,1:PRINT " +0.3"
1050  LOCATE 6,1:PRINT " +0.2":LOCATE 9,1:PRINT " +0.1"
1060  LOCATE 12,1:PRINT "    0":LOCATE 15,1:PRINT " -0.1"
1070  LOCATE 18,1:PRINT " -0.2":LOCATE 21,1:PRINT " -0.3"
1080  LOCATE 23,1:PRINT "Key : 1     10";
1090  LOCATE 23,21:PRINT "20";:LOCATE 23,29:PRINT "30";
1100  LOCATE 23,37:PRINT "40";:LOCATE 23,45:PRINT "50";
1110  LOCATE 23,53:PRINT "60";:LOCATE 23,61:PRINT "70";
1120  LOCATE 23,69:PRINT "80";:LOCATE 23,76:PRINT "88";
1130  FOR I=1 TO 88:INPUT #1,X#,Y#
1140   X1=73+I*920/88
1150   Y1=333-X#*293/.33
1160   Y2=333-Y#*293/.33
1170   CIRCLE (X1,Y1),3,,,,.5
1180   CIRCLE (X1,Y2),3,,,,.5
1190   LINE (X1,Y1)-(X1,Y2)
1200  NEXT I
1210  LINE (75,333)-(1000,333),,,&H3333
1220  LINE (75,40)-(1000,625),,B
1230  IF INKEY$="" THEN 1230 ELSE SCREEN 0,0:LOCATE 1,1,1


1000 'save "jmacs-11.bas",a
1010  DIM D#(12):D#(0)=0#:M#=1200#/LOG(2#)
1020  FLG=0
1030  N#=M#*LOG(CDBL(3^4/5/2^4))
1040  W#=M#*LOG(CDBL(3^12/2^19))
1050  CLS:A$="C C#D EbE F F#G AbA BbB "
1060  IF FLG=2 THEN OPEN "temp.dat" FOR OUTPUT AS #1
1070  GOTO 1190
1080  '***** Subroutine *****
1090   PRINT:PRINT:PRINT "<<< "+N$+" >>>":PRINT
1100   IF FLG=2 THEN PRINT #1,N$
1110   IF FLG=1 THEN LPRINT:LPRINT:LPRINT "<<< "+N$+" >>>":LPRINT
1120   FOR I=0 TO 11
1130    PRINT "     ["+MID$(A$,2*I+1,2)+"]    ";
1140    IF FLG=1 THEN LPRINT "     ["+MID$(A$,2*I+1,2)+"]    ";
1150    PRINT USING "####.##########";D#(I);:PRINT " cent"
1160    IF FLG=2 THEN PRINT #1,D#(I)
1170    IF FLG=1 THEN LPRINT USING "####.##########";D#(I);:LPRINT " cent"
1180   NEXT I:RETURN
1190  N$="[NO.1] 12 Equal Temperament"
1200    FOR I=1 TO 11
1210     D#(I)=CDBL(I)*100
1220    NEXT I
1230    GOSUB 1080
1240  N$="[NO.2] Pythagorean Scale ( 3#/2b )"
1250    D#(1)=M#*LOG(CDBL(3^7/2^11))
1260    D#(2)=M#*LOG(CDBL(3^2/2^3))
1270    D#(3)=M#*LOG(CDBL(2^5/3^3))
1280    D#(4)=M#*LOG(CDBL(3^4/2^6))
1290    D#(5)=M#*LOG(CDBL(2^2/3))
1300    D#(6)=M#*LOG(CDBL(3^6/2^9))
1310    D#(7)=M#*LOG(CDBL(3/2))
1320    D#(8)=M#*LOG(CDBL(3^8/2^12))
1330    D#(9)=M#*LOG(CDBL(3^3/2^4))
1340    D#(10)=M#*LOG(CDBL(2^4/3^2))
1350    D#(11)=M#*LOG(CDBL(3^5/2^7))
1360    GOSUB 1080
1370  N$="[NO.3] Pythagorean Scale ( 2#/3b )"
1380    D#(1)=M#*LOG(CDBL(3^7/2^11))
1390    D#(2)=M#*LOG(CDBL(3^2/2^3))
1400    D#(3)=M#*LOG(CDBL(2^5/3^3))
1410    D#(4)=M#*LOG(CDBL(3^4/2^6))
1420    D#(5)=M#*LOG(CDBL(2^2/3))
1430    D#(6)=M#*LOG(CDBL(3^6/2^9))
1440    D#(7)=M#*LOG(CDBL(3/2))
1450    D#(8)=M#*LOG(CDBL(2^7/3^4))
1460    D#(9)=M#*LOG(CDBL(3^3/2^4))
1470    D#(10)=M#*LOG(CDBL(2^4/3^2))
1480    D#(11)=M#*LOG(CDBL(3^5/2^7))
1490    GOSUB 1080
1500  N$="[NO.4] Pythagorean Scale ( 1#/4b )"
1510    D#(1)=M#*LOG(CDBL(2^8/3^5))
1520    D#(2)=M#*LOG(CDBL(3^2/2^3))
1530    D#(3)=M#*LOG(CDBL(2^5/3^3))
1540    D#(4)=M#*LOG(CDBL(3^4/2^6))
1550    D#(5)=M#*LOG(CDBL(2^2/3))
1560    D#(6)=M#*LOG(CDBL(3^6/2^9))
1570    D#(7)=M#*LOG(CDBL(3/2))
1580    D#(8)=M#*LOG(CDBL(2^7/3^4))
1590    D#(9)=M#*LOG(CDBL(3^3/2^4))
1600    D#(10)=M#*LOG(CDBL(2^4/3^2))
1610    D#(11)=M#*LOG(CDBL(3^5/2^7))
1620    GOSUB 1080
1630  N$="[NO.5] Pythagorean Scale ( 5b )"
1640    D#(1)=M#*LOG(CDBL(2^8/3^5))
1650    D#(2)=M#*LOG(CDBL(3^2/2^3))
1660    D#(3)=M#*LOG(CDBL(2^5/3^3))
1670    D#(4)=M#*LOG(CDBL(3^4/2^6))
1680    D#(5)=M#*LOG(CDBL(2^2/3))
1690    D#(6)=M#*LOG(CDBL(2^10/3^6))
1700    D#(7)=M#*LOG(CDBL(3/2))
1710    D#(8)=M#*LOG(CDBL(2^7/3^4))
1720    D#(9)=M#*LOG(CDBL(3^3/2^4))
1730    D#(10)=M#*LOG(CDBL(2^4/3^2))
1740    D#(11)=M#*LOG(CDBL(3^5/2^7))
1750    GOSUB 1080
1760  N$="[NO.6] Pythagorean Scale ( 4#/1b )"
1770    D#(1)=M#*LOG(CDBL(3^7/2^11))
1780    D#(2)=M#*LOG(CDBL(3^2/2^3))
1790    D#(3)=M#*LOG(CDBL(3^9/2^14))
1800    D#(4)=M#*LOG(CDBL(3^4/2^6))
1810    D#(5)=M#*LOG(CDBL(2^2/3))
1820    D#(6)=M#*LOG(CDBL(3^6/2^9))
1830    D#(7)=M#*LOG(CDBL(3/2))
1840    D#(8)=M#*LOG(CDBL(3^8/2^12))
1850    D#(9)=M#*LOG(CDBL(3^3/2^4))
1860    D#(10)=M#*LOG(CDBL(2^4/3^2))
1870    D#(11)=M#*LOG(CDBL(3^5/2^7))
1880    GOSUB 1080
1890  N$="[NO.7] Pythagorean Scale ( 5# )"
1900    D#(1)=M#*LOG(CDBL(3^7/2^11))
1910    D#(2)=M#*LOG(CDBL(3^2/2^3))
1920    D#(3)=M#*LOG(CDBL(3^9/2^14))
1930    D#(4)=M#*LOG(CDBL(3^4/2^6))
1940    D#(5)=M#*LOG(CDBL(2^2/3))
1950    D#(6)=M#*LOG(CDBL(3^6/2^9))
1960    D#(7)=M#*LOG(CDBL(3/2))
1970    D#(8)=M#*LOG(CDBL(3^8/2^12))
1980    D#(9)=M#*LOG(CDBL(3^3/2^4))
1990    D#(10)=M#*LOG(CDBL(3^10/2^15))
2000    D#(11)=M#*LOG(CDBL(3^5/2^7))
2010    GOSUB 1080
2020  N$="[NO.8] Meantone ( 3#/2b )"
2030    D#(1)=M#*LOG(CDBL(3^7/2^11))-N#*CDBL(7/4)
2040    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
2050    D#(3)=M#*LOG(CDBL(2^5/3^3))+N#*CDBL(3/4)
2060    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
2070    D#(5)=M#*LOG(CDBL(2^2/3))+N#*CDBL(1/4)
2080    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(3/2)
2090    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
2100    D#(8)=M#*LOG(CDBL(3^8/2^12))-N#*CDBL(2)
2110    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
2120    D#(10)=M#*LOG(CDBL(2^4/3^2))+N#*CDBL(1/2)
2130    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(5/4)
2140    GOSUB 1080
2150  N$="[NO.9] Meantone ( 2#/3b )"
2160    D#(1)=M#*LOG(CDBL(3^7/2^11))-N#*CDBL(7/4)
2170    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
2180    D#(3)=M#*LOG(CDBL(2^5/3^3))+N#*CDBL(3/4)
2190    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
2200    D#(5)=M#*LOG(CDBL(2^2/3))+N#*CDBL(1/4)
2210    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(3/2)
2220    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
2230    D#(8)=M#*LOG(CDBL(2^7/3^4))+N#*CDBL(1)
2240    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
2250    D#(10)=M#*LOG(CDBL(2^4/3^2))+N#*CDBL(1/2)
2260    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(5/4)
2270    GOSUB 1080
2280  N$="[NO.10] Meantone ( 1#/4b )"
2290    D#(1)=M#*LOG(CDBL(2^8/3^5))+N#*CDBL(5/4)
2300    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
2310    D#(3)=M#*LOG(CDBL(2^5/3^3))+N#*CDBL(3/4)
2320    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
2330    D#(5)=M#*LOG(CDBL(2^2/3))+N#*CDBL(1/4)
2340    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(3/2)
2350    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
2360    D#(8)=M#*LOG(CDBL(2^7/3^4))+N#*CDBL(1)
2370    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
2380    D#(10)=M#*LOG(CDBL(2^4/3^2))+N#*CDBL(1/2)
2390    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(5/4)
2400    GOSUB 1080
2410  N$="[NO.11] Meantone ( 5b )"
2420    D#(1)=M#*LOG(CDBL(2^8/3^5))+N#*CDBL(5/4)
2430    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
2440    D#(3)=M#*LOG(CDBL(2^5/3^3))+N#*CDBL(3/4)
2450    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
2460    D#(5)=M#*LOG(CDBL(2^2/3))+N#*CDBL(1/4)
2470    D#(6)=M#*LOG(CDBL(2^10/3^6))+N#*CDBL(3/2)
2480    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
2490    D#(8)=M#*LOG(CDBL(2^7/3^4))+N#*CDBL(1)
2500    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
2510    D#(10)=M#*LOG(CDBL(2^4/3^2))+N#*CDBL(1/2)
2520    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(5/4)
2530    GOSUB 1080
2540  N$="[NO.12] Meantone ( 4#/1b )"
2550    D#(1)=M#*LOG(CDBL(3^7/2^11))-N#*CDBL(7/4)
2560    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
2570    D#(3)=M#*LOG(CDBL(3^9/2^14))-N#*CDBL(9/4)
2580    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
2590    D#(5)=M#*LOG(CDBL(2^2/3))+N#*CDBL(1/4)
2600    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(3/2)
2610    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
2620    D#(8)=M#*LOG(CDBL(3^8/2^12))-N#*CDBL(2)
2630    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
2640    D#(10)=M#*LOG(CDBL(2^4/3^2))+N#*CDBL(1/2)
2650    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(5/4)
2660    GOSUB 1080
2670  N$="[NO.13] Meantone ( 5# )"
2680    D#(1)=M#*LOG(CDBL(3^7/2^11))-N#*CDBL(7/4)
2690    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
2700    D#(3)=M#*LOG(CDBL(3^9/2^14))-N#*CDBL(9/4)
2710    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
2720    D#(5)=M#*LOG(CDBL(2^2/3))+N#*CDBL(1/4)
2730    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(3/2)
2740    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
2750    D#(8)=M#*LOG(CDBL(3^8/2^12))-N#*CDBL(2)
2760    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
2770    D#(10)=M#*LOG(CDBL(3^10/2^15))-N#*CDBL(5/2)
2780    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(5/4)
2790    GOSUB 1080
2800  N$="[NO.14] Salinas ( 1/3 Syntonic Comma )"
2810    D#(1)=M#*LOG(CDBL(3^7/2^11))-N#*CDBL(7/3)
2820    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(2/3)
2830    D#(3)=M#*LOG(CDBL(2^5/3^3))+N#*CDBL(1)
2840    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(4/3)
2850    D#(5)=M#*LOG(CDBL(2^2/3))+N#*CDBL(1/3)
2860    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(2)
2870    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/3)
2880    D#(8)=M#*LOG(CDBL(3^8/2^12))-N#*CDBL(8/3)
2890    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(1)
2900    D#(10)=M#*LOG(CDBL(2^4/3^2))+N#*CDBL(2/3)
2910    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(5/3)
2920    GOSUB 1080
2930  N$="[NO.15] Verheijen-Rossi ( 1/5 Syntonic Comma )"
2940    D#(1)=M#*LOG(CDBL(3^7/2^11))-N#*CDBL(7/5)
2950    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(2/5)
2960    D#(3)=M#*LOG(CDBL(2^5/3^3))+N#*CDBL(3/5)
2970    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(4/5)
2980    D#(5)=M#*LOG(CDBL(2^2/3))+N#*CDBL(1/5)
2990    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(6/5)
3000    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/5)
3010    D#(8)=M#*LOG(CDBL(3^8/2^12))-N#*CDBL(8/5)
3020    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/5)
3030    D#(10)=M#*LOG(CDBL(2^4/3^2))+N#*CDBL(2/5)
3040    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(1)
3050    GOSUB 1080
3060  N$="[NO.16] Praetorius Meantone"
3070    D#(1)=M#*LOG(CDBL(3^7/2^11))-N#*CDBL(7/4)
3080    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
3090    D#(3)=M#*LOG(CDBL(2^5/3^3))+N#*CDBL(1/2)
3100    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
3110    D#(5)=M#*LOG(CDBL(2^2/3))+N#*CDBL(1/4)
3120    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(3/2)
3130    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
3140    D#(8)=M#*LOG(CDBL(3^8/2^12))-N#*CDBL(7/4)
3150    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
3160    D#(10)=M#*LOG(CDBL(2^4/3^2))+N#*CDBL(1/2)
3170    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(5/4)
3180    GOSUB 1080
3190  N$="[NO.17] Schnitger Meantone"
3200    D#(1)=M#*LOG(CDBL(3^7/2^11))-N#*CDBL(3/2)
3210    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
3220    D#(3)=M#*LOG(CDBL(2^5/3^3))+N#*CDBL(1/4)
3230    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
3240    D#(5)=M#*LOG(CDBL(2^2/3))+N#*CDBL(1/4)
3250    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(3/2)
3260    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
3270    D#(8)=M#*LOG(CDBL(3^8/2^12))-N#*CDBL(3/2)
3280    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
3290    D#(10)=M#*LOG(CDBL(2^4/3^2))+N#*CDBL(1/4)
3300    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(5/4)
3310    GOSUB 1080
3320  N$="[NO.18] Kirnberger <1>"
3330    D#(1)=M#*LOG(CDBL(2^8/3^5))
3340    D#(2)=M#*LOG(CDBL(3^2/2^3))
3350    D#(3)=M#*LOG(CDBL(2^5/3^3))
3360    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
3370    D#(5)=M#*LOG(CDBL(2^2/3))
3380    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(1)
3390    D#(7)=M#*LOG(CDBL(3/2))
3400    D#(8)=M#*LOG(CDBL(2^7/3^4))
3410    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(1)
3420    D#(10)=M#*LOG(CDBL(2^4/3^2))
3430    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(1)
3440    GOSUB 1080
3450  N$="[NO.19] Kirnberger <2>"
3460    D#(1)=M#*LOG(CDBL(2^8/3^5))
3470    D#(2)=M#*LOG(CDBL(3^2/2^3))
3480    D#(3)=M#*LOG(CDBL(2^5/3^3))
3490    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
3500    D#(5)=M#*LOG(CDBL(2^2/3))
3510    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(1)
3520    D#(7)=M#*LOG(CDBL(3/2))
3530    D#(8)=M#*LOG(CDBL(2^7/3^4))
3540    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(1/2)
3550    D#(10)=M#*LOG(CDBL(2^4/3^2))
3560    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(1)
3570    GOSUB 1080
3580  N$="[NO.20] Kirnberger <3>"
3590    D#(1)=M#*LOG(CDBL(2^8/3^5))
3600    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
3610    D#(3)=M#*LOG(CDBL(2^5/3^3))
3620    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
3630    D#(5)=M#*LOG(CDBL(2^2/3))
3640    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(1)
3650    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
3660    D#(8)=M#*LOG(CDBL(2^7/3^4))
3670    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
3680    D#(10)=M#*LOG(CDBL(2^4/3^2))
3690    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(1)
3700    GOSUB 1080
3710  N$="[NO.21] Werckmeister <1>"
3720    D#(1)=M#*LOG(CDBL(2^8/3^5))
3730    D#(2)=M#*LOG(CDBL(3^2/2^3))-W#*CDBL(1/2)
3740    D#(3)=M#*LOG(CDBL(2^5/3^3))
3750    D#(4)=M#*LOG(CDBL(3^4/2^6))-W#*CDBL(3/4)
3760    D#(5)=M#*LOG(CDBL(2^2/3))
3770    D#(6)=M#*LOG(CDBL(2^10/3^6))
3780    D#(7)=M#*LOG(CDBL(3/2))-W#*CDBL(1/4)
3790    D#(8)=M#*LOG(CDBL(2^7/3^4))
3800    D#(9)=M#*LOG(CDBL(3^3/2^4))-W#*CDBL(3/4)
3810    D#(10)=M#*LOG(CDBL(2^4/3^2))
3820    D#(11)=M#*LOG(CDBL(3^5/2^7))-W#*CDBL(3/4)
3830    GOSUB 1080
3840  N$="[NO.22] Werckmeister <2>"
3850    D#(1)=M#*LOG(CDBL(3^7/2^11))-W#*CDBL(4/3)
3860    D#(2)=M#*LOG(CDBL(3^2/2^3))-W#*CDBL(1/3)
3870    D#(3)=M#*LOG(CDBL(2^5/3^3))
3880    D#(4)=M#*LOG(CDBL(3^4/2^6))-W#*CDBL(2/3)
3890    D#(5)=M#*LOG(CDBL(2^2/3))
3900    D#(6)=M#*LOG(CDBL(3^6/2^9))-W#*CDBL(1)
3910    D#(7)=M#*LOG(CDBL(3/2))-W#*CDBL(1/3)
3920    D#(8)=M#*LOG(CDBL(3^8/2^12))-W#*CDBL(4/3)
3930    D#(9)=M#*LOG(CDBL(3^3/2^4))-W#*CDBL(2/3)
3940    D#(10)=M#*LOG(CDBL(2^4/3^2))+W#*CDBL(1/3)
3950    D#(11)=M#*LOG(CDBL(3^5/2^7))-W#*CDBL(1)
3960    GOSUB 1080
3970  N$="[NO.23] Werckmeister <3>"
3980    D#(1)=M#*LOG(CDBL(3^7/2^11))-W#*CDBL(3/4)
3990    D#(2)=M#*LOG(CDBL(3^2/2^3))
4000    D#(3)=M#*LOG(CDBL(2^5/3^3))+W#*CDBL(1/4)
4010    D#(4)=M#*LOG(CDBL(3^4/2^6))-W#*CDBL(1/2)
4020    D#(5)=M#*LOG(CDBL(2^2/3))+W#*CDBL(1/4)
4030    D#(6)=M#*LOG(CDBL(3^6/2^9))-W#*CDBL(1/2)
4040    D#(7)=M#*LOG(CDBL(3/2))
4050    D#(8)=M#*LOG(CDBL(3^8/2^12))-W#*CDBL(1)
4060    D#(9)=M#*LOG(CDBL(3^3/2^4))-W#*CDBL(1/4)
4070    D#(10)=M#*LOG(CDBL(2^4/3^2))+W#*CDBL(1/4)
4080    D#(11)=M#*LOG(CDBL(3^5/2^7))-W#*CDBL(1/2)
4090    GOSUB 1080
4100  N$="[NO.24] Kirnberger - Werckmeistar"
4110    D#(1)=M#*LOG(CDBL(2^8/3^5))
4120    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
4130    D#(3)=M#*LOG(CDBL(2^5/3^3))
4140    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(3/4)
4150    D#(5)=M#*LOG(CDBL(2^2/3))
4160    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(1)
4170    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
4180    D#(8)=M#*LOG(CDBL(2^7/3^4))
4190    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
4200    D#(10)=M#*LOG(CDBL(2^4/3^2))
4210    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(3/4)
4220    GOSUB 1080
4230  N$="[NO.25] Rameau-Legros Meantone"
4240    D#(1)=M#*LOG(CDBL(3^7/2^11))-N#*CDBL(5/4)
4250    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
4260    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
4270    D#(5)=M#*LOG(CDBL(2^2/3))+N#*CDBL(1/4)
4280    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(5/4)
4290    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
4300    D#(8)=M#*LOG(CDBL(3^8/2^12))-N#*CDBL(5/4)
4310    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
4320    D#(10)=M#*LOG(CDBL(2^4/3^2))+N#*CDBL(1/2)
4330    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(5/4)
4340    D#(3)=(D#(10)+D#(8)-1200#)/2#
4350    GOSUB 1080
4360  N$="[NO.26] Vogel-III Meantone"
4370    D#(1)=M#*LOG(CDBL(3^7/2^11))-N#*CDBL(3/2)
4380    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
4390    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
4400    D#(5)=M#*LOG(CDBL(2^2/3))
4410    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(3/2)
4420    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
4430    D#(8)=M#*LOG(CDBL(3^8/2^12))-N#*CDBL(3/2)
4440    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
4450    D#(10)=M#*LOG(CDBL(2^4/3^2))
4460    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(5/4)
4470    D#(3)=(D#(10)+D#(8)-1200#)/2#
4480    GOSUB 1080
4490  N$="[NO.27] Bruder Well-Tempered"
4500    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
4510    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
4520    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
4530    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(1)
4540    D#(5)=(M#*LOG(CDBL(2*5)))/8#
4550    D#(10)=D#(5)*2#
4560    D#(3)=D#(5)*3#-CDBL(1200*1)
4570    D#(8)=D#(5)*4#-CDBL(1200*1)
4580    D#(1)=D#(5)*5#-CDBL(1200*2)
4590    D#(6)=D#(5)*6#-CDBL(1200*2)
4600    D#(11)=D#(5)*7#-CDBL(1200*2)
4610    GOSUB 1080
4620  N$="[NO.28] Bruder - Werckmeister"
4630    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/4)
4640    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/2)
4650    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(3/4)
4660    D#(5)=(M#*LOG(CDBL(2*5)))/8#
4670    D#(10)=D#(5)*2#
4680    D#(3)=D#(5)*3#-CDBL(1200*1)
4690    D#(8)=D#(5)*4#-CDBL(1200*1)
4700    D#(1)=D#(5)*5#-CDBL(1200*2)
4710    D#(6)=D#(5)*6#-CDBL(1200*2)
4720    D#(11)=D#(6)-D#(7)+1200#
4730    D#(4)=D#(9)-D#(5)
4740    GOSUB 1080
4750  N$="[NO.29] Gottfried ( 1/6 Syntonic Comma )"
4760    D#(1)=M#*LOG(CDBL(3^7/2^11))-N#*CDBL(7/6)
4770    D#(2)=M#*LOG(CDBL(3^2/2^3))-N#*CDBL(1/3)
4780    D#(3)=M#*LOG(CDBL(2^5/3^3))+N#*CDBL(1/2)
4790    D#(4)=M#*LOG(CDBL(3^4/2^6))-N#*CDBL(2/3)
4800    D#(5)=M#*LOG(CDBL(2^2/3))+N#*CDBL(1/6)
4810    D#(6)=M#*LOG(CDBL(3^6/2^9))-N#*CDBL(1)
4820    D#(7)=M#*LOG(CDBL(3/2))-N#*CDBL(1/6)
4830    D#(8)=M#*LOG(CDBL(3^8/2^12))-N#*CDBL(4/3)
4840    D#(9)=M#*LOG(CDBL(3^3/2^4))-N#*CDBL(1/2)
4850    D#(10)=M#*LOG(CDBL(2^4/3^2))+N#*CDBL(1/3)
4860    D#(11)=M#*LOG(CDBL(3^5/2^7))-N#*CDBL(5/6)
4870    GOSUB 1080
4880  N$="[NO.30] Gottfried ( 1/6 Pythagorean Comma )"
4890    D#(1)=M#*LOG(CDBL(3^7/2^11))-W#*CDBL(7/6)
4900    D#(2)=M#*LOG(CDBL(3^2/2^3))-W#*CDBL(1/3)
4910    D#(3)=M#*LOG(CDBL(2^5/3^3))+W#*CDBL(1/2)
4920    D#(4)=M#*LOG(CDBL(3^4/2^6))-W#*CDBL(2/3)
4930    D#(5)=M#*LOG(CDBL(2^2/3))+W#*CDBL(1/6)
4940    D#(6)=M#*LOG(CDBL(3^6/2^9))-W#*CDBL(1)
4950    D#(7)=M#*LOG(CDBL(3/2))-W#*CDBL(1/6)
4960    D#(8)=M#*LOG(CDBL(3^8/2^12))-W#*CDBL(4/3)
4970    D#(9)=M#*LOG(CDBL(3^3/2^4))-W#*CDBL(1/2)
4980    D#(10)=M#*LOG(CDBL(2^4/3^2))+W#*CDBL(1/3)
4990    D#(11)=M#*LOG(CDBL(3^5/2^7))-W#*CDBL(5/6)
5000    GOSUB 1080
5010  N$="[NO.31] Marin Mersenne : Pure Temperament"
5020    D#(1)=M#*LOG(CDBL(25/24))
5030    D#(2)=M#*LOG(CDBL(9/8))
5040    D#(3)=M#*LOG(CDBL(75/64))
5050    D#(4)=M#*LOG(CDBL(5/4))
5060    D#(5)=M#*LOG(CDBL(4/3))
5070    D#(6)=M#*LOG(CDBL(25/18))
5080    D#(7)=M#*LOG(CDBL(3/2))
5090    D#(8)=M#*LOG(CDBL(25/16))
5100    D#(9)=M#*LOG(CDBL(5/3))
5110    D#(10)=M#*LOG(CDBL(16/9))
5120    D#(11)=M#*LOG(CDBL(15/8))
5130    GOSUB 1080
5140  N$="[NO.32] Pure  Temperament (Q-39)"
5150    D#(1)=M#*LOG(CDBL(5/4*5/4*2/3))
5160    D#(2)=M#*LOG(CDBL(5/4*2/3*2/3*2))
5170    D#(3)=M#*LOG(CDBL(2/3*2/3*2/3*4))
5180    D#(4)=M#*LOG(CDBL(5/4))
5190    D#(5)=M#*LOG(CDBL(2/3*2))
5200    D#(6)=M#*LOG(CDBL(5/4*5/4*2/3*2/3*2))
5210    D#(7)=M#*LOG(CDBL(5/4*2/3*2/3*2/3*4))
5220    D#(8)=M#*LOG(CDBL(5/4*5/4))
5230    D#(9)=M#*LOG(CDBL(5/4*2/3*2))
5240    D#(10)=M#*LOG(CDBL(2/3*2/3*4))
5250    D#(11)=M#*LOG(CDBL(5/4*5/4*2/3*2/3*2/3*4))
5260    GOSUB 1080
5270  N$="[NO.33] Pure  Temperament (Q-40)"
5280    D#(1)=M#*LOG(CDBL(5/4*5/4*2/3))
5290    D#(2)=M#*LOG(CDBL(5/4*2/3*2/3*2))
5300    D#(3)=M#*LOG(CDBL(3/2*5/4*5/4/2))
5310    D#(4)=M#*LOG(CDBL(5/4))
5320    D#(5)=M#*LOG(CDBL(2/3*2))
5330    D#(6)=M#*LOG(CDBL(5/4*5/4*2/3*2/3*2))
5340    D#(7)=M#*LOG(CDBL(3/2))
5350    D#(8)=M#*LOG(CDBL(5/4*5/4))
5360    D#(9)=M#*LOG(CDBL(5/4*2/3*2))
5370    D#(10)=M#*LOG(CDBL(2/3*2/3*4))
5380    D#(11)=M#*LOG(CDBL(3/2*5/4))
5390    GOSUB 1080
5400  N$="[NO.34] Pure  Temperament (Q-41)"
5410    D#(1)=M#*LOG(CDBL(5/4*5/4*2/3))
5420    D#(2)=M#*LOG(CDBL(3/2*3/2/2))
5430    D#(3)=M#*LOG(CDBL(3/2*5/4*5/4/2))
5440    D#(4)=M#*LOG(CDBL(5/4))
5450    D#(5)=M#*LOG(CDBL(2/3*2))
5460    D#(6)=M#*LOG(CDBL(3/2*3/2*5/4/2))
5470    D#(7)=M#*LOG(CDBL(3/2))
5480    D#(8)=M#*LOG(CDBL(5/4*5/4))
5490    D#(9)=M#*LOG(CDBL(5/4*2/3*2))
5500    D#(10)=M#*LOG(CDBL(3/2*3/2*5/4*5/4/2))
5510    D#(11)=M#*LOG(CDBL(3/2*5/4))
5520    GOSUB 1080
5530  N$="[NO.35] Pure  Temperament (Q-42)"
5540    D#(1)=M#*LOG(CDBL(3/2*3/2*3/2*5/4/4))
5550    D#(2)=M#*LOG(CDBL(3/2*3/2/2))
5560    D#(3)=M#*LOG(CDBL(3/2*5/4*5/4/2))
5570    D#(4)=M#*LOG(CDBL(5/4))
5580    D#(5)=M#*LOG(CDBL(3/2*3/2*3/2*5/4*5/4/4))
5590    D#(6)=M#*LOG(CDBL(3/2*3/2*5/4/2))
5600    D#(7)=M#*LOG(CDBL(3/2))
5610    D#(8)=M#*LOG(CDBL(5/4*5/4))
5620    D#(9)=M#*LOG(CDBL(3/2*3/2*3/2/2))
5630    D#(10)=M#*LOG(CDBL(3/2*3/2*5/4*5/4/2))
5640    D#(11)=M#*LOG(CDBL(3/2*5/4))
5650    GOSUB 1080
5660  N$="[NO.36] Pure  Temperament (Q-48)"
5670    D#(1)=M#*LOG(CDBL(4/5*2/3*2))
5680    D#(2)=M#*LOG(CDBL(5/4*2/3*2/3*2))
5690    D#(3)=M#*LOG(CDBL(2/3*2/3*2/3*4))
5700    D#(4)=M#*LOG(CDBL(5/4))
5710    D#(5)=M#*LOG(CDBL(2/3*2))
5720    D#(6)=M#*LOG(CDBL(4/5*2/3*2/3*4))
5730    D#(7)=M#*LOG(CDBL(5/4*2/3*2/3*2/3*4))
5740    D#(8)=M#*LOG(CDBL(4/5*2))
5750    D#(9)=M#*LOG(CDBL(5/4*2/3*2))
5760    D#(10)=M#*LOG(CDBL(2/3*2/3*4))
5770    D#(11)=M#*LOG(CDBL(4/5*2/3*2/3*2/3*8))
5780    GOSUB 1080
5790  N$="[NO.37] Pure  Temperament (Q-49)"
5800    D#(1)=M#*LOG(CDBL(4/5*2/3*2))
5810    D#(2)=M#*LOG(CDBL(5/4*2/3*2/3*2))
5820    D#(3)=M#*LOG(CDBL(3/2*4/5))
5830    D#(4)=M#*LOG(CDBL(5/4))
5840    D#(5)=M#*LOG(CDBL(2/3*2))
5850    D#(6)=M#*LOG(CDBL(4/5*2/3*2/3*4))
5860    D#(7)=M#*LOG(CDBL(3/2))
5870    D#(8)=M#*LOG(CDBL(4/5*2))
5880    D#(9)=M#*LOG(CDBL(5/4*2/3*2))
5890    D#(10)=M#*LOG(CDBL(2/3*2/3*4))
5900    D#(11)=M#*LOG(CDBL(3/2*5/4))
5910    GOSUB 1080
5920  N$="[NO.38] Pure  Temperament (Q-50)"
5930    D#(1)=M#*LOG(CDBL(4/5*2/3*2))
5940    D#(2)=M#*LOG(CDBL(3/2*3/2/2))
5950    D#(3)=M#*LOG(CDBL(3/2*4/5))
5960    D#(4)=M#*LOG(CDBL(5/4))
5970    D#(5)=M#*LOG(CDBL(2/3*2))
5980    D#(6)=M#*LOG(CDBL(3/2*3/2*5/4/2))
5990    D#(7)=M#*LOG(CDBL(3/2))
6000    D#(8)=M#*LOG(CDBL(4/5*2))
6010    D#(9)=M#*LOG(CDBL(5/4*2/3*2))
6020    D#(10)=M#*LOG(CDBL(3/2*3/2*4/5))
6030    D#(11)=M#*LOG(CDBL(3/2*5/4))
6040    GOSUB 1080
6050  N$="[NO.39] Pure  Temperament (Q-51)"
6060    D#(1)=M#*LOG(CDBL(3/2*3/2*3/2*5/4/4))
6070    D#(2)=M#*LOG(CDBL(3/2*3/2/2))
6080    D#(3)=M#*LOG(CDBL(3/2*4/5))
6090    D#(4)=M#*LOG(CDBL(5/4))
6100    D#(5)=M#*LOG(CDBL(3/2*3/2*3/2*4/5/2))
6110    D#(6)=M#*LOG(CDBL(3/2*3/2*5/4/2))
6120    D#(7)=M#*LOG(CDBL(3/2))
6130    D#(8)=M#*LOG(CDBL(4/5*2))
6140    D#(9)=M#*LOG(CDBL(3/2*3/2*3/2/2))
6150    D#(10)=M#*LOG(CDBL(3/2*3/2*4/5))
6160    D#(11)=M#*LOG(CDBL(3/2*5/4))
6170    GOSUB 1080
6180  N$="[NO.40] Pure  Temperament (Q-57)"
6190    D#(1)=M#*LOG(CDBL(4/5*2/3*2))
6200    D#(2)=M#*LOG(CDBL(4/5*4/5*2/3*2/3*4))
6210    D#(3)=M#*LOG(CDBL(2/3*2/3*2/3*4))
6220    D#(4)=M#*LOG(CDBL(4/5*4/5*2))
6230    D#(5)=M#*LOG(CDBL(2/3*2))
6240    D#(6)=M#*LOG(CDBL(4/5*2/3*2/3*4))
6250    D#(7)=M#*LOG(CDBL(4/5*4/5*2/3*2/3*2/3*8))
6260    D#(8)=M#*LOG(CDBL(4/5*2))
6270    D#(9)=M#*LOG(CDBL(4/5*4/5*2/3*4))
6280    D#(10)=M#*LOG(CDBL(2/3*2/3*4))
6290    D#(11)=M#*LOG(CDBL(4/5*2/3*2/3*2/3*8))
6300    GOSUB 1080
6310  N$="[NO.41] Pure  Temperament (Q-58)"
6320    D#(1)=M#*LOG(CDBL(4/5*2/3*2))
6330    D#(2)=M#*LOG(CDBL(4/5*4/5*2/3*2/3*4))
6340    D#(3)=M#*LOG(CDBL(3/2*4/5))
6350    D#(4)=M#*LOG(CDBL(4/5*4/5*2))
6360    D#(5)=M#*LOG(CDBL(2/3*2))
6370    D#(6)=M#*LOG(CDBL(4/5*2/3*2/3*4))
6380    D#(7)=M#*LOG(CDBL(3/2))
6390    D#(8)=M#*LOG(CDBL(4/5*2))
6400    D#(9)=M#*LOG(CDBL(4/5*4/5*2/3*4))
6410    D#(10)=M#*LOG(CDBL(2/3*2/3*4))
6420    D#(11)=M#*LOG(CDBL(3/2*4/5*4/5*2))
6430    GOSUB 1080
6440  N$="[NO.42] Pure  Temperament (Q-59)"
6450    D#(1)=M#*LOG(CDBL(4/5*2/3*2))
6460    D#(2)=M#*LOG(CDBL(3/2*3/2/2))
6470    D#(3)=M#*LOG(CDBL(3/2*4/5))
6480    D#(4)=M#*LOG(CDBL(4/5*4/5*2))
6490    D#(5)=M#*LOG(CDBL(2/3*2))
6500    D#(6)=M#*LOG(CDBL(3/2*3/2*4/5*4/5))
6510    D#(7)=M#*LOG(CDBL(3/2))
6520    D#(8)=M#*LOG(CDBL(4/5*2))
6530    D#(9)=M#*LOG(CDBL(4/5*4/5*2/3*4))
6540    D#(10)=M#*LOG(CDBL(3/2*3/2*4/5))
6550    D#(11)=M#*LOG(CDBL(3/2*4/5*4/5*2))
6560    GOSUB 1080
6570  N$="[NO.43] Pure  Temperament (Q-60)"
6580    D#(1)=M#*LOG(CDBL(3/2*3/2*3/2*4/5*4/5/2))
6590    D#(2)=M#*LOG(CDBL(3/2*3/2/2))
6600    D#(3)=M#*LOG(CDBL(3/2*4/5))
6610    D#(4)=M#*LOG(CDBL(4/5*4/5*2))
6620    D#(5)=M#*LOG(CDBL(3/2*3/2*3/2*4/5/2))
6630    D#(6)=M#*LOG(CDBL(3/2*3/2*4/5*4/5))
6640    D#(7)=M#*LOG(CDBL(3/2))
6650    D#(8)=M#*LOG(CDBL(4/5*2))
6660    D#(9)=M#*LOG(CDBL(3/2*3/2*3/2/2))
6670    D#(10)=M#*LOG(CDBL(3/2*3/2*4/5))
6680    D#(11)=M#*LOG(CDBL(3/2*4/5*4/5*2))
6690    GOSUB 1080
6700  N$="[NO.44] Pure  Temperament (T-39)"
6710    D#(1)=M#*LOG(CDBL(5/3*5/3*5/3*2/3*2/3/2))
6720    D#(2)=M#*LOG(CDBL(5/3*2/3))
6730    D#(3)=M#*LOG(CDBL(5/3*5/3*5/3/4))
6740    D#(4)=M#*LOG(CDBL(5/3*5/3*2/3*2/3))
6750    D#(5)=M#*LOG(CDBL(2/3*2))
6760    D#(6)=M#*LOG(CDBL(5/3*5/3/2))
6770    D#(7)=M#*LOG(CDBL(5/3*2/3*2/3*2))
6780    D#(8)=M#*LOG(CDBL(5/3*5/3*5/3*2/3/2))
6790    D#(9)=M#*LOG(CDBL(5/3))
6800    D#(10)=M#*LOG(CDBL(2/3*2/3*4))
6810    D#(11)=M#*LOG(CDBL(5/3*5/3*2/3*2/2))
6820    GOSUB 1080
6830  N$="[NO.45] Pure  Temperament (T-40)"
6840    D#(1)=M#*LOG(CDBL(3/2*5/3*5/3/4))
6850    D#(2)=M#*LOG(CDBL(5/3*2/3))
6860    D#(3)=M#*LOG(CDBL(5/3*5/3*5/3/4))
6870    D#(4)=M#*LOG(CDBL(3/2*5/3/2))
6880    D#(5)=M#*LOG(CDBL(2/3*2))
6890    D#(6)=M#*LOG(CDBL(5/3*5/3/2))
6900    D#(7)=M#*LOG(CDBL(3/2))
6910    D#(8)=M#*LOG(CDBL(5/3*5/3*5/3*2/3/2))
6920    D#(9)=M#*LOG(CDBL(5/3))
6930    D#(10)=M#*LOG(CDBL(3/2*5/3*5/3*5/3/4))
6940    D#(11)=M#*LOG(CDBL(5/3*5/3*2/3*2/2))
6950    GOSUB 1080
6960  N$="[NO.46] Pure  Temperament (T-41)"
6970    D#(1)=M#*LOG(CDBL(3/2*5/3*5/3/4))
6980    D#(2)=M#*LOG(CDBL(3/2*3/2/2))
6990    D#(3)=M#*LOG(CDBL(5/3*5/3*5/3/4))
7000    D#(4)=M#*LOG(CDBL(3/2*5/3/2))
7010    D#(5)=M#*LOG(CDBL(3/2*3/2*5/3*5/3*5/3/8))
7020    D#(6)=M#*LOG(CDBL(5/3*5/3/2))
7030    D#(7)=M#*LOG(CDBL(3/2))
7040    D#(8)=M#*LOG(CDBL(3/2*3/2*5/3*5/3/4))
7050    D#(9)=M#*LOG(CDBL(5/3))
7060    D#(10)=M#*LOG(CDBL(3/2*5/3*5/3*5/3/4))
7070    D#(11)=M#*LOG(CDBL(3/2*3/2*5/3/2))
7080    GOSUB 1080
7090  N$="[NO.47] Pure  Temperament (T-49)"
7100    D#(1)=M#*LOG(CDBL(3/5*2/3*2/3*4))
7110    D#(2)=M#*LOG(CDBL(5/3*2/3))
7120    D#(3)=M#*LOG(CDBL(3/5*2))
7130    D#(4)=M#*LOG(CDBL(5/3*5/3*2/3*2/3))
7140    D#(5)=M#*LOG(CDBL(2/3*2))
7150    D#(6)=M#*LOG(CDBL(5/3*5/3/2))
7160    D#(7)=M#*LOG(CDBL(5/3*2/3*2/3*2))
7170    D#(8)=M#*LOG(CDBL(3/5*2/3*4))
7180    D#(9)=M#*LOG(CDBL(5/3))
7190    D#(10)=M#*LOG(CDBL(2/3*2/3*4))
7200    D#(11)=M#*LOG(CDBL(5/3*5/3*2/3*2/2))
7210    GOSUB 1080
7220  N$="[NO.48] Pure  Temperament (T-50)"
7230    D#(1)=M#*LOG(CDBL(3/2*5/3*5/3/4))
7240    D#(2)=M#*LOG(CDBL(5/3*2/3))
7250    D#(3)=M#*LOG(CDBL(3/5*2))
7260    D#(4)=M#*LOG(CDBL(3/2*5/3/2))
7270    D#(5)=M#*LOG(CDBL(2/3*2))
7280    D#(6)=M#*LOG(CDBL(5/3*5/3/2))
7290    D#(7)=M#*LOG(CDBL(3/2))
7300    D#(8)=M#*LOG(CDBL(3/5*2/3*4))
7310    D#(9)=M#*LOG(CDBL(5/3))
7320    D#(10)=M#*LOG(CDBL(3/2*3/5*2))
7330    D#(11)=M#*LOG(CDBL(5/3*5/3*2/3*2/2))
7340    GOSUB 1080
7350  N$="[NO.49] Pure  Temperament (T-51)"
7360    D#(1)=M#*LOG(CDBL(3/2*5/3*5/3/4))
7370    D#(2)=M#*LOG(CDBL(3/2*3/2/2))
7380    D#(3)=M#*LOG(CDBL(3/5*2))
7390    D#(4)=M#*LOG(CDBL(3/2*5/3/2))
7400    D#(5)=M#*LOG(CDBL(3/2*3/2*3/5))
7410    D#(6)=M#*LOG(CDBL(5/3*5/3/2))
7420    D#(7)=M#*LOG(CDBL(3/2))
7430    D#(8)=M#*LOG(CDBL(3/2*3/2*5/3*5/3/4))
7440    D#(9)=M#*LOG(CDBL(5/3))
7450    D#(10)=M#*LOG(CDBL(3/2*3/5*2))
7460    D#(11)=M#*LOG(CDBL(3/2*3/2*5/3/2))
7470    GOSUB 1080
7480  N$="[NO.50] Pure  Temperament (T-59)"
7490    D#(1)=M#*LOG(CDBL(3/5*2/3*2/3*4))
7500    D#(2)=M#*LOG(CDBL(5/3*2/3))
7510    D#(3)=M#*LOG(CDBL(3/5*2))
7520    D#(4)=M#*LOG(CDBL(3/5*3/5*2/3*2/3*8))
7530    D#(5)=M#*LOG(CDBL(2/3*2))
7540    D#(6)=M#*LOG(CDBL(3/5*3/5*4))
7550    D#(7)=M#*LOG(CDBL(5/3*2/3*2/3*2))
7560    D#(8)=M#*LOG(CDBL(3/5*2/3*4))
7570    D#(9)=M#*LOG(CDBL(5/3))
7580    D#(10)=M#*LOG(CDBL(2/3*2/3*4))
7590    D#(11)=M#*LOG(CDBL(3/5*3/5*2/3*8))
7600    GOSUB 1080
7610  N$="[NO.51] Pure  Temperament (T-60)"
7620    D#(1)=M#*LOG(CDBL(3/2*3/5*3/5*2))
7630    D#(2)=M#*LOG(CDBL(5/3*2/3))
7640    D#(3)=M#*LOG(CDBL(3/5*2))
7650    D#(4)=M#*LOG(CDBL(3/2*5/3/2))
7660    D#(5)=M#*LOG(CDBL(2/3*2))
7670    D#(6)=M#*LOG(CDBL(3/5*3/5*4))
7680    D#(7)=M#*LOG(CDBL(3/2))
7690    D#(8)=M#*LOG(CDBL(3/5*2/3*4))
7700    D#(9)=M#*LOG(CDBL(5/3))
7710    D#(10)=M#*LOG(CDBL(3/2*3/5*2))
7720    D#(11)=M#*LOG(CDBL(3/5*3/5*2/3*8))
7730    GOSUB 1080
7740  N$="[NO.52] Pure  Temperament (T-61)"
7750    D#(1)=M#*LOG(CDBL(3/2*3/5*3/5*2))
7760    D#(2)=M#*LOG(CDBL(3/2*3/2/2))
7770    D#(3)=M#*LOG(CDBL(3/5*2))
7780    D#(4)=M#*LOG(CDBL(3/2*5/3/2))
7790    D#(5)=M#*LOG(CDBL(3/2*3/2*3/5))
7800    D#(6)=M#*LOG(CDBL(3/5*3/5*4))
7810    D#(7)=M#*LOG(CDBL(3/2))
7820    D#(8)=M#*LOG(CDBL(3/2*3/2*3/5*3/5*2))
7830    D#(9)=M#*LOG(CDBL(5/3))
7840    D#(10)=M#*LOG(CDBL(3/2*3/5*2))
7850    D#(11)=M#*LOG(CDBL(3/2*3/2*5/3/2))
7860    GOSUB 1080
7870  N$="[NO.53] Pure  Temperament (T-69)"
7880    D#(1)=M#*LOG(CDBL(3/5*2/3*2/3*4))
7890    D#(2)=M#*LOG(CDBL(3/5*3/5*3/5*2/3*8))
7900    D#(3)=M#*LOG(CDBL(3/5*2))
7910    D#(4)=M#*LOG(CDBL(3/5*3/5*2/3*2/3*8))
7920    D#(5)=M#*LOG(CDBL(2/3*2))
7930    D#(6)=M#*LOG(CDBL(3/5*3/5*4))
7940    D#(7)=M#*LOG(CDBL(3/5*3/5*3/5*2/3*2/3*16))
7950    D#(8)=M#*LOG(CDBL(3/5*2/3*4))
7960    D#(9)=M#*LOG(CDBL(3/5*3/5*3/5*8))
7970    D#(10)=M#*LOG(CDBL(2/3*2/3*4))
7980    D#(11)=M#*LOG(CDBL(3/5*3/5*2/3*8))
7990    GOSUB 1080
8000  N$="[NO.54] Pure  Temperament (T-70)"
8010    D#(1)=M#*LOG(CDBL(3/2*3/5*3/5*2))
8020    D#(2)=M#*LOG(CDBL(3/5*3/5*3/5*2/3*8))
8030    D#(3)=M#*LOG(CDBL(3/5*2))
8040    D#(4)=M#*LOG(CDBL(3/2*3/5*3/5*3/5*4))
8050    D#(5)=M#*LOG(CDBL(2/3*2))
8060    D#(6)=M#*LOG(CDBL(3/5*3/5*4))
8070    D#(7)=M#*LOG(CDBL(3/2))
8080    D#(8)=M#*LOG(CDBL(3/5*2/3*4))
8090    D#(9)=M#*LOG(CDBL(3/5*3/5*3/5*8))
8100    D#(10)=M#*LOG(CDBL(3/2*3/5*2))
8110    D#(11)=M#*LOG(CDBL(3/5*3/5*2/3*8))
8120    GOSUB 1080
8130  N$="[NO.55] Pure  Temperament (T-71)"
8140    D#(1)=M#*LOG(CDBL(3/2*3/5*3/5*2))
8150    D#(2)=M#*LOG(CDBL(3/2*3/2/2))
8160    D#(3)=M#*LOG(CDBL(3/5*2))
8170    D#(4)=M#*LOG(CDBL(3/2*3/5*3/5*3/5*4))
8180    D#(5)=M#*LOG(CDBL(3/2*3/2*3/5))
8190    D#(6)=M#*LOG(CDBL(3/5*3/5*4))
8200    D#(7)=M#*LOG(CDBL(3/2))
8210    D#(8)=M#*LOG(CDBL(3/2*3/2*3/5*3/5*2))
8220    D#(9)=M#*LOG(CDBL(3/5*3/5*3/5*8))
8230    D#(10)=M#*LOG(CDBL(3/2*3/5*2))
8240    D#(11)=M#*LOG(CDBL(3/2*3/2*3/5*3/5*3/5*4))
8250    GOSUB 1080
8260  IF FLG=2 THEN CLOSE #1


1000 'save "jmacs-12.bas",a
1010  DIM D#(12):M#=1200#/LOG(2#):OPEN "temp.dat" FOR INPUT AS #1
1020  XX=0
1030  A$="Pitch Differences of Temperaments : Note = [ C ]"
1040  KEY OFF:SCREEN 1,0:LOCATE 1,10,0:PRINT A$
1050  LOCATE 2,1:PRINT "(Cent)":LOCATE 3,1:PRINT "  +45"
1060  LOCATE 6,1:PRINT "  +30":LOCATE 9,1:PRINT "  +15"
1070  LOCATE 12,1:PRINT "    0":LOCATE 15,1:PRINT "  -15"
1080  LOCATE 18,1:PRINT "  -30":LOCATE 21,1:PRINT "  -45"
1090  LOCATE 23,1:PRINT "Temp : 1          10          20          30";
1100  PRINT "          40          50     55";
1110  FOR I=1 TO 55:INPUT #1,A$:FOR J=0 TO 11:INPUT #1,D#(J):NEXT J
1120   X#=D#(XX)-CDBL(100*XX)
1130   X=75+I*900/55
1140   Y=333-X#*293/52
1150   CIRCLE (X,Y),3,,,,.5
1160   LINE (X,Y)-(X,333)
1170  NEXT I
1180  LINE (75,333)-(1000,333),,,&H3333:LINE (75,40)-(1000,625),,B
1190  IF INKEY$="" THEN 1190 ELSE SCREEN 0,0:LOCATE 1,1,1


1000 'save "jmacs-13.bas",a
1010  KEY OFF:SCREEN 1,0:LOCATE 1,1,0
1020  WW=15:A=100:B=80:C=60
1030  I=0'**********************
1040   LINE (0,30+I*232)-(1023,210+I*232),,B
1050   LINE (0,120+I*232)-(1023,120+I*232),,,&H3333
1060   XOLD=0:YOLD=120+I*232:ZPT=YOLD
1070   LOCATE 8*I+1,5:PRINT "[ Pure Temperament ]";
1080   PRINT "  1st : 3rd = 4 : 5  /  1st : 5th = 2 : 3";
1090   FOR J=0 TO 1022
1100    Y1=SIN(J*3.1416*2/WW)
1110    Y2=SIN(J*3.1416*2/WW*5/4)
1120    Y3=SIN(J*3.1416*2/WW*3/2)
1130    Y=ZPT-(A*Y1+B*Y2+C*Y3)/3.5
1140    LINE (XOLD,YOLD)-(J,Y):XOLD=J:YOLD=Y
1150   NEXT J
1160  I=1'**********************
1170   LINE (0,30+I*232)-(1023,210+I*232),,B
1180   LINE (0,120+I*232)-(1023,120+I*232),,,&H3333
1190   XOLD=0:YOLD=120+I*232:ZPT=YOLD
1200   LOCATE 8*I+1,5:PRINT "[ 12 Equal Temperament ]";
1210   PRINT "  3rd = 400 Cent  /  5th = 700 Cent"
1220   FOR J=0 TO 1022
1230    Y1=SIN(J*3.1416*2/WW)
1240    Y2=SIN(J*3.1416*2/WW*2^(4/12))
1250    Y3=SIN(J*3.1416*2/WW*2^(7/12))
1260    Y=ZPT-(A*Y1+B*Y2+C*Y3)/3.5
1270    LINE (XOLD,YOLD)-(J,Y):XOLD=J:YOLD=Y
1280   NEXT J
1290  IF INKEY$="" THEN 1290
1300  SCREEN 0,0:LOCATE 1,1,1


1000 'save "jmacs-14.bas",a
1010  KEY OFF:SCREEN 1,0:M#=1200#/LOG(2#)
1020  DIM D#(12):DIM X5(12),Y5(12),X3(12),Y3(12),F(12)
1030  DATA 3,8,1,6,11,4,9,2,7,0,5,10
1040  RESTORE 1030:FOR I=0 TO 11:READ F(I):NEXT I
1050  B$="C C#D EbE F F#G G#A BbB "
1060  OPEN "temp.dat" FOR INPUT AS #1
1070  FOR LOOP=1 TO 55
1080   CLS:INPUT #1,A$
1090   FOR I=0 TO 11:INPUT #1,D#(I):NEXT I
1100   FOR I=0 TO 11:X=644+350*COS(3.1416/6*I):Y=392-350*SIN(3.1416/6*I)
1110    LINE (644,392)-(X,Y),,,&H1111:NEXT I
1120   LOCATE 8,50:PRINT "C":LOCATE 9,43:PRINT "F":LOCATE 9,56:PRINT "G"
1130    LOCATE 11,38:PRINT "Bb":LOCATE 11,61:PRINT "D"
1140    LOCATE 14,36:PRINT "Eb":LOCATE 14,63:PRINT "A"
1150    LOCATE 17,38:PRINT "G#":LOCATE 17,61:PRINT "E"
1160    LOCATE 20,50:PRINT "F#":LOCATE 19,43:PRINT "C#":LOCATE 19,56:PRINT "B"
1170   FOR I=0 TO 11:J=(I+7) MOD 12:K=(I+4) MOD 12
1180    P=D#(I):Q=D#(J):R=Q-P:IF R<0 THEN R=R+1200
1190    S=D#(I):T=D#(K):U=T-S:IF U<0 THEN U=U+1200
1200    X5(I)=644+(20+7*ABS(R-701.955))*COS(3.1416/6*F(I))
1210    Y5(I)=392-(20+7*ABS(R-701.955))*SIN(3.1416/6*F(I))
1220    X3(I)=644+(20+7*ABS(U-386.314))*COS(3.1416/6*F(I))
1230    Y3(I)=392-(20+7*ABS(U-386.314))*SIN(3.1416/6*F(I))
1240    NEXT I
1250   FOR I=0 TO 11:J=(I+7) MOD 12
1260    LINE (X5(I),Y5(I))-(X5(J),Y5(J))
1270    LINE (X3(I),Y3(I))-(X3(J),Y3(J)),,,&H6666
1280    NEXT I
1290   LOCATE 2,3,0:PRINT A$
1300   FOR I=0 TO 11
1310    LOCATE 5+I,6:PRINT "["+MID$(B$,2*I+1,2)+"] =";
1320    PRINT USING " ####.####";D#(I):NEXT I
1330  LINE (0,0)-(1023,767),,B
1340  IF INKEY$="" THEN 1340 ELSE NEXT LOOP
1350  CLOSE #1:SCREEN 0,0:LOCATE 1,1,1


1000 'save "jmacs-15.bas",a
1010 '===== Data Area =====
1020  DATA 100,100,100,0,0,0,100,80,60,0,0,0,100,80,90,0,0,0
1030  DATA 100,80,90,0,1,0,100,80,90,0,2,0,100,80,90,0,3,0
1040  DATA 100,80,90,0,5,0,100,80,90,0,10,0,100,80,90,0,20,0
1050  DATA 100,80,90,0,0,1,100,80,90,0,0,2,100,80,90,0,0,3
1060  DATA 100,80,90,0,0,5,100,80,90,0,0,10,100,80,90,0,0,20
1070  DATA 100,80,90,0,2,5,100,80,90,0,10,3,100,80,90,0,10,20
1080  KEY OFF:SCREEN 1,0:LOCATE 1,1,0:DIM M(6,3):RESTORE 1010
1090  FOR LOOP=1 TO 6
1100   CLS:FOR J=1 TO 3: FOR I=1 TO 6:READ M(I,J):NEXT I:NEXT J
1110   FOR I=0 TO 2
1120    A=M(1,I+1):B=M(2,I+1):C=M(3,I+1):D=M(4,I+1):E=M(5,I+1):F=M(6,I+1)
1130    A$=RIGHT$(STR$(A),LEN(STR$(A))-1)
1140    B$=RIGHT$(STR$(B),LEN(STR$(B))-1)
1150    C$=RIGHT$(STR$(C),LEN(STR$(C))-1)
1160    LOCATE 8*I+1,3
1170    PRINT "[Pure Temp.]   1st(";A$;"%)=";D;"ms , ";
1180    PRINT "3rd(";B$;"%)=";E;"ms , ";
1190    PRINT "5th(";C$;"%)=";F;"ms";
1200    LINE (0,30+I*232)-(1023,210+I*232),,B
1210    LINE (0,120+I*232)-(1023,120+I*232),,,&H3333
1220    G=(A+B+C)/3:A=A*100/G:B=B*100/G:C=C*100/G
1230    XOLD=0:YOLD=120+I*232:ZPT=YOLD
1240    FOR J=0 TO 1022 STEP 1
1250     T=.2*J:X=J
1260     IF D100 OR IY>100 THEN 1340
1240  CLS:LOCATE 11,4,0:PRINT "X =";IX
1250  LOCATE 14,4:PRINT "Y =";IY
1260  FOR I=1 TO 360 STEP TT
1270   X=XP+300*COS(I*3.1415926535#/180*IX)
1280   Y=YP-300*SIN(I*3.1415926535#/180*IY)
1290   CIRCLE (X,Y),1
1300  NEXT I
1310  LINE (0,0)-(1023,767),,B
1320  IF INKEY$="" THEN 1320
1330  GOTO 1220
1340  SCREEN 0,0:LOCATE 1,1,1


1000 'save "jmacs-18.bas",a
1010  KEY OFF:SCREEN 1,0:M#=1200#/LOG(2#)
1020  TT=.1:XP=600:YP=384
1030  DATA 1,"1度",1,1
1040  DATA 1,"完全5度",3,2
1050  DATA 1,"完全4度",4,3
1060  DATA 1,"長3度",5,4
1070  DATA 1,"短3度",6,5
1080  DATA 1,"大全音",9,8
1090  DATA 1,"小全音",10,9
1100  DATA 1,"全音階的半音",16,15
1110  DATA 1,"半音階的小半音",25,24
1120  DATA 1,"大リンマ",27,25
1130  DATA 1,"半音階的大半音",135,128
1140  DATA 1,"ピタゴラス・リンマ",256,243
1150  DATA 999," ",1,1
1160  RESTORE 1030
1170 '#### LOOP ####
1180  READ FLG,A$,IX,IY:IF FLG=0 THEN 1180 ELSE IF FLG=999 THEN 1310
1190  CLS:LOCATE 7,2,0:PRINT "[Name]":LOCATE 9,4:PRINT A$
1200  LOCATE 13,2:PRINT "[Ratio]":LOCATE 15,4:PRINT IX;":";IY
1210   X=XP+300:Y=YP
1220  FOR I=0 TO 360 STEP TT
1230   XX=XP+300*COS(I*3.1415926535#/180*IX)
1240   YY=YP-300*SIN(I*3.1415926535#/180*IY)
1250   LINE (X,Y)-(XX,YY)
1260   X=XX:Y=YY
1270  NEXT I
1280  LINE (0,0)-(1023,767),,B
1290  IF INKEY$="" THEN 1290
1300  GOTO 1170
1310  SCREEN 0,0:LOCATE 1,1,1:LIST 1000-1170


1000 'save "jmacs-19.bas",a
1010  KEY OFF:SCREEN 1,0:M#=1200#/LOG(2#)
1020  LP=32:TT=.5:XP#=600#:YP#=384#
1030  A$="平均律完全5度"
1040  PP#=1.498307077#
1050  CLS:LOCATE 5,2,0:PRINT "[Name]":LOCATE 7,4:PRINT A$
1060  LOCATE 11,2:PRINT "[Ratio]":LOCATE 13,4:PRINT PP#
1070  LOCATE 17,2:PRINT "[Count]":LOCATE 19,4:PRINT LP;"Loop"
1080   X=XP#+300:Y=YP#
1090  FOR I=0 TO 360*LP STEP TT
1100   J#=CDBL(I)
1110   XX=XP#+300#*COS(J#*3.1415926535#/180#*PP#*2#)
1120   YY=YP#-300#*SIN(J#*3.1415926535#/180#*2#)
1130   LINE (X,Y)-(XX,YY)
1140   X=XX:Y=YY
1150  NEXT I
1160  LINE (0,0)-(1023,767),,B
1170  IF INKEY$="" THEN 1170
1180  SCREEN 0,0:LOCATE 1,1,1


1000 'save "jmacs-20.bas",a
1010  KEY OFF:SCREEN 1,0:M#=1200#/LOG(2#)
1020  LP=8:TT=.5:XP#=600#:YP#=384#
1030  A$="平均律長3度"
1040  PP#=1.25992105#
1050  CLS:LOCATE 5,2,0:PRINT "[Name]":LOCATE 7,4:PRINT A$
1060  LOCATE 11,2:PRINT "[Ratio]":LOCATE 13,4:PRINT PP#
1070  LOCATE 17,2:PRINT "[Count]":LOCATE 19,4:PRINT LP;"Loop"
1080   X=XP#+300:Y=YP#
1090  FOR I=0 TO 360*LP STEP TT
1100   J#=CDBL(I)
1110   XX=XP#+300#*COS(J#*3.1415926535#/180#*PP#*2#)
1120   YY=YP#-300#*SIN(J#*3.1415926535#/180#*2#)
1130   LINE (X,Y)-(XX,YY)
1140   X=XX:Y=YY
1150  NEXT I
1160  LINE (0,0)-(1023,767),,B
1170  IF INKEY$="" THEN 1170
1180  SCREEN 0,0:LOCATE 1,1,1


1000 'save "jmacs-21.bas",a
1010  KEY OFF:SCREEN 1,0:M#=1200#/LOG(2#)
1020  LP=1:TT=.5:XP#=600#:YP#=384#
1030  A$="ピタゴラス音律長3度"
1040  PP#=1.265625#
1050  CLS:LOCATE 5,2,0:PRINT "[Name]":LOCATE 7,4:PRINT A$
1060  LOCATE 11,2:PRINT "[Ratio]":LOCATE 13,4:PRINT PP#
1070  LOCATE 17,2:PRINT "[Count]":LOCATE 19,4:PRINT LP;"Loop"
1080   X=XP#+300:Y=YP#
1090  FOR I=0 TO 360*LP STEP TT
1100   J#=CDBL(I)
1110   XX=XP#+300#*COS(J#*3.1415926535#/180#*PP#*4#)
1120   YY=YP#-300#*SIN(J#*3.1415926535#/180#*4#)
1130   LINE (X,Y)-(XX,YY)
1140   X=XX:Y=YY
1150  NEXT I
1160  LINE (0,0)-(1023,767),,B
1170  IF INKEY$="" THEN 1170
1180  SCREEN 0,0:LOCATE 1,1,1


1000 'save "jmacs-22.bas",a
1010  KEY OFF:SCREEN 1,0:M#=1200#/LOG(2#)
1020  LP=8:TT=.5:XP#=600#:YP#=384#
1030  A$="ミーントーン5度"
1040  PP#=1.495348781#
1050  CLS:LOCATE 5,2,0:PRINT "[Name]":LOCATE 7,4:PRINT A$
1060  LOCATE 11,2:PRINT "[Ratio]":LOCATE 13,4:PRINT PP#
1070  LOCATE 17,2:PRINT "[Count]":LOCATE 19,4:PRINT LP;"Loop"
1080   X=XP#+300:Y=YP#
1090  FOR I=0 TO 360*LP STEP TT
1100   J#=CDBL(I)
1110   XX=XP#+300#*COS(J#*3.1415926535#/180#*PP#*2#)
1120   YY=YP#-300#*SIN(J#*3.1415926535#/180#*2#)
1130   LINE (X,Y)-(XX,YY)
1140   X=XX:Y=YY
1150  NEXT I
1160  LINE (0,0)-(1023,767),,B
1170  IF INKEY$="" THEN 1170
1180  SCREEN 0,0:LOCATE 1,1,1

BASIC その20 音源制御プログラム

10 'save "b:editor.bas",a
20  DATA "<<<<<<  DSP-PTB SYSTEM  DEBUG/EDIT PROGRAM   by Y.Nagashima  >>>>>>"
30 ' DEBUG=1:' <-- Not [REM] = Only Development Mode !!
40 '======= Menu Screen Setting =======
50   DIM GD$(100,2),INDT$(128),DF$(100),DFT(100)
60   DIM A$(4,18),A(1,72),B(72):' A=Tone Data,B=Flag
70   DIM Y(5,256),PF(14):'Y=Wave Data
80   DIM HC(128):'Harmony Coefficients
90   MAIT=200:OUTPT=&HE0D0:INPT=&HE0D8:OUT OUTPT,0
100   TEST=1:' <-- Timer Escape Mode ON !!
110   SCREEN 3:CONSOLE 0,25,0,1:WIDTH 80,25:COLOR 7:CLS 3
120   VIEW(0,0)-(639,399),,7
130 '======= Mode Select =======
140   CLS 3
150   LOCATE 5,9:COLOR 5
160   PRINT "If you have missed operation, and if you want to return to ";
170   PRINT "EDITOR,"
180   LOCATE 5,12:COLOR 5
190   PRINT "Please Input the word  !!!"
200   A$=INKEY$:IF A$="" THEN 200
210   IF A$<>"s" AND A$<>"S" THEN 350
220   LOCATE 20,18:COLOR 4:PRINT A$;
230   A$=INKEY$:IF A$="" THEN 230
240   IF A$<>"o" AND A$<>"O" THEN 350
250   LOCATE 23,18:COLOR 4:PRINT A$;
260   A$=INKEY$:IF A$="" THEN 260
270   IF A$<>"r" AND A$<>"R" THEN 350
280   LOCATE 26,18:COLOR 4:PRINT A$;
290   A$=INKEY$:IF A$="" THEN 290
300   IF A$<>"r" AND A$<>"R" THEN 350
310   LOCATE 29,18:COLOR 4:PRINT A$;
320   A$=INKEY$:IF A$="" THEN 320
330   IF A$<>"y" AND A$<>"Y" THEN 350
340   GOTO 870:' Jump to Command Control Mode
350 '======= OS Mode =======
360   CLS 3
370   LOCATE 20,0:COLOR 5:PRINT "*** ";:COLOR 6
380   PRINT "OS MODE";:COLOR 5:PRINT " ***"
390   LOCATE 18,6:COLOR 6:PRINT "<<>>"
400   LOCATE 20,8:COLOR 7:PRINT "0:SYSTEM RESET"
410   LOCATE 20,10:COLOR 7:PRINT "1:System Boot"
420   LOCATE 20,12:COLOR 7:PRINT "2:Execute"
430   LOCATE 20,14:COLOR 7:PRINT "Input Number=";
440   K=VAL(INPUT$(1))
450   PRINT K
460   IF K>2 THEN BEEP :GOTO 110
470   ON K GOTO 110,510
480   LOCATE 20,8:COLOR 3:PRINT "0:SYSTEM RESET"
490   OD=&H0:GOSUB 2590:RD=&HF0:GOSUB 2710
500   GOTO 110
510   LOCATE 20,12:COLOR 3:PRINT "2:Execute"
520   LOCATE 20,15:COLOR 7:PRINT "(RETURN to Tester mode)"
530   LOCATE 20,14:COLOR 7:INPUT "Input jump address:&H",K$
540   IF K$="" THEN K$="E000"
550   K$=RIGHT$("000"+K$,4)
560   OD=&H2:GOSUB 2590:OD=VAL("&H"+LEFT$(K$,2)):GOSUB 2590
570   OD=VAL("&H"+RIGHT$(K$,2)):GOSUB 2590:RD=&HF2:GOSUB 2710
580   IF VAL("&H"+K$)<>&HE000 THEN PRINT "Not availavle":END
590 '======= Tester Mode =======
600   COLOR 7:CLS 3
610   LOCATE 20,0:COLOR 5:PRINT "*** OS MODE-->";
620   COLOR 6:PRINT "Tester MODE ";:COLOR 5:PRINT "***"
630   LOCATE 18,6:COLOR 5:PRINT "<<>>"
640   LOCATE 20,7:COLOR 6:PRINT "<<>>"
650   LOCATE 22,9:COLOR 7:PRINT "0:Return to OS mode"
660   LOCATE 22,11:COLOR 7:PRINT "1:Command control mode"
670   LOCATE 22,13:COLOR 7:PRINT "2:Realtime mode"
680   LOCATE 22,15:COLOR 7:PRINT "Input Number=";
690   K=VAL(INPUT$(1))
700   PRINT K
710   IF K>2 THEN BEEP :GOTO 390
720   ON K GOTO 860,750
730   OD=&H0:GOSUB 2590:RD=&HF1:GOSUB 2710
740   GOTO 110
750   CLS 3
760   LOCATE 20,0:COLOR 5:PRINT "***OS MODE-->Tester MODE-->";
770   COLOR 6:PRINT "Realtime MODE";:COLOR 5:PRINT "***"
780   LOCATE 18,6:COLOR 5:PRINT "<<>>"
790   LOCATE 20,7:COLOR 5:PRINT "<<>>"
800   LOCATE 22,8:COLOR 6:PRINT "<<>>"
810   LOCATE 24,11:COLOR 7:PRINT "(RETURN to Tester mode)"
820   LOCATE 24,10:COLOR 7:INPUT"INPUT Register Number";K$
830   IF K$="" THEN GOTO 600
840   LOCATE 24,12:COLOR 7:INPUT"INPUT Register DATA";K
850   GOTO 760
860   OD=&H1:GOSUB 2590:RD=&HF3:GOSUB 2710
870 '======= Command Control Mode =======
880   CLS 3
890   LOCATE 10,0:COLOR 5:PRINT "***OS MODE-->Tester MODE-->";
900   COLOR 6:PRINT "Command MODE";:COLOR 5:PRINT "***"
910   COLOR 7
920   XD=1:YD=1:OX=1:OY=1
930   LOCATE 25,7:PRINT "Wait for moments !!!"
940   LOCATE 1,13:PRINT STRING$(72,"*")
950   RESTORE 3220:BN=0
960    FOR J=1 TO 18
970      FOR I=1 TO 4
980        READ G$:A$(I,J)=G$:BN=BN+1
990        A(0,BN)=VAL("&h"+RIGHT$(A$(I,J),1))
1000        A(1,BN)=VAL("&h"+MID$(A$(I,J),15,1))
1010        LOCATE 72-(J-1)*4-(I-1),13:PRINT " ";
1020      NEXT I
1030    NEXT J
1040   RESTORE 3650:FOR I=1 TO 72:READ B(I):NEXT I
1050 '======== Print Main Menu =======
1060   CLS 3
1070   COLOR 6:LOCATE 5,1:RESTORE 20:READ G$:PRINT G$
1080   GOSUB 3130:' <--- flame / Mode Writing
1090    FOR I=1 TO 18
1100      FOR J=1 TO 4
1110        LOCATE (J-1)*19+4,(I-1)+6:PRINT A$(J,I)
1120     NEXT J
1130    NEXT I
1140   LINE (30,11*16)-(18*8*3+33+8,11*16)
1150   LINE (18*8*3+34+8,10*16)-(18*8*4+33+8,12*16),,B
1160   LINE (30,12*16)-(18*8*4+33+8,12*16)
1170   LINE (18*8*2+27+8,12*16)-(18*8*2+27+8,16*16),,B
1180   LINE (29,9*16)-(18*8*4+33+8,16*16),,B
1190 '======== Edit Main Menu =======
1200    GOSUB 3170:GOSUB 3180:GOSUB 3190
1210 '  KKK$=CHR$(28)+CHR$(29)+CHR$(31)+CHR$(30)+"Q"+"q"+CHR$(127)+CHR$(18)
1220    LC=4:GOSUB 1440
1230    D$=INKEY$:IF D$="" THEN 1230
1240 '  ON INSTR(KKK$,D$) GOSUB 3070,3080,3100,3090,1530,1530,1290,1250
1241    IF D$=CHR$(28) THEN GOSUB 3070
1242    IF D$=CHR$(29) THEN GOSUB 3080
1243    IF D$=CHR$(31) THEN GOSUB 3100
1244    IF D$=CHR$(30) THEN GOSUB 3090
1245    IF D$="Q" THEN GOSUB 1530
1246    IF D$="q" THEN GOSUB 1530
1247    IF D$=CHR$(127) THEN GOSUB 1290
1248    IF D$=CHR$(18) THEN GOSUB 1250
1250    COLOR 7
1260    LOCATE (OX-1)*19+4,(OY-1)+6:PRINT A$(OX,OY)
1270    OX=XD:OY=YD
1280    GOTO 1220
1290    RETURN 1300
1300    GOSUB 3160:GOSUB 3180:GOSUB 3200
1310    NB=(YD-1)*4+XD:GOSUB 1420
1320    LC=6:GOSUB 1440
1330    IF B(NB)<>0 THEN GOSUB 1790:GOTO 1200
1340    A$(XD,YD)=LEFT$(A$(XD,YD),14)+HEX$(A(1,NB))+HEX$(A(0,NB))
1350    LOCATE XD*19-1,YD+5:COLOR CH:PRINT HEX$(A(1,NB));
1360    LOCATE XD*19,YD+5:COLOR CL:PRINT HEX$(A(0,NB));
1370    D$=INKEY$:IF D$="" THEN 1370
1380 '  ON INSTR(KKK$,D$) GOSUB 1420,1430,1490,1510,1530,1530,1400,1410
1381    IF D$=CHR$(28) THEN GOSUB 1420
1382    IF D$=CHR$(29) THEN GOSUB 1430
1383    IF D$=CHR$(31) THEN GOSUB 1490
1384    IF D$=CHR$(30) THEN GOSUB 1510
1385    IF D$="Q" THEN GOSUB 1530
1386    IF D$="q" THEN GOSUB 1530
1387    IF D$=CHR$(127) THEN GOSUB 1400
1388    IF D$=CHR$(18) THEN GOSUB 1410
1390    GOTO 1340
1400    GOSUB 1550
1410    RETURN 1200
1420    HL=0:CH=6:CL=5:RETURN
1430    HL=1:CH=5:CL=6:RETURN
1440    COLOR LC
1450    LOCATE (XD-1)*19+4,(YD-1)+6:PRINT A$(XD,YD)
1460    LOCATE 0,0:'DUMY
1470    RETURN
1480 '======= Key Scan Routine =======
1490    IF A(HL,NB)=0 THEN A(HL,NB)=15 ELSE A(HL,NB)=A(HL,NB)-1
1500    RETURN
1510    IF A(HL,NB)=15 THEN A(HL,NB)=0 ELSE A(HL,NB)=A(HL,NB)+1
1520    RETURN
1530 '======= Program Stop Routine =======
1540    COLOR 7:CLS:OUT OUTPT,0:END
1550 '======= Tone Data Block Transfer routine =======
1560    GOSUB 3160:GOSUB 3170:GOSUB 3210
1570    LC=2:GOSUB 1440
1580    IF NB>40 THEN RETURN
1590    IF NB>12 THEN 1680
1600    IF NB=6 THEN 1620
1610    RETURN
1620    OD=&H50:GOSUB 2590
1630    OD=A(0,2):GOSUB 2590
1640    RD=&H0:GOSUB 2710:RETURN:' Song NO. Transfer
1650 '======= Tone Data Send =======
1660    DATA 21,22,23,25,27,29,30,33,34,31,32,35,36,20,24,13,14
1670    DATA 26,28,15,16,17,18,19,37,38,39,40
1680    XX=A(1,1)*16+A(0,1):IF XX>31 THEN BEEP:GOTO 1610
1690    OD=&H30:GOSUB 2590
1700    OD=XX:GOSUB 2590
1710    RESTORE 1660
1720    SUM=XX
1730     FOR J=1 TO 28
1740       READ K:XX=A(1,K)*16+A(0,K):SUM=(SUM+XX) MOD 256
1750       OD=XX:GOSUB 2590
1760     NEXT J
1770    FOR J=29 TO 48:OD=0:GOSUB 2590:NEXT J
1780    OD=SUM:GOSUB 2590:RD=&H0:GOSUB 2710:RETURN
1790 '======= Command Transfer Routine =======
1800    GOSUB 3160:GOSUB 3170:GOSUB 3210
1810    LC=2:GOSUB 1440
1820    IF B(NB)=0 THEN RETURN:' Not Command !
1830    IF B(NB)=1 THEN RETURN:' Not Defined ! (Reserved)
1840    IF B(NB)=2 THEN GOSUB 3710:' Song Data Transfer
1850    IF B(NB)=3 THEN GOSUB 2050:' Play Start
1860    IF B(NB)=4 THEN GOSUB 2070:' Play Stop
1870    IF B(NB)=5 THEN GOSUB 2090:' Play Continue
1880    IF B(NB)=6 THEN GOSUB 8240:' Wave Drawing Editor
1890    IF B(NB)=7 THEN GOSUB 6600:' Harmonics Editor
1900    IF B(NB)=8 THEN GOSUB 6330:' Data File Send
1910    IF B(NB)=9 THEN GOSUB 7050:' File Killer
1920    IF B(NB)=10 THEN GOSUB 2120:' Tone Data Load
1930    IF B(NB)=11 THEN GOSUB 2290:' Tone Data Save
1940    IF B(NB)=12 THEN GOSUB 4160:' Old Wave Editor
1950    IF B(NB)=13 THEN GOSUB 2570:' Return to TEST Mode
1960    IF B(NB)=14 THEN GOSUB 9340:' File Name List Up
1970    IF B(NB)=15 THEN GOSUB 11350:' PCM Data Plotter
1980    IF B(NB)=16 THEN GOSUB 10990:' Full PCM Data Converter
1990    IF B(NB)=17 THEN GOSUB 9410:' Sweep PCM Data Maker
2000    IF B(NB)=18 THEN GOSUB 2040:'
2010    IF B(NB)=19 THEN GOSUB 11540:' Files Define
2020    IF B(NB)=20 THEN GOSUB 12240:' Files Preset
2030    IF B(NB)=21 THEN GOSUB 2040
2040    RETURN
2050    OD=&H90:GOSUB 2590:OD=16*A(1,4)+A(0,4)
2060    GOSUB 2590:RD=&H0:GOSUB 2710:RETURN:' Play Start
2070    OD=&HB0:GOSUB 2590:OD=16*A(1,4)+A(0,4)
2080    GOSUB 2590:RD=&H0:GOSUB 2710:RETURN:' Play Stop
2090    OD=&HA0:GOSUB 2590:OD=16*A(1,4)+A(0,4)
2100    GOSUB 2590:RD=&H0:GOSUB 2710:RETURN:' Play Continue
2110 '======= Tone Data File Load =======
2120    COLOR 7:CLS 3
2130    LOCATE 25,0:COLOR 6:PRINT "LOAD TONE DATA"
2140    LOCATE 0,3:COLOR 7:PRINT "FILE NAME LIST":SY=CSRLIN+1
2150    FILES "b:\data\*.DAT":MY=CSRLIN-1
2160    GOSUB 2380
2170    IF F$="        " THEN BEEP :GOTO 2270
2180    OPEN "b:\data\"+F$+".DAT" FOR INPUT AS #1
2190     FOR K=13 TO 40
2200       INPUT #1,XX
2210       A(1,K)=INT(XX/16)
2220       A(0,K)=XX MOD 16
2230       A$=HEX$(A(1,K))+HEX$(A(0,K))
2240       TX=(K MOD 4):TY=INT(K/4)+1
2250       A$(TX,TY)=LEFT$(A$(TX,TY),14)+A$
2260     NEXT K
2270    CLOSE #1:RETURN 1050
2280 '======= Tone Data File Save =======
2290    COLOR 7:CLS 3
2300    LOCATE 25,0:COLOR 6:PRINT "SAVE TONE DATA"
2310    LOCATE 0,3:COLOR 7:PRINT "FILE NAME LIST":FILES "b:\data\*.DAT"
2320    INPUT "Save File Name=";F$
2330    IF F$="" THEN BEEP :GOTO 2370
2340    OPEN "b:\data\"+F$+".DAT" FOR OUTPUT AS #1
2350    FOR K=13 TO 40:PRINT #1,A(1,K)*16+A(0,K):NEXT K
2360    FOR K=1 TO 20:PRINT #1,0:NEXT K
2370    CLOSE #1:RETURN 1050
2380 '======= File Name Edit =======
2390    CX=0:CY=SY:DEF SEG=&HA000
2400    K$="":FOR I=CX*2+CY*160 TO CX*2+CY*160+79 STEP 2:K$=K$+CHR$(PEEK(I))
2410    NEXT I
2420    LOCATE CX,CY:COLOR 4:PRINT K$:LOCATE 0,0
2430    D$=INPUT$(1)
2440    COLOR 7:LOCATE CX,CY:PRINT K$
2450    ON INSTR(KKK$,D$) GOSUB 2470,2470,2480,2500,2490,2490,2520,2520
2460    GOTO 2400
2470    CX=(CX+40)MOD 80:RETURN
2480    IF CY=MY THEN CY=SY ELSE CY=CY+1
2490    RETURN
2500    IF CY=SY THEN CY=MY ELSE CY=CY-1
2510    RETURN
2520    COLOR 6:LOCATE CX,CY
2530    F$=LEFT$(K$,8)
2540    RETURN 2550
2550    RETURN
2560 '======= Return to Tester mode =======
2570    FD=1000:OD=&H0:GOSUB 2590:RD=&HF2:GOSUB 2710
2580    RETURN 590
2590 '======= 1 Data Send Only =======
2600   IF DEBUG=1 THEN RETURN
2610    ODD=OD AND &HF:OD=INT(OD/16)
2620    OUT OUTPT,(&H10 OR (&HF AND (NOT OD))):CTR=0
2630    ID=INP(INPT) AND &H20:RR=1:GOSUB 3010:IF ID<>0 THEN 2630
2640    OUT OUTPT,0:CTR=0
2650    ID=INP(INPT) AND &H20:RR=1:GOSUB 3010:IF ID=0 THEN 2650
2660    OUT OUTPT,(&H10 OR (&HF AND (NOT ODD))):CTR=0
2670    ID=INP(INPT) AND &H20:RR=2:GOSUB 3010:IF ID<>0 THEN 2670
2680    OUT OUTPT,0:CTR=0
2690    ID=INP(INPT) AND &H20:RR=2:GOSUB 3010:IF ID=0 THEN 2690
2700    RETURN
2710 '======= Message Receive only =======
2720   IF DEBUG=1 THEN RETURN
2730    OUT OUTPT,&H10:CTR=0
2740    ID=INP(INPT) AND &H2F:RR=3:GOSUB 3010:IF ID<>0 THEN 2740:'<0>
2750    OUT OUTPT,0:CTR=0
2760    ID=INP(INPT) AND &H2F:RR=3:GOSUB 3010:IF ID<>&H2F THEN 2760:'
2770    OUT OUTPT,&H10:CTR=0
2780    ID=INP(INPT) AND &H2F:RR=4:GOSUB 3010:IF ID<>&HF THEN 2780
2790    OUT OUTPT,0:CTR=0
2800    ID=INP(INPT) AND &H20:RR=4:GOSUB 3010:IF ID=0 THEN 2800
2810    OUT OUTPT,&H10:CTR=0
2820    ID=INP(INPT) AND &H2F:RR=5:GOSUB 3010:IF (ID AND &H20)<>0 THEN 2820:'H
2830    GD=16*(ID AND &HF):OUT OUTPT,0:CTR=0
2840    ID=INP(INPT) AND &H2F:RR=5:GOSUB 3010:IF (ID AND &H20)=0 THEN 2840:'
2850    GD=GD OR (ID AND &HF):OUT OUTPT,&H10:CTR=0
2860    ID=INP(INPT) AND &H2F:RR=6:GOSUB 3010:IF ID<>&HF THEN 2860
2870    OUT OUTPT,0:CTR=0
2880    ID=INP(INPT) AND &H20:RR=6:GOSUB 3010:IF ID=0 THEN 2880
2890    IF RD=GD THEN RETURN ELSE RR=0:GOTO 3030
2900 '======= 8 Bit Data Receive only =======
2910   IF DEBUG=1 THEN RETURN
2920    OUT OUTPT,&H10:CTR=0
2930    ID=INP(INPT) AND &H2F:RR=7:GOSUB 3010:IF (ID AND &H20)<>0 THEN 2930:'H
2940    GD=16*(ID AND &HF):OUT OUTPT,0:CTR=0
2950    ID=INP(INPT) AND &H2F:RR=7:GOSUB 3010:IF (ID AND &H20)=0 THEN 2950:'
2960    GD=GD OR (ID AND &HF):OUT OUTPT,&H10:CTR=0
2970    ID=INP(INPT) AND &H2F:RR=8:GOSUB 3010:IF ID<>&HF THEN 2970
2980    OUT OUTPT,0:CTR=0
2990    ID=INP(INPT) AND &H20:RR=8:GOSUB 3010:IF ID=0 THEN 2990
3000    RETURN
3010 '======= Communication Error Check =======
3020    CTR=CTR+1:IF TEST=0 OR CTR"
3150    GOSUB 3160:GOSUB 3170:GOSUB 3180:RETURN
3160    LOCATE 32,4:COLOR 7:PRINT " DATA SELECT ":RETURN
3170    LOCATE 47,4:COLOR 7:PRINT " DATA CHANGE ":RETURN
3180    LOCATE 62,4:COLOR 7:PRINT " DATA TRANSFER ":RETURN
3190    LOCATE 32,4:COLOR 3:PRINT " DATA SELECT ":RETURN
3200    LOCATE 47,4:COLOR 3:PRINT " DATA CHANGE ":RETURN
3210    LOCATE 62,4:COLOR 3:PRINT " DATA TRANSFER ":RETURN
3220 '======= Menu Data Area =======
3230    DATA "Edit Tone NO= 01"
3240    DATA "Play Song NO= 00","Send Song NO= 00"
3250    DATA "Play Channel= FF"
3260    DATA "Song Data Trans."
3270    DATA " Play Start !!  "," Play Stop !!   "
3280    DATA " Play Continue !"
3290    DATA "    ・・・・・・・・    ","    ・・・・・・・・    ","    ・・・・・・・・    "
3300    DATA "    ・・・・・・・・    "
3310    DATA "Random  (A) = 00"
3320    DATA "Random  (B) = 00"
3330    DATA "Random Clk  = 02"
3340    DATA "Random Sel. = 47"
3350    DATA "Rnd.F-No(L) = FF","Rnd.F-No(M) = FF","Rnd.F-No(H) = 01"
3360    DATA "Sweep Range = 07"
3370    DATA "F-No.(low)  = 00","F-No.(mid)  = 00","F-No.(high) = 00"
3380    DATA "Sweep Speed = 05"
3390    DATA "Bank NO.(A) = 60","Control SGA = 00"
3400    DATA "Bank NO.(B) = 64","Control SGB = 10"
3410    DATA "Att.Lev.(A) = F6","Dec.Lev.(A) = 00"
3420    DATA "Att.Lev.(B) = FF","Dec.Lev.(B) = 00"
3430    DATA "Att.Spd (A) = 45"
3440    DATA "Dec.Spd (A) = C6"
3450    DATA "Att.Spd (B) = 45"
3460    DATA "Dec.Spd (B) = C6"
3470    DATA "Rel.Lev.(A) = 00","Rel.Spd (A) = 95"
3480    DATA "Rel.Lev.(B) = 00","Rel.Spd (B) = 95"
3490    DATA "    ・・・・・・・・    ","    ・・・・・・・・    ","    ・・・・・・・・    "
3500    DATA "    ・・・・・・・・    "
3510    DATA "Harmonics Drawer","  Files Define  ","  Files Preset  "
3520    DATA "    ////////    "
3530    DATA "    ・・・・・・・・    ","    ・・・・・・・・    ","    ・・・・・・・・    "
3540    DATA "    ・・・・・・・・    "
3550    DATA "Wave Draw Editor","Harmonics Editor","Data File Trans."
3560    DATA "Data File Killer"
3570    DATA "    ・・・・・・・・    ","    ・・・・・・・・    ","    ・・・・・・・・    "
3580    DATA "    ・・・・・・・・    "
3590    DATA "File Name ListUp","PCM Data Plotter","Full PCM Convert"
3600    DATA "Sweep PCM Data !"
3610    DATA "    ・・・・・・・・    ","    ・・・・・・・・    ","    ・・・・・・・・    "
3620    DATA "    ・・・・・・・・    "
3630    DATA " Tone Data Load "
3640    DATA " Tone Data Save ","Old Wave Editor ","Return to Start "
3650    DATA 0,0,0,0,2,3,4,5,1,1,1,1
3660    DATA 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0:'Tone Data
3670    DATA 1,1,1,1,18,19,20,21
3680    DATA 1,1,1,1,6,7,8,9
3690    DATA 1,1,1,1,14,15,16,17
3700    DATA 1,1,1,1,10,11,12,13
3710 '======= Song Data Transfer =======
3720    XX=A(1,3)*16+A(0,3):IF XX>15 THEN BEEP:RETURN
3730    TEST=0:GOSUB 5580:IF TEST=1 THEN RETURN
3740    RETURN:' ###  FORMAT IS DELETED !!!
3750 '======= 128 Wave Mode =======
3760    LINE (0,0)-(350,399),0,BF
3770    LINE(9,17)-(282,274),5,B
3780     FOR I=1 TO 128
3790       HC(I)=0
3800     NEXT I
3810    CLS:GOSUB 5330:'Title
3820    COLOR 7
3830    LOCATE 2,18:PRINT "1st ------  Harmonics  ------ 128st"
3840    LOCATE 2,20:INPUT "Harmonics(1-128) or End(Return)";BNO
3850    IF BNO=0 THEN 3940
3860    IF BNO>129 THEN BEEP :GOTO 3810
3870    LOCATE 2,21:INPUT "POWER(0-128)";PW$
3880    IF PW$="" THEN 3900 ELSE PW=VAL(PW$)
3890    IF PW>128 OR PW<0 THEN BEEP :GOTO 3810
3900    HC(BNO)=PW
3910    LINE (BNO*2+15,274-128*2)-(BNO*2+17,274),0,BF
3920    LINE (BNO*2+15,274-PW*2)-(BNO*2+17,274),7,BF
3930    GOTO 3810
3940    LOCATE 2,21:INPUT "WAVE BOX No.(1<->6)";BNK
3950    IF BNK<1 OR BNK>6 THEN BEEP:GOTO 4140
3960    BNK=BNK-1
3970    LOCATE 2,23:COLOR 3:PRINT "     Now Computing !!!"
3980    MAXY=0
3990     FOR X=1 TO 256
4000       Y(BNK,X)=0
4010        FOR I=1 TO 128
4020          IF HC(I)=0 THEN 4040
4030          Y(BNK,X)=HC(I)*SIN(6.283*X/256*I)+Y(BNK,X)
4040        NEXT I
4050       IF MAXY5 THEN RETURN 1050
4300    ON N GOSUB 4330,5190,5070,5370,3750
4310    GOTO 4190
4320 '======= 14 Wave Make =======
4330    LINE (0,0)-(350,399),0,BF
4340    LINE(9,17)-(350,274),5,B
4350    PF(1)=256/2
4360     FOR I=2 TO 14
4370       PF(I)=0
4380     NEXT I
4390    LINE (24-5,18)-(24+2,274),7,BF
4400    CLS:GOSUB 5330
4410    COLOR 7
4420    LOCATE 2,18:PRINT "01 02 03 04 05 06 07 08 09 10 11 12 13 14"
4430    LOCATE 2,19:INPUT "F-No.(1<->14) or NEXT(RETURN)";BNO
4440    IF BNO=0 THEN 4520
4450    IF BNO>14 THEN BEEP :GOTO 4400
4460    LOCATE 2,20:INPUT "POWER(0<->128)";PW
4470    IF PW>128 OR PW<0 THEN BEEP :GOTO 4400
4480    PF(BNO)=PW
4490    LINE (BNO*24-5,274-128*2)-(BNO*24+2,274),0,BF
4500    LINE (BNO*24-5,274-PW*2)-(BNO*24+2,274),7,BF
4510    GOTO 4400
4520    LOCATE 2,20:INPUT "WAVE BOX No.(1<->6)";BNK
4530    IF BNK<1 OR BNK>6 THEN BEEP:GOTO 4700
4540    BNK=BNK-1
4550    MAXY=0
4560     FOR X=1 TO 256
4570       Y(BNK,X)=0
4580        FOR I=1 TO 14
4590          IF PF(I)=0 THEN 4610
4600          Y(BNK,X)=PF(I)*SIN(6.283*X/256*I)+Y(BNK,X)
4610        NEXT I
4620       IF MAXY6)";BNK:BNK=BNK-1
5100    IF BNK>5 OR BNK<0 THEN BEEP:RETURN
5110    INPUT "Save File Name";F$
5120    IF F$="" THEN BEEP:GOTO 5170
5130    OPEN "B:\data\"+F$+".WAV" FOR OUTPUT AS #1
5140     FOR X=1 TO 256
5150       PRINT #1,CHR$(Y(BNK,X)+128);
5160     NEXT X
5170    CLOSE #1
5180    RETURN
5190 '======= Wave File Load =======
5200    PRINT:PRINT "FILE NAME LIST":SY=CSRLIN+1
5210    FILES "B:\data\*.WAV":MY=CSRLIN-1
5220    INPUT "WAVE BOX No.(1<->6)";BNK:BNK=BNK-1
5230    IF BNK>5 OR BNK<0 THEN BEEP:RETURN
5240    GOSUB 2380
5250    IF F$="        " THEN BEEP:GOTO 5300
5260    OPEN "B:\data\"+F$+".WAV" FOR INPUT AS #1
5270     FOR X=1 TO 256
5280       Y(BNK,X)=ASC(INPUT$(1,#1))-128
5290     NEXT X
5300    CLOSE #1
5310    GOSUB 4850
5320    RETURN
5330 '======= Wave Editor Title =======
5340    LOCATE 25,0:COLOR 6
5350    PRINT "WAVE EDITOR V1.0"
5360    RETURN
5370 '======= Wave Data Transfer =======
5380    PRINT
5390    INPUT"WAVE BOX NO.(1<->6)=";BNK
5400    BNK=BNK-1
5410    IF BNK<0 OR BNK>5 THEN BEEP:RETURN
5420    INPUT"BANK NO.=(0<->255)";BANK
5430    IF BANK<0 OR BANK>255 THEN BEEP:RETURN
5440    COLOR 2
5450    PRINT "WAVE BOX NO.";:COLOR 5:PRINT BNK+1;:COLOR 2
5460    PRINT"-->BANK NO.";:COLOR 5:PRINT BANK
5470    COLOR 7
5480    OD=&H31:GOSUB 2590
5490    OD=BANK:GOSUB 2590
5500    SUM=BANK
5510     FOR I=1 TO 256
5520       IF Y(BNK,I)<0 THEN OD=128-Y(BNK,I) ELSE OD=Y(BNK,I):'NOT EXIST 128
5530       SUM=(SUM+OD) MOD 256:GOSUB 2590
5540     NEXT I
5550    OD=SUM:GOSUB 2590
5560    RD=&H0:GOSUB 2710
5570    RETURN
5580 '======= Tester Song Data Send Routine =======
5590    FOR I=0 TO 4:LOCATE 0,I:COLOR 7:PRINT STRING$(79," "):NEXT I
5600    LOCATE 5,0:COLOR 6:PRINT "Select Songs :"
5610    LOCATE 30,0:COLOR 5:PRINT "(1) TEST Songs"
5620    LOCATE 30,1:COLOR 5:PRINT "(2)  Songs"
5630    LOCATE 20,3:COLOR 3:PRINT "Select ---";
5640    SEL$=INKEY$:IF SEL$="" THEN 5640
5650    IF SEL$="1" THEN 5710
5660    FOR I=0 TO 4:LOCATE 0,I:COLOR 7:PRINT STRING$(79," "):NEXT I
5670    LOCATE 20,0:COLOR 5:PRINT "***OS MODE-->Tester MODE-->";
5680    COLOR 6:PRINT "Comand MODE";:COLOR 5:PRINT "***"
5690    COLOR 7,0:LOCATE 5,1:RESTORE 20:READ G$:PRINT G$
5700    RETURN
5710    TEST=1
5720    FOR I=0 TO 4:LOCATE 0,I:COLOR 7:PRINT STRING$(79," "):NEXT I
5730    LOCATE 5,0:COLOR 3:PRINT "Select Display Mode :"
5740    LOCATE 30,0:COLOR 4:PRINT "(1) Display Monitor ON"
5750    LOCATE 30,1:COLOR 4:PRINT "(2) Silent (High Speed)"
5760    LOCATE 20,3:COLOR 6:PRINT "Select ---";
5770    SEL$=INKEY$:IF SEL$="" THEN 5770
5780    IF SEL$<>"1" THEN 6130 ELSE 5910
5790    FOR I=0 TO 4:LOCATE 0,I:COLOR 7:PRINT STRING$(79," "):NEXT I
5800    LOCATE 0,0:COLOR 5:FILES "b:\data\*.nkk"
5810    LOCATE 0,0:COLOR 7:PRINT STRING$(79," "):LOCATE 20,4:COLOR 3
5820    INPUT "Input File Name (*****.DSP) ";FIL$
5830    FOR I=0 TO 4:LOCATE 0,I:COLOR 7:PRINT STRING$(79," "):NEXT I
5840    OPEN "b:\data\"+FIL$+".nkk" AS #1
5850    FIELD #1,128 AS DUM1$,128 AS DUM2$:GET #1
5860    LL=256*ASC(MID$(DUM1$,2,1))+ASC(LEFT$(DUM1$,1)):MM=INT(LL/256)+1
5870    GD$(1,1)=RIGHT$(DUM1$,128-16):GD$(1,2)=DUM2$
5880    FOR I=2 TO MM:GET #1:GD$(I,1)=DUM1$:GD$(I,2)=DUM2$:NEXT I
5890    NN=LL MOD 256:IF NN>127 THEN NN=NN-128:JJ=2 ELSE JJ=1:GD$(MM,2)=""
5900    GD$(MM,JJ)=LEFT$(GD$(MM,JJ),NN):CLOSE #1:RETURN
5910    GOSUB 5790
5920    OD=&H40:GOSUB 2590:SUM=XX
5930    OD=XX:GOSUB 2590
5940    LL=LL-16:COLOR 6
5950    OD=INT(LL/256):SUM=(SUM+OD) MOD 256:GOSUB 2590
5960    OD=INT(LL MOD 256):SUM=(SUM+OD) MOD 256:GOSUB 2590
5970    LOCATE 0,0:LINENO=0:FOR JJJ=1 TO MM
5980     FOR III=1 TO LEN(GD$(JJJ,1))
5990       AA$=HEX$(ASC(MID$(GD$(JJJ,1),III,1))):IF LEN(AA$)=1 THEN AA$="0"+AA$
6000       OD=VAL("&h"+AA$):SUM=(SUM+OD) MOD 256:GOSUB 2590
6010       PRINT AA$;" ";:LINENO=LINENO+1:IF LINENO MOD 25=0 THEN PRINT
6020       IF LINENO MOD 125=0 THEN LOCATE 0,0
6030     NEXT III
6040     FOR III=1 TO LEN(GD$(JJJ,2))
6050       AA$=HEX$(ASC(MID$(GD$(JJJ,2),III,1))):IF LEN(AA$)=1 THEN AA$="0"+AA$
6060       OD=VAL("&h"+AA$):SUM=(SUM+OD) MOD 256:GOSUB 2590
6070       PRINT AA$;" ";:LINENO=LINENO+1:IF LINENO MOD 25=0 THEN PRINT
6080       IF LINENO MOD 125=0 THEN LOCATE 0,0
6090     NEXT III
6100    NEXT JJJ
6110    OD=SUM:GOSUB 2590:RD=&H0:GOSUB 2710
6120    BEEP:GOTO 5660
6130    GOSUB 5790
6140    OD=&H40:GOSUB 2590:SUM=XX
6150    OD=XX:GOSUB 2590
6160    LL=LL-16:COLOR 6
6170    OD=INT(LL/256):SUM=(SUM+OD) MOD 256:GOSUB 2590
6180    OD=INT(LL MOD 256):SUM=(SUM+OD) MOD 256:GOSUB 2590
6190    LOCATE 10,2:COLOR 6:PRINT "Block Size =";MM
6200     FOR JJJ=1 TO MM
6210       FOR III=1 TO LEN(GD$(JJJ,1))
6220         AA$=HEX$(ASC(MID$(GD$(JJJ,1),III,1)))
6230         OD=VAL("&h"+AA$):SUM=(SUM+OD) MOD 256:GOSUB 2590
6240       NEXT III
6250       FOR III=1 TO LEN(GD$(JJJ,2))
6260         AA$=HEX$(ASC(MID$(GD$(JJJ,2),III,1)))
6270         OD=VAL("&h"+AA$):SUM=(SUM+OD) MOD 256:GOSUB 2590
6280       NEXT III
6290       LOCATE 35,2:COLOR 5:PRINT "Remain =";:COLOR 3:PRINT MM-JJJ
6300     NEXT JJJ
6310    OD=SUM:GOSUB 2590:RD=&H0:GOSUB 2710
6320    BEEP:GOTO 5660
6330 '======= Data File Transfer Routine =======
6340    COLOR 7:CLS 3
6350    LOCATE 25,0:INPUT "Send Tone NO ( 0 - 1F ) = ";ZZ$:XX=VAL("&h"+ZZ$)
6360    IF XX<>INT(XX) OR XX>31 THEN BEEP:GOTO 6350
6370    LOCATE 0,3:COLOR 7:PRINT "Select File : ":SY=CSRLIN+1
6380    FILES "b:\data\*.DAT":MY=CSRLIN-1
6390    GOSUB 2380
6400    IF F$="BASIC   " OR F$="" THEN 6590
6410    OPEN "b:\data\"+F$+".DAT" FOR INPUT AS #1
6420     FOR K=13 TO 40
6430       INPUT #1,WW
6440       A(1,K)=INT(WW/16)
6450       A(0,K)=WW MOD 16
6460     NEXT K
6470    CLOSE #1
6480    OD=&H30:GOSUB 2590
6490    OD=XX:GOSUB 2590
6500    RESTORE 1660
6510    SUM=XX
6520     FOR J=1 TO 28
6530       READ K:XX=A(1,K)*16+A(0,K):SUM=(SUM+XX) MOD 256
6540       OD=XX:GOSUB 2590
6550     NEXT J
6560     FOR J=29 TO 48:OD=0:GOSUB 2590:NEXT J
6570    OD=SUM:GOSUB 2590:RD=&H0:GOSUB 2710
6580    GOTO 6340
6590    RETURN 1050
6600 '======= Harmonics Data File Routine =======
6610    FOR K=1 TO 128:HC(K)=0:NEXT K
6620    CLS 3:COLOR 7:GOSUB 7250
6630    CLS
6640    LOCATE 2,0:COLOR 4:PRINT "<< Harmonics Data File Editor >>"
6650    LOCATE 10,2:COLOR 7
6660    PRINT "1.Data File Load"
6670    LOCATE 10,3:COLOR 7
6680    PRINT "2.Data File Save "
6690    LOCATE 10,4:COLOR 7
6700    PRINT "3.Data Caluclate/Save/Transfer"
6710    LOCATE 10,5:COLOR 7
6720    PRINT "4.Data Edit"
6730    LOCATE 10,6:COLOR 7
6740    PRINT "5.Rerurn"
6750    LOCATE 50,5:COLOR 6:PRINT "Input NO. ?";
6760    ANS$=INKEY$:N=VAL(ANS$):IF N>0 AND N<6 THEN 6770 ELSE 6760
6770    IF N=1 THEN GOSUB 6820:CLS:GOSUB 7250:GOTO 6640
6780    IF N=2 THEN GOSUB 6940:CLS:GOSUB 7250:GOTO 6640
6790    IF N=3 THEN GOSUB 7680:CLS:GOSUB 7250:GOTO 6640
6800    IF N=4 THEN GOSUB 7320:GOTO 6630
6810    RETURN 1050
6820 '======= Harmonics Data File Load =======
6830    COLOR 7:CLS 3
6840    LOCATE 25,0:COLOR 3:PRINT "Load Harmonics Data"
6850    LOCATE 0,3:COLOR 7:PRINT "File Name List":SY=CSRLIN+1
6860    FILES "b:\data\*.hcc":MY=CSRLIN-1
6870    GOSUB 2380
6880    IF F$="end     " THEN 6930
6890    OPEN "b:\data\"+F$+".hcc" FOR INPUT AS #1
6900     FOR K=1 TO 128
6910       INPUT #1,HC(K)
6920     NEXT K
6930    CLOSE #1:RETURN
6940 '======= Harmonics Data File Save =======
6950    COLOR 7:CLS 3
6960    LOCATE 25,0:COLOR 3:PRINT "Save Harmonics Data"
6970    LOCATE 0,3:COLOR 7:PRINT "File Name List":FILES "b:\data\*.hcc"
6980    INPUT "Save File Name=";F$
6990    IF F$="" THEN BEEP :GOTO 7040
7000    OPEN "b:\data\"+F$+".hcc" FOR OUTPUT AS #1
7010     FOR K=1 TO 128
7020       PRINT #1,HC(K)
7030     NEXT K
7040    CLOSE #1:RETURN
7050 '======= File Erase =======
7060    COLOR 7:CLS 3
7070    LOCATE 25,0:COLOR 6:PRINT "Erase File"
7080    LOCATE 0,3:COLOR 5:PRINT "Select : [ WAV / DAT / HCC / PCM";
7090    INPUT " / SRC / BIN ]";SF$
7100    IF SF$="wav" OR SF$="WAV" THEN 7170
7110    IF SF$="dat" OR SF$="DAT" THEN 7170
7120    IF SF$="hcc" OR SF$="HCC" THEN 7170
7130    IF SF$="pcm" OR SF$="PCM" THEN 7170
7140    IF SF$="src" OR SF$="SRC" THEN 7170
7150    IF SF$="bin" OR SF$="BIN" THEN 7170
7160    BEEP:GOTO 7240
7170    LOCATE 0,5:COLOR 7:PRINT "File Name List":SY=CSRLIN+1
7180    FILES "b:\data\*."+SF$:MY=CSRLIN-1
7190    GOSUB 2380
7200    IF F$="        " THEN 7240
7210    LOCATE 25,0:COLOR 2:BEEP:INPUT "   OK ? (y/n)  ";ANS$
7220    IF ANS$<>"Y" AND ANS$<>"y" THEN 7240
7230    KILL "b:\data\"+F$+"."+SF$
7240    RETURN 1050
7250 '======= Harmonics Data Draw =======
7260    LINE(54,141)-(586,399),0,BF
7270    LINE(54,141)-(586,399),5,B
7280     FOR K=1 TO 128
7290       LINE (K*4+60,398-HC(K)*2)-(K*4+62,398),7,BF
7300     NEXT K
7310    RETURN
7320 '======= Harmonics Data Edit =======
7330    CLS:K=1
7340    LOCATE 20,1:COLOR 6:PRINT "Harmonic NO. ="
7350    LOCATE 20,3:COLOR 6:PRINT "Harmonic Level ="
7360    GOSUB 7440:GOSUB 7530
7370    ED$=INKEY$
7380    IF ED$=CHR$(28) THEN GOSUB 7580:GOTO 7370:' Right
7390    IF ED$=CHR$(29) THEN GOSUB 7600:GOTO 7370:' Left
7400    IF ED$=CHR$(30) THEN GOSUB 7620:GOTO 7370:' Up
7410    IF ED$=CHR$(31) THEN GOSUB 7640:GOTO 7370:' Down
7420    IF ED$=CHR$(13) THEN 7660:' Return
7430    GOTO 7370
7440    LINE(54,115)-(586,135),0,BF
7450    LINE (K*4+55,115)-(K*4+67,125),4,BF
7460    LINE (K*4+58,116)-(K*4+64,130),3,BF
7470    LINE (K*4+60,118)-(K*4+62,135),6,BF
7480    LOCATE 35,1:COLOR 5:PRINT "     ";
7490    LOCATE 35,1:COLOR 5:PRINT K;
7500    LOCATE 37,3:COLOR 5:PRINT "     ";
7510    LOCATE 37,3:COLOR 5:PRINT HC(K);
7520    RETURN
7530    LINE (K*4+60,398-128*2)-(K*4+62,398),0,BF
7540    LINE (K*4+60,398-HC(K)*2)-(K*4+62,398),7,BF
7550    LOCATE 37,3:COLOR 5:PRINT "     ";
7560    LOCATE 37,3:COLOR 5:PRINT HC(K);
7570    RETURN
7580    IF K=128 THEN RETURN
7590    K=K+1:GOSUB 7440:RETURN
7600    IF K=1 THEN RETURN
7610    K=K-1:GOSUB 7440:RETURN
7620    IF HC(K)=128 THEN RETURN
7630    HC(K)=HC(K)+1:GOSUB 7530:RETURN
7640    IF HC(K)=0 THEN RETURN
7650    HC(K)=HC(K)-1:GOSUB 7530:RETURN
7660    LINE(54,115)-(586,135),0,BF
7670    RETURN
7680 '======= Wave Compute/Save/Transfer =======
7690    BNK=0:CLS
7700    LOCATE 25,2:COLOR 3:PRINT "!!! Now Computing !!!"
7710    MAXY=0
7720     FOR X=1 TO 256
7730       LINE (X*2+60,130)-(X*2+61,135),6,BF
7740       Y(BNK,X)=0
7750        FOR I=1 TO 128
7760          IF HC(I)=0 THEN 7780
7770          Y(BNK,X)=HC(I)*SIN(6.283*X/256*I)+Y(BNK,X)
7780        NEXT I
7790       IF MAXY"y" AND ANS$<>"Y" THEN 8110
7970    COLOR 7:LOCATE 5,4
7980    INPUT"BANK NO.=(0<->255)";BANK
7990    IF BANK<0 OR BANK>255 THEN BEEP:GOTO 7970
8000    LOCATE 2,7:COLOR 4:PRINT "Now Transfer Start !!"
8010    OD=&H31:GOSUB 2590
8020    OD=BANK:GOSUB 2590
8030    SUM=BANK
8040     FOR I=1 TO 256
8050       IF Y(BNK,I)<0 THEN OD=128-Y(BNK,I) ELSE OD=Y(BNK,I):'NOT EXIST 128
8060       IF OD=256 THEN OD=255
8070       SUM=(SUM+OD) MOD 256:GOSUB 2590
8080     NEXT I
8090    OD=SUM:GOSUB 2590
8100    RD=&H0:GOSUB 2710
8110    CLS
8120    LOCATE 0,2:COLOR 5:PRINT "Wave Data File Save ? (Y/N)"
8130    ANS$=INKEY$:IF ANS$="" THEN 8130
8140    IF ANS$<>"y" AND ANS$<>"Y" THEN CLS 3:RETURN
8150    CLS 3:COLOR 7:PRINT:PRINT "File Name List":FILES "B:\data\*.WAV"
8160    PRINT:INPUT "Save File Name";F$
8170    IF F$="" THEN RETURN
8180    OPEN "B:\data\"+F$+".WAV" FOR OUTPUT AS #1
8190     FOR X=1 TO 256
8200       PRINT #1,CHR$(Y(BNK,X)+128);
8210     NEXT X
8220    CLOSE #1
8230    RETURN
8240 '======= Wave Drawing Editor =======
8250    BNK=0:FOR X=1 TO 256:Y(BNK,X)=0:NEXT X
8260    CLS 3:COLOR 7:GOSUB 8680
8270    LOCATE 2,0:COLOR 4:PRINT "<< Wave Data Drawing Editor >>"
8280    LOCATE 10,2:COLOR 7
8290    PRINT "1.Data File Load"
8300    LOCATE 10,3:COLOR 7
8310    PRINT "2.Data File Save "
8320    LOCATE 10,4:COLOR 7
8330    PRINT "3.Data Transfer"
8340    LOCATE 10,5:COLOR 7
8350    PRINT "4.Data Drawing Edit"
8360    LOCATE 10,6:COLOR 7
8370    PRINT "5.Rerurn"
8380    LOCATE 50,5:COLOR 3:PRINT "Input NO. ?";
8390    ANS$=INKEY$:N=VAL(ANS$):IF N>0 AND N<6 THEN 8400 ELSE 8390
8400    IF N=1 THEN GOSUB 8450:GOSUB 8680:GOTO 8270
8410    IF N=2 THEN GOSUB 8570:GOSUB 8680:GOTO 8270
8420    IF N=3 THEN GOSUB 9180:CLS:GOTO 8270
8430    IF N=4 THEN GOSUB 8760:GOSUB 8680:GOTO 8270
8440    RETURN 1050
8450 '======= Wave Data File Load =======
8460    COLOR 7:CLS 3
8470    LOCATE 25,0:COLOR 3:PRINT "Load Wave Data"
8480    LOCATE 0,3:COLOR 7:PRINT "File Name List":SY=CSRLIN+1
8490    FILES "b:\data\*.wav":MY=CSRLIN-1
8500    GOSUB 2380
8510    IF F$="end     " OR F$="" THEN RETURN
8520    OPEN "b:\data\"+F$+".wav" FOR INPUT AS #1
8530     FOR X=1 TO 256
8540       Y(BNK,X)=ASC(INPUT$(1,#1))-128
8550     NEXT X
8560    CLOSE #1:RETURN
8570 '======= Wave Data File Save =======
8580    COLOR 7:CLS 3
8590    LOCATE 25,0:COLOR 3:PRINT "Save Wave Data"
8600    LOCATE 0,3:COLOR 7:PRINT "File Name List":FILES "b:\data\*.wav"
8610    INPUT "Save File Name=";F$
8620    IF F$="" THEN RETURN
8630    OPEN "b:\data\"+F$+".wav" FOR OUTPUT AS #1
8640     FOR K=1 TO 256
8650       PRINT #1,CHR$(Y(BNK,K)+128);
8660     NEXT K
8670    CLOSE #1:RETURN
8680 '======= Wave Data Draw =======
8690    CLS
8700    LINE(54,270-129)-(586,270+129),0,BF
8710    LINE(54,270-129)-(586,270+129),5,B
8720     FOR K=1 TO 256
8730       CIRCLE (K*2+60,270-Y(BNK,K)),1,6
8740     NEXT K
8750    RETURN
8760 '======= Wave Draw Editing =======
8770    CLS:K=1
8780    LOCATE 20,1:COLOR 7:PRINT "Point Address ="
8790    LOCATE 20,3:COLOR 7:PRINT "Point Level ="
8800    GOSUB 8880:GOSUB 9010
8810    ED$=INKEY$
8820    IF ED$=CHR$(28) THEN GOSUB 9080:GOTO 8810:' Right
8830    IF ED$=CHR$(29) THEN GOSUB 9100:GOTO 8810:' Left
8840    IF ED$=CHR$(30) THEN GOSUB 9120:GOTO 8810:' Up
8850    IF ED$=CHR$(31) THEN GOSUB 9140:GOTO 8810:' Down
8860    IF ED$=CHR$(13) THEN 9160:' Return
8870    GOTO 8810
8880    LINE(54,115)-(586,135),0,BF
8890    LINE (K*2+55,115)-(K*2+67,125),4,BF
8900    LINE (K*2+58,116)-(K*2+64,130),3,BF
8910    LINE (K*2+60,118)-(K*2+62,135),6,BF
8920    CIRCLE (OLDX,OLDY),2,0
8930    CIRCLE (K*2+60,270-Y(BNK,K)),1,6
8940    CIRCLE (K*2+60,270-Y(BNK,K)),2,4
8950    OLDX=K*2+60:OLDY=270-Y(BNK,K)
8960    LOCATE 35,1:COLOR 5:PRINT "    ";
8970    LOCATE 35,1:COLOR 5:PRINT K;
8980    LOCATE 33,3:COLOR 5:PRINT "     ";
8990    LOCATE 33,3:COLOR 5:PRINT Y(BNK,K)+128
9000    RETURN
9010    CIRCLE (OLDX,OLDY),2,0
9020    CIRCLE (K*2+60,270-Y(BNK,K)),1,6
9030    CIRCLE (K*2+60,270-Y(BNK,K)),2,4
9040    OLDX=K*2+60:OLDY=270-Y(BNK,K)
9050    LOCATE 33,3:COLOR 5:PRINT "     ";
9060    LOCATE 33,3:COLOR 5:PRINT Y(BNK,K)+128
9070    RETURN
9080    IF K=256 THEN RETURN
9090    K=K+1:GOSUB 8880:RETURN
9100    IF K=1 THEN RETURN
9110    K=K-1:GOSUB 8880:RETURN
9120    IF Y(BNK,K)=127 THEN RETURN
9130    Y(BNK,K)=Y(BNK,K)+1:GOSUB 9010:RETURN
9140    IF Y(BNK,K)=<-127 THEN RETURN
9150    Y(BNK,K)=Y(BNK,K)-1:GOSUB 9010:RETURN
9160    LINE(54,115)-(586,135),0,BF
9170    RETURN
9180 '======= Drawn Wave Transfer =======
9190    CLS:COLOR 7:LOCATE 2,0
9200    INPUT"BANK NO.=(0<->255)";BANK
9210    IF BANK<0 OR BANK>255 THEN BEEP:GOTO 9190
9220    LOCATE 20,2:COLOR 4:PRINT "Now Transfer Start !!"
9230    OD=&H31:GOSUB 2590
9240    OD=BANK:GOSUB 2590
9250    SUM=BANK
9260     FOR I=1 TO 256
9270       IF Y(BNK,I)<0 THEN OD=128-Y(BNK,I) ELSE OD=Y(BNK,I):'NOT EXIST 128
9280       IF OD=256 THEN OD=255
9290       SUM=(SUM+OD) MOD 256:GOSUB 2590
9300     NEXT I
9310    OD=SUM:GOSUB 2590
9320    RD=&H0:GOSUB 2710
9330    RETURN
9340 '======= File Name List Up =======
9350    COLOR 7:CLS 3
9360    INPUT "List Up File Name Suffix =";F$
9370    IF F$="" THEN F$="*"
9380    LOCATE 0,2:COLOR 7:FILES "b:\data\*."+F$
9390    IF INKEY$="" THEN 9390
9400    RETURN 1050
9410 '======= Sweep PCM Data Make Routine =======
9420    FOR K=1 TO 128:HC(K)=0:NEXT K
9430    CLS 3:COLOR 7:GOSUB 7250
9440    CLS
9450    LOCATE 2,0:COLOR 4:PRINT "<< Sweep PCM Data File Maker >>"
9460    LOCATE 10,2:COLOR 7
9470    PRINT "1.  Data File Load"
9480    LOCATE 10,3:COLOR 7
9490    PRINT "2.  Data File Save "
9500    LOCATE 10,4:COLOR 7
9510    PRINT "3.  Data Caluclate to "
9520    LOCATE 10,5:COLOR 7
9530    PRINT "4.  Data Transfer to DSP"
9540    LOCATE 10,6:COLOR 7
9550    PRINT "5. Source PCM Data File Converter to "
9560    LOCATE 10,7:COLOR 7
9570    PRINT "6. Rerurn"
9580    LOCATE 55,7:COLOR 6:PRINT "Input NO. ?";
9590    ANS$=INKEY$:N=VAL(ANS$):IF N>0 AND N<7 THEN 9600 ELSE 9590
9600    IF N=1 THEN GOSUB 6820:CLS:GOSUB 7250:GOTO 9450
9610    IF N=2 THEN GOSUB 6940:CLS:GOSUB 7250:GOTO 9450
9620    IF N=3 THEN GOSUB 9670:CLS:GOSUB 7250:GOTO 9450
9630    IF N=4 THEN GOSUB 10130:CLS:GOSUB 7250:GOTO 9450
9640    IF N=5 THEN GOSUB 10450:CLS 3:GOTO 9450
9650    RETURN 1050
9660    RETURN : 'DUMMY
9670 '======= Sweep PCM Data Compute/Save =======
9680    BNK=0:CLS
9690    LOCATE 25,2:COLOR 3:PRINT "!!! Now Computing !!!"
9700    MAXY=0
9710     FOR X=1 TO 256
9720       LINE (X*2+60,130)-(X*2+61,135),6,BF
9730       Y(BNK,X)=0
9740        FOR I=1 TO 128
9750          IF HC(I)=0 THEN 9770
9760          Y(BNK,X)=HC(I)*SIN(6.283*X/256*I)+Y(BNK,X)
9770        NEXT I
9780       IF MAXY Data File Save ? (Y/N)"
9950    ANS$=INKEY$:IF ANS$="" THEN 9950
9960    IF ANS$<>"y" AND ANS$<>"Y" THEN CLS 3:RETURN
9970    CLS 3:COLOR 7:PRINT:PRINT "File Name List":FILES "B:\data\*.PCM"
9980    PRINT:INPUT "Save File Name";F$
9990    IF F$="" OR LEN(F$)>5 THEN BEEP:GOTO 9930
10000    COLOR 7:PRINT:INPUT "PCM Page NO (0-255)";FGH
10010    IF FGH>255 THEN BEEP:GOTO 9930
10020    PRINT:COLOR 5:PRINT "Save OK ? (Y/N)"
10030    ANS$=INKEY$:IF ANS$="" THEN 10030
10040    IF ANS$<>"y" AND ANS$<>"Y" THEN CLS 3:RETURN
10050    FG$=HEX$(FGH):IF LEN(FG$)=1 THEN FG$="0"+FG$
10060    OPEN "B:\data\"+F$+FG$+".PCM" FOR OUTPUT AS #1
10070    PRINT #1,FG$
10080     FOR X=1 TO 256
10090       PRINT #1,CHR$(Y(BNK,X)+128);
10100     NEXT X
10110    CLOSE #1
10120    GOTO 10000
10130 '======= Sweep PCM Data Transfer to DSP =======
10140    COLOR 7:CLS 3:BNK=0
10150    INPUT "List Up  File Name =";F$
10160    IF F$="" THEN F$="*"
10170    LOCATE 0,2:COLOR 7:FILES "b:\data\"+F$+"*.PCM"
10180    PRINT:INPUT "Transfer  File Name =";F$
10190    INPUT "Max Bank NO (1,2,4,8,16,32,64,128,256) =";BKMAX
10200    PRINT:COLOR 3:PRINT "Transfer to DSP OK ? (Y/N)"
10210    ANS$=INKEY$:IF ANS$="" THEN 10210
10220    IF ANS$="y" OR ANS$="Y" THEN 10230 ELSE RETURN
10230    CLS:LOCATE 2,4:COLOR 4:PRINT "Now Transfer Start !!"
10240    LOCATE 0,9:COLOR 6
10250    FOR JKJ=0 TO BKMAX-1
10260      FG$=HEX$(JKJ):IF LEN(FG$)=1 THEN FG$="0"+FG$
10270      OPEN "b:\data\"+F$+FG$+".PCM" FOR INPUT AS #1
10280      INPUT #1,DUM$:PRINT DUM$;" , ";
10290      FOR X=1 TO 256
10300        Y(BNK,X)=ASC(INPUT$(1,#1))-128
10310      NEXT X
10320      CLOSE #1
10330      OD=&H31:GOSUB 2590
10340      OD=JKJ:GOSUB 2590
10350      SUM=JKJ
10360       FOR I=1 TO 256
10370         IF Y(BNK,I)<0 THEN OD=128-Y(BNK,I) ELSE OD=Y(BNK,I):'NOT EX. 128
10380         IF OD=256 THEN OD=255
10390         SUM=(SUM+OD) MOD 256:GOSUB 2590
10400       NEXT I
10410      OD=SUM:GOSUB 2590
10420      RD=&H0:GOSUB 2710
10430    NEXT JKJ
10440    RETURN
10450 '======= Source PCM Data Covert to  File =======
10460    COLOR 7:CLS 3:BNK=0
10470    LOCATE 0,2:FILES "b:\data\*.PCM"
10480    PRINT:INPUT "Output  File Name =";FA$
10490    CLS:LOCATE 0,2:FILES "b:*.SRC"
10500    PRINT:INPUT "Input  File Name =";FB$
10510    PRINT:INPUT "Start Bank NO (1-256) =";BKMIN
10520    PRINT:INPUT "Max Bank NO (1,2,4,8,32,64,128,256) =";BKMAX
10530    PRINT:PRINT:COLOR 3:PRINT "File Make OK ? (Y/N)"
10540    ANS$=INKEY$:IF ANS$="" THEN 10540
10550    IF ANS$="y" OR ANS$="Y" THEN 10560 ELSE RETURN
10560    FOR JKJ=BKMIN-1 TO BKMAX-1
10570      CLS 3:LOCATE 2,2:COLOR 3:PRINT "Now Compute Bank =";JKJ+1
10580      FG$=STR$(JKJ+1):FG$=RIGHT$(FG$,LEN(FG$)-1)
10590      OPEN "b:\data\"+FB$+FG$+".SRC" FOR INPUT AS #1
10600      INPUT #1,INDT$(1):IF LEFT$(INDT$(1),2)="/*" THEN 10600
10610      FOR X=2 TO 127:INPUT #1,INDT$(X):NEXT X
10620      CLOSE #1:PRINT:PRINT:COLOR 5
10630      FOR X=1 TO 128
10640        HC(X)=VAL(RIGHT$(INDT$(X),10))
10650        PRINT HC(X);
10660      NEXT X
10670      MAXY=0
10680      FOR X=1 TO 256
10690        LINE (X*2+60,330)-(X*2+61,435),6,BF
10700        Y(BNK,X)=0
10710        FOR I=1 TO 128
10720          IF HC(I)=0 THEN 10740
10730          Y(BNK,X)=HC(I)*SIN(6.283*X/256*I)+Y(BNK,X)
10740        NEXT I
10750        IF MAXY File Name =";FB$
11030    PRINT:INPUT "Input File Size (Byte) =";DTSIZE
11040    IF INT(DTSIZE/256)<>DTSIZE/256 THEN BEEP:GOTO 11010
11050    CLS:LOCATE 0,2:FILES "b:\data\*.PCM"
11060    PRINT:INPUT "Output  File Name =";FA$
11070    PRINT "Max Bank NO =";DTSIZE/256
11080    PRINT:PRINT:COLOR 3:PRINT "File Make OK ? (Y/N)"
11090    ANS$=INKEY$:IF ANS$="" THEN 11090
11100    IF ANS$<>"y" AND ANS$<>"Y" THEN RETURN 1050
11110    CLS 3:OPEN "b:\data\"+FB$+".BIN" FOR INPUT AS #1
11120    FOR JKJ=0 TO DTSIZE/256-1
11130      LOCATE 2,2:COLOR 3:PRINT "Now Compute Bank =";JKJ+1
11140      FOR X=1 TO 256
11150        DUMINP=ASC(INPUT$(1,#1))-128
11160        IF DUMINP<0 THEN Y(BNK,X)=DUMINP+127 ELSE Y(BNK,X)=-DUMINP
11170      NEXT X
11180      WM=200:OFST=330
11190      LINE (OFST+10-1,WM-128)-(OFST+10+256,WM+128),0,BF
11200      LINE (OFST+10,WM)-(OFST+266,WM),7
11210      LINE (OFST+10-1,WM-128)-(OFST+10+256,WM+128),4,B
11220      FOR X=1 TO 256
11230        PSET (OFST+X+10,WM-Y(BNK,X))
11240      NEXT X
11250      FG$=HEX$(JKJ):IF LEN(FG$)=1 THEN FG$="0"+FG$
11260      OPEN "B:\data\"+FA$+FG$+".PCM" FOR OUTPUT AS #2
11270      PRINT #2,FG$
11280      FOR X=1 TO 256
11290        PRINT #2,CHR$(Y(BNK,X)+128);
11300      NEXT X
11310      CLOSE #2
11320    NEXT JKJ
11330    CLOSE #1
11340    RETURN 1050
11350 '======= PCM Data Plotter =======
11360    COLOR 7:CLS 3:WM=250
11370    LOCATE 0,2:FILES "b:\data\*.BIN"
11380    PRINT:INPUT "Plot  File Name =";FB$
11390    IF FB$="" THEN RETURN 1050
11400    PRINT:INPUT "File Size (Byte) =";DTSIZE
11410    CLS 3
11420    LOCATE 18,1:COLOR 5:PRINT"File Name =  < ";FB$;".BIN >"
11430    LOCATE 18,3:PRINT"Data Size  = ";DTSIZE;" bytes"
11440    LINE (0,WM)-(639,WM),7
11450    LINE (0,WM-128)-(639,WM+128),4,B
11460    OPEN "b:\data\"+FB$+".BIN" FOR INPUT AS #1
11470    FOR JKJ=0 TO DTSIZE-1
11480      DUMINP=ASC(INPUT$(1,#1))-128
11490      IF DUMINP<0 THEN PLOTY=DUMINP+127 ELSE PLOTY=-DUMINP
11500      PSET (INT(640*(JKJ+1)/DTSIZE),WM-PLOTY),6
11510    NEXT JKJ
11520    CLOSE #1
11530    IF INKEY$="" THEN 11530 ELSE 11360
11540 '======= Files Define =======
11550    OPEN "B:\data\files.def" FOR INPUT AS #1
11560    INPUT #1,DFCOUNT
11570    FOR I=1 TO DFCOUNT
11580      INPUT #1,DFT(I)
11590      INPUT #1,DF$(I)
11600    NEXT I
11610    CLOSE #1
11620    CLS 3
11630    COLOR 7:PRINT
11640    FOR I=1 TO DFCOUNT
11650      PRINT "          (";I;")    ";
11660      COLOR 5:PRINT DF$(I);:COLOR 7:NF$=DF$(I)
11670      IF RIGHT$(NF$,4)=".dat" THEN PRINT "   ,   Tone NO. = ";DFT(I);
11680      IF RIGHT$(NF$,4)=".DAT" THEN PRINT "   ,   Tone NO. = ";DFT(I);
11690      IF RIGHT$(NF$,4)=".wav" THEN PRINT "   ,   Bank NO. = ";DFT(I);
11700      IF RIGHT$(NF$,4)=".WAV" THEN PRINT "   ,   Bank NO. = ";DFT(I);
11710      PRINT
11720    NEXT I
11730    COLOR 3:PRINT:PRINT "Select : ";
11740    COLOR 6:PRINT "(1) New File / (2) Delete File / (3) Escape"
11750    A$=INKEY$:IF A$="" THEN 11750
11760    IF A$="3" THEN 12060
11770    IF A$="1" THEN 11790
11780    IF A$="2" THEN 11940 ELSE 11750
11790    COLOR 4:PRINT:PRINT "   << New File Define >>"
11800    COLOR 7:INPUT "                          New File Name = ";NF$
11810    IF RIGHT$(NF$,4)=".dat" THEN 11850
11820    IF RIGHT$(NF$,4)=".DAT" THEN 11850
11830    IF RIGHT$(NF$,4)=".wav" THEN 11870
11840    IF RIGHT$(NF$,4)=".WAV" THEN 11870 ELSE BEEP:GOTO 11800
11850    COLOR 6:INPUT "                          Tone NO. = ";DFT(DFCOUNT+1)
11860    IF DFT(DFCOUNT+1)>31 THEN BEEP:GOTO 11850 ELSE GOTO 11890
11870    COLOR 6:INPUT "                          Bank NO. = ";DFT(DFCOUNT+1)
11880    IF DFT(DFCOUNT+1)>255 THEN BEEP:GOTO 11870
11890    FOR I=1 TO DFCOUNT
11900      IF NF$=DF$(I) THEN BEEP:GOTO 11800
11910    NEXT I
11920    DFCOUNT=DFCOUNT+1:DF$(DFCOUNT)=NF$
11930    GOTO 11630
11940    COLOR 4:PRINT:PRINT "   << File Delete >>"
11950    COLOR 7:INPUT "                          Delete File NO. = ";KF
11960    IF KF>DFCOUNT OR KF<>INT(KF) THEN BEEP:GOTO 11950
11970    COLOR 2:BEEP:PRINT:PRINT "               !!!  Delete OK ? (Y/N)  !!!"
11980    A$=INKEY$:IF A$="" THEN 11980
11990    IF A$<>"y" AND A$<>"Y" THEN 11630
12000    IF KF=DFCOUNT THEN DFCOUNT=DFCOUNT-1:GOTO 11630
12010    FOR I=KF TO DFCOUNT-1
12020      DF$(I)=DF$(I+1)
12030      DFT(I)=DFT(I+1)
12040    NEXT I
12050    DFCOUNT=DFCOUNT-1:GOTO 11630
12060    COLOR 4:BEEP:PRINT:PRINT:PRINT "        Define File Save ? (Y/N)"
12070    A$=INKEY$:IF A$="" THEN 12070
12080    IF A$<>"y" AND A$<>"Y" THEN 12160
12090    OPEN "B:\data\files.def" FOR OUTPUT AS #1
12100    PRINT #1,DFCOUNT
12110    FOR I=1 TO DFCOUNT
12120      PRINT #1,DFT(I)
12130      PRINT #1,DF$(I)
12140    NEXT I
12150    CLOSE #1
12160    RETURN 1050
12170    OPEN "B:\data\files.def" FOR OUTPUT AS #1
12180    PRINT #1,5
12190    FOR I=1 TO 5
12200      PRINT #1,1
12210      PRINT #1,"dummy.dat"
12220    NEXT I
12230    CLOSE #1
12240 '======= Files Preset =======
12250    CLS 3:COLOR 7
12260    PRINT " <<< Preset File Read >>>":PRINT:COLOR 6
12270    OPEN "B:\data\files.def" FOR INPUT AS #1
12280    INPUT #1,DFCOUNT
12290    FOR I=1 TO DFCOUNT
12300      INPUT #1,DFT(I)
12310      INPUT #1,DF$(I)
12320      PRINT "          File #";I;" = ";DF$(I)
12330    NEXT I
12340    CLOSE #1:PRINT:PRINT
12350    FOR PRS=1 TO DFCOUNT
12360      IF RIGHT$(DF$(PRS),3)="dat" THEN 12410
12370      IF RIGHT$(DF$(PRS),3)="DAT" THEN 12410
12380      IF RIGHT$(DF$(PRS),3)="wav" THEN 12620
12390      IF RIGHT$(DF$(PRS),3)="WAV" THEN 12620
12400      GOTO 12790
12410     XX=DFT(PRS)
12420      COLOR 4:PRINT
12430      PRINT " < ";DF$(PRS);" > is Read , Transfer to DSP : Tone NO =";XX
12440      OPEN "b:\data\"+DF$(PRS) FOR INPUT AS #1
12450      FOR K=13 TO 40
12460        INPUT #1,WW
12470        A(1,K)=INT(WW/16)
12480        A(0,K)=WW MOD 16
12490      NEXT K
12500      CLOSE #1
12510      OD=&H30:GOSUB 2590
12520      OD=XX:GOSUB 2590
12530      RESTORE 1660
12540      SUM=XX
12550      FOR J=1 TO 28
12560        READ K:XX=A(1,K)*16+A(0,K):SUM=(SUM+XX) MOD 256
12570        OD=XX:GOSUB 2590
12580      NEXT J
12590      FOR J=29 TO 48:OD=0:GOSUB 2590:NEXT J
12600      OD=SUM:GOSUB 2590:RD=&H0:GOSUB 2710
12610      GOTO 12790
12620     BANK=DFT(PRS)
12630      COLOR 3:PRINT
12640      PRINT " < ";DF$(PRS);" > is Read , Transfer to DSP : Bank NO =";BANK
12650      OPEN "b:\data\"+DF$(PRS) FOR INPUT AS #1
12660      FOR X=1 TO 256
12670        Y(BNK,X)=ASC(INPUT$(1,#1))-128
12680      NEXT X
12685      CLOSE #1
12690      OD=&H31:GOSUB 2590
12700      OD=BANK:GOSUB 2590
12710      SUM=BANK
12720      FOR I=1 TO 256
12730        IF Y(BNK,I)<0 THEN OD=128-Y(BNK,I) ELSE OD=Y(BNK,I):'NOT EXIST 128
12740        IF OD=256 THEN OD=255
12750        SUM=(SUM+OD) MOD 256:GOSUB 2590
12760      NEXT I
12770      OD=SUM:GOSUB 2590
12780      RD=&H0:GOSUB 2710
12790    NEXT PRS
12800    RETURN 1050

Z80アセンブラ その1 液晶表示パネル

;-----------------------------------------------------------------------
;	LCD System Message Box : Feb.1993
;-----------------------------------------------------------------------

;##### RAM Map #####
	dseg
	org	0000h
rx_fifo		ds	16384
rx_top		ds	2
rx_end		ds	2
rsb		ds	1
dcb		ds	1
channel		ds	1
keyno		ds	1
disp_main	ds	1
disp_sub	ds	1
sw_status	ds	2
mon_write	ds	1
mon_erase	ds	1
ddd		ds	5
disp_data	ds	1

;##### I/O Map, Defines #####
	cseg
s_gen		equ	0017h
sio_a		equ	0018h
sio_b		equ	001ah
pio_a		equ	001ch
pio_b		equ	001eh
lcd		equ	0080h
tail		equ	0000h

;##### MACRO #####
clear	macro	@1
	xor	a
	ld	(@1),a
	endm
move	macro	@1,@2
	ld	a,@2
	ld	(@1),a
	endm
inc_a	macro	@1
	ld	a,(@1)
	inc	a
	ld	(@1),a
	endm
inc_chk	macro	@1,@2
	inc_a	@1
	cp	@2
	jp	c,99$
	clear	@1
99$:
	endm
brc	macro	@1,@2
	cp	@1
	jp	c,@2
	endm
brz	macro	@1,@2
	cp	@1
	jp	z,@2
	endm
io_set	macro	@1,@2
	ld	a,@2
	out	(@1+1),a
	endm
io_put	macro	@1,@2
	ld	a,@2
	out	(@1+0),a
	endm
lcd_set	macro	@1
	ld	a,@1
	out	(lcd+1),a
	endm
lcd_out	macro	@1
	out	(lcd),a
	endm
lcd_put	macro	@1
	ld	a,@1
	lcd_out
	endm
lcd_mes	macro	@1,@2,@3
	ld	hl,@1*40+@2-41
	ld	de,@3
	call	lcd_disp_mess
	endm
lcd_add	macro
	lcd_set	46h			; Address Set
	lcd_put	l
	lcd_put	h
	lcd_set	42h			; Data Set
	endm
add_set	macro	@1
	ld	a,(@1)
	add	a,a
	ld	e,a
	ld	d,0
	ld	hl,monitor_add
	add	hl,de
	ld	e,(hl)
	inc	hl
	ld	d,(hl)
	ex	de,hl
	endm

;##### RESET #####
	org	0000h
	ld      sp,0ffffh
	di
	jp      main

;##### INT / NMI #####
	org     0020h
	dw	_midi_
_midi_:
	ex	af,af'
	exx
	ld	de,(rx_top)
	ld	a,10000000b
	or	d
	ld	h,a
	ld	l,e
	in	a,(sio_a)
	ld	(hl),a
	inc	de
	res	6,d
	ld	(rx_top),de
	exx
	ex	af,af'
	ei
	reti
	org     0066h
	retn

;##### Initialize --> Main Loop #####
main:
	ld      hl,08000h
	ld      a,0f0h
_ram_clear_loop:
	ld      (hl),0
	inc     hl
	cp	h
	jp	nc,_ram_clear_loop
	call	lcd_initial_set
; 
	io_set	pio_a,0cfh		; Mode 3
	io_set	pio_a,00000000b		; 0:Out / 1:In
	io_set	pio_a,007h		; Interrupt Disable
	io_set	pio_b,0cfh		; Mode 3
	io_set	pio_b,00000011b		; 0:Out / 1:In
	io_set	pio_b,007h		; Interrupt Disable
	io_put	pio_b,0
	io_put	pio_a,0ffh		; LED All Off
	io_put	pio_b,80h
	io_put	pio_b,0
; 
	io_put	s_gen,00000000b		; Clock Generator
	io_put	0f0h,003h		; WDT Disable
	io_put	0f0h,0b1h		; WDT Disable
	io_put	0f4h,001h		; Daisy Chain
	io_put	010h,0			; CTC #1 Int. Disable
	io_put	011h,0			; CTC #2 Int. Disable
	io_put	012h,0			; CTC #3 Int. Disable
	io_put	013h,0			; CTC #4 Int. Disable
	io_set	sio_b,00011000b		; Channel Reset B
	io_set	sio_b,1			; Resister Point = 1B
	io_set	sio_b,0			; Interrupt Mode
	io_set	sio_b,2			; Resister Point = 2B
	io_set	sio_b,20h		; Reset Address
	xor	a
	ld	i,a			; High Address
	io_set	sio_a,00011000b		; Channel Reset A
	io_set	sio_a,4			; Resister Point = 4
	io_set	sio_a,10000100b		; Mode
	io_set	sio_a,1			; Resister Point = 1
	io_set	sio_a,00011000b		; Interrupt Mode
	io_set	sio_a,3			; Resister Point = 3
	io_set	sio_a,11000001b		; Receive Start
	im	2
	call	init_menu
	ei
loop:
	call	sw_scan
	call	midi_check
	jp	loop

;##### MIDI #####
midi_check:
	ld	de,(rx_end)
	ld	hl,(rx_top)
	and	a			; CY <-- 0
	sbc	hl,de
	ret	z
	ld	a,10000000b
	or	d
	ld	h,a
	ld	l,e
	ld	b,(hl)			; [B] = Rx Data
	inc	de
	res	6,d
	ld	(rx_end),de
	ld	a,b			; **********
	call	led_display		; **********
	ld	a,(disp_main)
	cp	1
	jp	nz,_non_momitor
	ld	a,b
	cp	0f0h
	ret	nc
	add_set	mon_erase
	lcd_add
	ld	a,' '
	lcd_out
	lcd_out
	inc_chk	mon_erase,104
	add_set	mon_write
	lcd_add
	ld	a,b
	srl	a
	srl	a
	srl	a
	srl	a
	ld	l,a
	ld	h,0
	ld	de,hex_table
	add	hl,de
	ld	a,(hl)
	lcd_out
	ld	a,b
	and	00001111b
	ld	l,a
	ld	h,0
	ld	de,hex_table
	add	hl,de
	ld	a,(hl)
	lcd_out
	inc_chk	mon_write,104
	ret
hex_table:
	db	'0123456789ABCDEF'
monitor_add:
	dw	1,4,7,10,13,16,19,22,25,28,31,34,37
	dw	41,44,47,50,53,56,59,62,65,68,71,74,77
        dw      81,84,87,90,93,96,99,102,105,108,111,114,117
        dw      121,124,127,130,133,136,139,142,145,148,151,154,157
        dw      161,164,167,170,173,176,179,182,185,188,191,194,197
        dw      201,204,207,210,213,216,219,222,225,228,231,234,237
        dw      241,244,247,250,253,256,259,262,265,268,271,274,277
        dw      281,284,287,290,293,296,299,302,305,308,311,314,317
_non_momitor:
	bit	7,b
	jp	z,_m_50			; running
	ld	a,b
	cp	0f8h
	ret	nc
	brc	0f0h,_m_10
	clear	rsb
	ret
_m_10:
	ld	a,b
	and	00001111b
	ld	(channel),a
	ld	a,b
	and	11110000b
	ld	(rsb),a
	clear	dcb
	ret
_m_50:
	ld	a,(rsb)
	cp	0
	ret	z
	cp	0c0h
	ret	z
	cp	0d0h
	ret	z
	ld	a,(dcb)
	cp	0
	jp	nz,_m_90
	inc	a
	ld	(dcb),a
	ld	a,b
	ld	(keyno),a
	ret
_m_90:
	clear	dcb
	ld	a,(disp_main)
	cp	2
	ret	nz
	ld	a,(rsb)
	cp	0a0h
	ret	nz
	ld	a,(channel)
	cp	00fh
	ret	nz
	ld	a,(keyno)
	cp	95
	ret	nz
	ld	a,b
	ld	(disp_data),a
	brc	200,_d_22
	sub	200
	ld	(disp_data),a
	move	ddd+4,2
	jp	_d_2
_d_22:
	brc	100,_d_1
	sub	100
	ld	(disp_data),a
	move	ddd+4,1
	jp	_d_2
_d_1:
	move	ddd+4,0
	ld	a,(disp_data)
	cp	10
	jp	nc,_d_2
	move	ddd+3,0
	jp	_d_15
_d_2:
	ld	a,(disp_data)
	brc	90,_d_3
	sub	90
	ld	(disp_data),a
	move	ddd+3,9
	jp	_d_15
_d_3:
	brc	80,_d_4
	sub	80
	ld	(disp_data),a
	move	ddd+3,8
	jp	_d_15
_d_4:
	brc	70,_d_5
	sub	70
	ld	(disp_data),a
	move	ddd+3,7
	jp	_d_15
_d_5:
	brc	60,_d_6
	sub	60
	ld	(disp_data),a
	move	ddd+3,6
	jp	_d_15
_d_6:
	brc	50,_d_7
	sub	50
	ld	(disp_data),a
	move	ddd+3,5
	jp	_d_15
_d_7:
	brc	40,_d_8
	sub	40
	ld	(disp_data),a
	move	ddd+3,4
	jp	_d_15
_d_8:
	brc	30,_d_9
	sub	30
	ld	(disp_data),a
	move	ddd+3,3
	jp	_d_15
_d_9:
	brc	20,_d_10
	sub	20
	ld	(disp_data),a
	move	ddd+3,2
	jp	_d_15
_d_10:
	brc	10,_d_11
	sub	10
	ld	(disp_data),a
	move	ddd+3,1
	jp	_d_15
_d_11:
	move	ddd+3,0
_d_15:
	ld	a,(disp_data)
	ld	(ddd+2),a
	move	ddd+0,28		; Start Point (X)
	ld	a,(ddd+2)
	call	bar_char_sub
	move	ddd+0,15		; Start Point (X)
	ld	a,(ddd+3)
	call	bar_char_sub
	move	ddd+0,2			; Start Point (X)
	ld	a,(ddd+4)
	call	bar_char_sub
	ret

bar_char_sub:
	add	a,a
	ld	l,a
	ld	h,0
	ld	de,bar_select
	add	hl,de
	ld	e,(hl)
	inc	hl
	ld	d,(hl)			; [DE] = Dot Data
	clear	ddd+1			; Start Point (Y)
_d_20:
	ld	a,(ddd+1)
	add	a,a
	ld	l,a
	ld	h,0
	ld	bc,bar_y_sel
	add	hl,bc
	ld	c,(hl)
	inc	hl
	ld	b,(hl)
	ld	a,(ddd+0)
	ld	l,a
	ld	h,0
	add	hl,bc			; [HL] = Draw Point
	lcd_add
	ld	b,10
_d_30:
	ld	a,(de)
	brz	0,_d_32
	ld	a,0ffh
	jp	_d_35
_d_32:
	ld	a,' '
_d_35:
	lcd_out
	inc	de
	dec	b
	jp	nz,_d_30
	inc_a	ddd+1
	cp	8
	jp	nz,_d_20
	ret
bar_select:
	dw	bar_char_0,bar_char_1,bar_char_2,bar_char_3,bar_char_4
	dw	bar_char_5,bar_char_6,bar_char_7,bar_char_8,bar_char_9
bar_y_sel:
	dw	0,40,80,120,160,200,240,280
bar_char_0:
	db	0,1,1,1,1,1,1,1,1,0
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	0,1,1,1,1,1,1,1,1,0
bar_char_1:
	db	0,0,0,1,1,1,1,0,0,0
	db	0,0,1,1,1,1,1,0,0,0
	db	0,0,0,0,1,1,1,0,0,0
	db	0,0,0,0,1,1,1,0,0,0
	db	0,0,0,0,1,1,1,0,0,0
	db	0,0,0,0,1,1,1,0,0,0
	db	0,0,0,0,1,1,1,0,0,0
	db	0,1,1,1,1,1,1,1,1,0
bar_char_2:
	db	1,1,1,1,1,1,1,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
	db	1,1,1,1,1,1,1,1,1,1
	db	1,1,1,0,0,0,0,0,0,0
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,1,1,1,1,1,1,1
bar_char_3:
	db	1,1,1,1,1,1,1,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
	db	0,0,0,0,1,1,1,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,1,1,1,1,1,1,1
bar_char_4:
	db	0,0,0,0,0,1,1,1,0,0
	db	0,0,0,0,0,1,1,1,0,0
	db	1,1,1,0,0,1,1,1,0,0
	db	1,1,1,0,0,1,1,1,0,0
	db	1,1,1,0,0,1,1,1,0,0
	db	1,1,1,1,1,1,1,1,1,1
	db	0,0,0,0,0,1,1,1,0,0
	db	0,0,0,0,0,1,1,1,0,0
bar_char_5:
	db	1,1,1,1,1,1,1,1,1,1
	db	1,1,1,0,0,0,0,0,0,0
	db	1,1,1,0,0,0,0,0,0,0
	db	1,1,1,1,1,1,1,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,1,1,1,1,1,1,1
bar_char_6:
	db	1,1,1,1,1,1,1,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,0,0,0
	db	1,1,1,0,0,0,0,0,0,0
	db	1,1,1,1,1,1,1,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,1,1,1,1,1,1,1
bar_char_7:
	db	1,1,1,1,1,1,1,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
bar_char_8:
	db	1,1,1,1,1,1,1,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	0,1,1,1,1,1,1,1,1,0
	db	1,1,1,1,1,1,1,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,1,1,1,1,1,1,1
bar_char_9:
	db	1,1,1,1,1,1,1,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,1,1,1,1,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
	db	0,0,0,0,0,0,0,1,1,1
	db	1,1,1,0,0,0,0,1,1,1
	db	1,1,1,1,1,1,1,1,1,1

;##### LCD Screens #####
lcd_initial_set:
; 
	lcd_set	40h			; System Set
	lcd_put	30h			; Screen
	lcd_put	85h			; Field(X)
	lcd_put	7			; Field(Y)
	lcd_put	39			; C/R
	lcd_put	48			; TC/R
	lcd_put	3fh			; L/F
	lcd_put	40			; APL
	lcd_put	0			; APH
; 
	lcd_set	44h			; Scroll
	lcd_put	0			; Start Address #1(L)
	lcd_put	0			;		 (H)
	lcd_put	40h			; Line #1
	lcd_put	0			; Start Address #2(L)
	lcd_put	10h			;		 (H)
	lcd_put	40h			; Line #2
; 
	lcd_set	5ah			; HDot Scroll
	lcd_put	0			; Scroll=0
; 
	lcd_set	5bh			; Overlay
	lcd_put	01h	 		; Character + Graphic
; 
	lcd_set	58h			; Display OFF
	lcd_put	14h	 		; Parameter
; 
	lcd_set	5dh			; Cursor Form
	lcd_put	04h			; X=5
	lcd_put	86h			; Y=7
	lcd_set	4ch			; Right
	lcd_set	46h			; Address Set
	lcd_put	0
	lcd_put	0
; 
	lcd_set	52h			; Erase
	call	wait_long
	call	wait_long
	call	wait_long
	lcd_set	59h			; Display ON
	lcd_put	14h			; Cursor OFF
	ret

init_menu:
;;;;	call	lcd_initial_set
	clear	disp_main
	clear	disp_sub
	clear	rx_top
	clear	rx_end
	clear	rsb
	clear	dcb
	lcd_mes	1,1,init_mess_1
	lcd_mes	2,1,init_mess_2
	lcd_mes	3,1,init_mess_3
	lcd_mes	4,1,init_mess_4
	lcd_mes	5,1,init_mess_5
	lcd_mes	6,1,init_mess_6
	lcd_mes	7,1,init_mess_7
	lcd_mes	8,1,init_mess_8
	call	disp_cursor
	ret
init_mess_1:	db	'=== (^_^) MIDI DISPLAY  by Y.N (^_^) ===',tail
init_mess_2:	db	'|                                      |',tail
init_mess_3:	db	'|  [A]  Universal MIDI Stream Monitor  |',tail
init_mess_4:	db	'|  [B]  "Chaotic Grains" Bar Counter   |',tail
init_mess_5:	db	'|  [C]  (reserved)                     |',tail
init_mess_6:	db	'|  [D]  (reserved)                     |',tail
init_mess_7:	db	'|  [E]  (reserved)                     |',tail
init_mess_8:	db	'|______________________________________|',tail

clear_screen:
	lcd_mes	1,1,init_mess_0
	lcd_mes	2,1,init_mess_0
	lcd_mes	3,1,init_mess_0
	lcd_mes	4,1,init_mess_0
	lcd_mes	5,1,init_mess_0
	lcd_mes	6,1,init_mess_0
	lcd_mes	7,1,init_mess_0
	lcd_mes	8,1,init_mess_0
	ret
init_mess_0:	db	'                                        ',tail

disp_momitor:
	call	clear_screen
	move	mon_write,0
	move	mon_erase,16
	move	disp_main,1
	ret

disp_bar:
	call	clear_screen
	move	disp_main,2
	ret

;##### Subroutines #####
wait_long:
	ld	b,0ffh
1$:
	call	wait_sub
	dec	b
	jr	nz,1$
	ret
wait_sub:
	ld	a,0ffh
1$:
	nop
	dec	a
	jr	nz,1$
	ret

led_display:
	xor	0ffh
	out	(pio_a+0),a
	io_put	pio_b,80h
	io_put	pio_b,0
	ret

lcd_disp_mess:
	lcd_add
1$:
	ld	a,(de)
	cp	tail
	ret	z
	out	(lcd),a
	inc	de
	jp	1$

sw_scan:
	inc_a	sw_status+0
	cp	50
	ret	c
	clear	sw_status+0
	ld	a,(sw_status+1)		; Old Status = [B]
	ld	b,a
	in	a,(pio_b)
	and	00000011b
	ld	(sw_status+1),a		; Newest Status = [C]
	ld	c,a
	xor	b
	ret	z			; Non Event
	ld	a,c
	brz	00000011b,init_menu	; Both ON
	cp	0
	ret	z			; Both OFF
	ld	a,b
	cp	0
	ret	nz			; Off Event
	ld	a,c
	brz	00000010b,_20$		; SW(1) ON
	brz	00000001b,_30$		; SW(2) ON
	ret
_20$:
	inc_chk	disp_sub,5
	call	disp_cursor
	ret
_30$:
	ld	a,(disp_main)
	cp	1
	ret	z
	cp	2
	ret	z
	brz	0,_disp_menu
	ret
_disp_menu:
	ld	a,(disp_sub)
	brz	0,disp_momitor
	brz	1,disp_bar
	ret

disp_cursor:
	ld	a,(disp_main)
	brz	0,_disp_main
	ret
_disp_main:
	ld	a,(disp_sub)
	brz	0,10$_
	brz	1,20$_
	brz	2,30$_
	brz	3,40$_
	brz	4,50$_
	ret
10$_:
	lcd_mes	3,3,_21_
	lcd_mes	4,3,_12_
	lcd_mes	5,3,_13_
	lcd_mes	6,3,_14_
	lcd_mes	7,3,_15_
	ret
20$_:
	lcd_mes	3,3,_11_
	lcd_mes	4,3,_22_
	lcd_mes	5,3,_13_
	lcd_mes	6,3,_14_
	lcd_mes	7,3,_15_
	ret
30$_:
	lcd_mes	3,3,_11_
	lcd_mes	4,3,_12_
	lcd_mes	5,3,_23_
	lcd_mes	6,3,_14_
	lcd_mes	7,3,_15_
	ret
40$_:
	lcd_mes	3,3,_11_
	lcd_mes	4,3,_12_
	lcd_mes	5,3,_13_
	lcd_mes	6,3,_24_
	lcd_mes	7,3,_15_
	ret
50$_:
	lcd_mes	3,3,_11_
	lcd_mes	4,3,_12_
	lcd_mes	5,3,_13_
	lcd_mes	6,3,_14_
	lcd_mes	7,3,_25_
	ret
_11_:	db	' [A] ',tail
_12_:	db	' [B] ',tail
_13_:	db	' [C] ',tail
_14_:	db	' [D] ',tail
_15_:	db	' [E] ',tail
_21_:	db	0ffh,'[A]',0ffh,tail
_22_:	db	0ffh,'[B]',0ffh,tail
_23_:	db	0ffh,'[C]',0ffh,tail
_24_:	db	0ffh,'[D]',0ffh,tail
_25_:	db	0ffh,'[E]',0ffh,tail

	end

Z80アセンブラ その2 PowerGloveの制御

;-----------------------------------------------------------------------
;	Power Glove Interface : April.1993
;-----------------------------------------------------------------------

;##### RAM Map #####
	dseg
	org	0000h
tx_fifo		ds	256
tx_top		ds	1
tx_end		ds	1
timer_flag	ds	4
glove_status	ds	1
x_value		ds	2
y_value		ds	2
counts		ds	4
channel		ds	1

;##### I/O Map #####
	cseg
ctc_0		equ	0010h
s_gen		equ	0017h
sio_a		equ	0018h
sio_b		equ	001ah
pio_a		equ	001ch
pio_b		equ	001eh
led_up		equ	0080h
led_down	equ	0081h
led_left	equ	0082h
led_right	equ	0083h

;##### MACRO #####
io_set	macro	@1,@2
	ld	a,@2
	out	(@1+1),a
	endm
io_put	macro	@1,@2
	ld	a,@2
	out	(@1+0),a
	endm

;##### RESET #####
	org	0000h
	ld      sp,0ffffh
	di
	jp      main

;##### INT / NMI #####
	org     0020h
	dw	_timer_
_timer_:
	ex	af,af'
	ld	a,1
	ld	(timer_flag+0),a
	ex	af,af'
	ei
	reti
	org     0066h
	retn

;##### Data Table #####
	org	0100h
display_table_1:
	db	10h,10h,10h,10h,10h,10h,10h,10h		; 1 - 8
	db	18h,18h,18h,18h,18h,18h,18h,18h		; 9 - 16
	db	08h,08h,08h,08h,0ch,0ch,0ch,0ch		; 17 - 24
	db	0ch,0ch,0ch,0ch,04h,04h,04h,04h		; 25 - 32
	db	06h,06h,06h,06h,06h,06h,06h,06h		; 33 - 40
	db	02h,02h,02h,02h,03h,03h,03h,03h		; 41 - 48
	db	03h,03h,03h,03h,01h,01h,01h,01h		; 49 - 56
	db	01h,01h,01h,01h,01h,01h,01h,01h		; 57 - 64
	db	01h,01h,01h,01h,01h,01h,01h,01h		; 65 - 72
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 73 - 80
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 81 - 88
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 89 - 96
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 97 - 104
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 105 - 112
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 113 - 120
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 121 - 128
	org	0200h
display_table_2:
	db	10h,10h,10h,10h,10h,10h,10h,10h		; 1 - 8
	db	10h,10h,10h,10h,08h,08h,08h,08h		; 9 - 16
	db	08h,08h,08h,08h,08h,08h,08h,08h		; 17 - 24
	db	04h,04h,04h,04h,04h,04h,04h,04h		; 25 - 32
	db	04h,04h,04h,04h,02h,02h,02h,02h		; 33 - 40
	db	02h,02h,02h,02h,02h,02h,02h,02h		; 41 - 48
	db	01h,01h,01h,01h,01h,01h,01h,01h		; 49 - 56
	db	01h,01h,01h,01h,01h,01h,01h,01h		; 57 - 64
	db	01h,01h,01h,01h,00h,00h,00h,00h		; 65 - 72
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 73 - 80
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 81 - 88
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 89 - 96
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 97 - 104
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 105 - 112
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 113 - 120
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 121 - 128
	org	0300h
display_table_3:
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 1 - 8
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 9 - 16
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 17 - 24
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 25 - 32
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 33 - 40
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 41 - 48
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 49 - 56
	db	01h,01h,01h,01h,01h,01h,01h,01h		; 57 - 64
	db	01h,01h,01h,01h,01h,01h,01h,01h		; 65 - 72
	db	01h,01h,01h,01h,03h,03h,03h,03h		; 73 - 80
	db	03h,03h,03h,03h,02h,02h,02h,02h		; 81 - 88
	db	06h,06h,06h,06h,06h,06h,06h,06h		; 89 - 96
	db	04h,04h,04h,04h,0ch,0ch,0ch,0ch		; 97 - 104
	db	0ch,0ch,0ch,0ch,08h,08h,08h,08h		; 105 - 112
	db	18h,18h,18h,18h,18h,18h,18h,18h		; 113 - 120
	db	10h,10h,10h,10h,10h,10h,10h,10h		; 121 - 128
	org	0400h
display_table_4:
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 1 - 8
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 9 - 16
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 17 - 24
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 25 - 32
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 33 - 40
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 41 - 48
	db	00h,00h,00h,00h,00h,00h,00h,00h		; 49 - 56
	db	00h,00h,00h,00h,01h,01h,01h,01h		; 57 - 64
	db	01h,01h,01h,01h,01h,01h,01h,01h		; 65 - 72
	db	01h,01h,01h,01h,01h,01h,01h,01h		; 73 - 80
	db	02h,02h,02h,02h,02h,02h,02h,02h		; 81 - 88
	db	02h,02h,02h,02h,04h,04h,04h,04h		; 89 - 96
	db	04h,04h,04h,04h,04h,04h,04h,04h		; 97 - 104
	db	08h,08h,08h,08h,08h,08h,08h,08h		; 105 - 112
	db	08h,08h,08h,08h,10h,10h,10h,10h		; 113 - 120
	db	10h,10h,10h,10h,10h,10h,10h,10h		; 121 - 128

;##### Main #####
main:
	ld      hl,08000h
	ld      a,090h
_ram_clear_loop:
	ld      (hl),0
	inc     hl
	cp      h
	jr	nc,_ram_clear_loop
	io_put	led_up,0ffh
	io_put	led_down,0ffh
	io_put	led_right,0ffh
	io_put	led_left,0ffh
	io_set	pio_a,0cfh		; Mode 3
	io_set	pio_a,00001111b		; 0:Out / 1:In
	io_set	pio_a,007h		; Interrupt Disable
	io_set	pio_b,0cfh		; Mode 3
	io_set	pio_b,00000000b		; 0:Out / 1:In
	io_set	pio_b,007h		; Interrupt Disable
	io_put	pio_a,10000000b
	io_put	pio_b,0ffh
	io_put	s_gen,00000000b		; Clock Generator
	io_set	sio_a,00011000b		; Channel Reset A
	io_set	sio_a,00000100b		; Resister Point = 4
	io_set	sio_a,10000100b		; Mode
	io_set	sio_a,00000001b		; Resister Point = 1
	io_set	sio_a,00000000b		; Interrupt Mode
	io_set	sio_a,00000101b		; Resister Point = 5
	io_set	sio_a,01101000b		; Transmit Start
	io_put	ctc_0,20h		; Int. Address
	io_put	ctc_0,10100101b
	io_put	ctc_0,80		; about 5msec
	ld	a,0eh
	ld	(channel),a
	call	centering
	im	2
	ei
loop:
	call	glove_scan
	call	display_timer
	call	tx_data_check
	jr	loop

;##### Subroutines #####
x_value_set:
	ld	a,(x_value+1)
	ld	b,a
	ld	a,(x_value)
	cp	b
	ret	z
	ld	(x_value+1),a
	ld	a,(channel)
	or	0b0h
	ld	b,a
	call	tx_data_set
	ld	b,10
	call	tx_data_set
	ld	a,(x_value)
	ld	b,a
	call	tx_data_set
	call	x_value_disp
	ret
y_value_set:
	ld	a,(y_value+1)
	ld	b,a
	ld	a,(y_value)
	cp	b
	ret	z
	ld	(y_value+1),a
	ld	a,(channel)
	or	0b0h
	ld	b,a
	call	tx_data_set
	ld	b,7
	call	tx_data_set
	ld	a,(y_value)
	ld	b,a
	call	tx_data_set
	call	y_value_disp
	ret
tx_data_check:
	ld	a,(tx_end)
	ld	b,a
	ld	a,(tx_top)
	cp	b
	ret	z
	io_set	sio_a,00000000b		; Resister Point = 0
	in	a,(sio_a+1)
	bit	2,a
	ret	z
	ld	hl,tx_fifo
	ld	l,b
	ld	a,(hl)
	out	(sio_a),a
	ld	a,b
	inc	a
	ld	(tx_end),a
	ret
tx_data_set:
	ld	hl,tx_fifo
	ld	a,(tx_top)
	ld	l,a
	inc	a
	ld	(tx_top),a
	ld	(hl),b
	ret
centering:
	ld	a,040h
	ld	(x_value),a
	ld	(y_value),a
	call	x_value_set
	call	y_value_set
	ret

;##### Panel LED Display #####
display_timer:
	ld	a,(timer_flag+1)
	cp	50			; about 250msec
	ret	c
	xor	a
	ld	(timer_flag+1),a
	ld	a,(timer_flag+2)
	inc	a
	ld	(timer_flag+2),a
	call	x_value_disp
	call	y_value_disp
	xor	a
	ld	(counts+0),a
	ld	(counts+1),a
	ld	(counts+2),a
	ld	(counts+3),a
	ret
x_value_disp:
	ld	a,(timer_flag+2)
	bit	0,a
	jr	nz,_x_disp_off
	ld	hl,display_table_3
	ld	de,display_table_1
	jr	_x_disp_mix
_x_disp_off:
	ld	hl,display_table_4
	ld	de,display_table_2
_x_disp_mix:
	ld	a,(x_value)
	ld	l,a
	ld	a,(hl)
	xor	0ffh
	out	(led_right),a
	ld	h,d
	ld	a,(hl)
	xor	0ffh
	out	(led_left),a
	ret
y_value_disp:
	ld	a,(timer_flag+2)
	bit	0,a
	jr	z,_y_disp_off
	ld	hl,display_table_3
	ld	de,display_table_1
	jr	_y_disp_mix
_y_disp_off:
	ld	hl,display_table_4
	ld	de,display_table_2
_y_disp_mix:
	ld	a,(y_value)
	ld	l,a
	ld	a,(hl)
	xor	0ffh
	out	(led_up),a
	ld	h,d
	ld	a,(hl)
	xor	0ffh
	out	(led_down),a
	ret

;##### Power Glove Event Check #####
;------------------------------------------------------------------------*
;  Bit :   7       6      5     4     3        2       1          0      |
;        right   left   down   up   start   select   first(B)   thumb(A) |
;------------------------------------------------------------------------*
glove_check:
	ld	a,(glove_status)
	and	00001100b
	jr	z,_glove_cont
	call	centering
	ret
_glove_cont:
	ld	a,(glove_status)
	bit	1,a
	jp	z,_glove_2
	ld	c,3			; Fast Skip
	ld	a,(glove_status)
	bit	4,a			; Up ?
	jr	z,_glove_1
	ld	a,(counts+0)
	add	a,c
	ld	(counts+0),a
	bit	3,a
	jr	z,_glove_1
	ld	a,(y_value)
	cp	7fh
	jr	z,_glove_1
	inc	a
	ld	(y_value),a
	call	y_value_set
	xor	a
	ld	(counts+0),a
	ld	(counts+1),a
_glove_1:
	ld	a,(glove_status)
	bit	5,a			; Down ?
	jr	z,_glove_2
	ld	a,(counts+1)
	add	a,c
	ld	(counts+1),a
	bit	3,a
	jr	z,_glove_2
	ld	a,(y_value)
	cp	0
	jr	z,_glove_2
	dec	a
	ld	(y_value),a
	call	y_value_set
	xor	a
	ld	(counts+0),a
	ld	(counts+1),a
_glove_2:
	ld	a,(glove_status)
	bit	0,a
	ret	z
	ld	c,2			; Slow Skip
	ld	a,(glove_status)
	bit	6,a			; Left ?
	jr	z,_glove_3
	ld	a,(counts+2)
	add	a,c
	ld	(counts+2),a
	bit	3,a
	jr	z,_glove_3
	ld	a,(x_value)
	cp	0
	jr	z,_glove_3
	dec	a
	ld	(x_value),a
	call	x_value_set
	xor	a
	ld	(counts+2),a
	ld	(counts+3),a
_glove_3:
	ld	a,(glove_status)
	bit	7,a			; Right ?
	ret	z
	ld	a,(counts+3)
	add	a,c
	ld	(counts+3),a
	bit	3,a
	ret	z
	ld	a,(x_value)
	cp	7fh
	ret	z
	inc	a
	ld	(x_value),a
	call	x_value_set
	xor	a
	ld	(counts+2),a
	ld	(counts+3),a
	ret

;##### Power Glove Status Scan #####
glove_scan:
	ld	a,(timer_flag+0)
	cp	0
	ret	z
	xor	a
	ld	(timer_flag+0),a
	ld	a,(timer_flag+1)
	inc	a
	ld	(timer_flag+1),a
	ld	b,0
	call	p_s_pulse
	in	a,(pio_a)
	bit	0,a
	jr	z,1$
	set	0,b
1$:
	call	clock_shift
	in	a,(pio_a)
	bit	0,a
	jr	z,2$
	set	1,b
2$:
	call	clock_shift
	in	a,(pio_a)
	bit	0,a
	jr	z,3$
	set	2,b
3$:
	call	clock_shift
	in	a,(pio_a)
	bit	0,a
	jr	z,4$
	set	3,b
4$:
	call	clock_shift
	in	a,(pio_a)
	bit	0,a
	jr	z,5$
	set	4,b
5$:
	call	clock_shift
	in	a,(pio_a)
	bit	0,a
	jr	z,6$
	set	5,b
6$:
	call	clock_shift
	in	a,(pio_a)
	bit	0,a
	jr	z,7$
	set	6,b
7$:
	call	clock_shift
	in	a,(pio_a)
	bit	0,a
	jr	z,8$
	set	7,b
8$:
	call	clock_shift
	ld	a,b
	out	(pio_b),a
	xor	0ffh
	ld	(glove_status),a	; New Status
	call	glove_check
	ret
p_s_pulse:
	io_put	pio_a,11000000b
	nop
	nop
	io_put	pio_a,10000000b
	ret
clock_shift:
	io_put	pio_a,00000000b
	nop
	nop
	io_put	pio_a,10000000b
	ret

	end

Z80アセンブラ その3 MIDIマージ/ビデオ制御

;-----------------------------------------------------------------------
;	[PEGASUS-5] MIDI Merge / Video Control Box : December.1993
;-----------------------------------------------------------------------

;##### RAM Map #####
	dseg
	org	0000h
rx_fifo_0	ds	4096
rx_fifo_1	ds	4096
rx_fifo_2	ds	4096
rx_fifo_3	ds	4096
tx_fifo_0	ds	4096
tx_fifo_1	ds	4096
tx_fifo_2	ds	4096
rx_top_0	ds	2
rx_end_0	ds	2
rx_top_1	ds	2
rx_end_1	ds	2
rx_top_2	ds	2
rx_end_2	ds	2
rx_top_3	ds	2
rx_end_3	ds	2
tx_top_0	ds	2
tx_end_0	ds	2
tx_top_1	ds	2
tx_end_1	ds	2
tx_top_2	ds	2
tx_end_2	ds	2
rx_data		ds	4
rsb		ds	4
dcb		ds	4
channel		ds	4
keyno		ds	4
cg_note		ds	1
cg_velocity	ds	1
power_timer	ds	2
led_timer	ds	8
led_mode	ds	1
led_data	ds	1
glove_tim	ds	1
glove_old	ds	1

;##### I/O Map #####
	cseg
sio_a		equ	0018h	; for Glove Rx
sio_b		equ	001ah	; for Glove Rx
pio_a		equ	001ch
pio_b		equ	001eh
uart_0		equ	0080h
uart_1		equ	0084h
uart_2		equ	0088h
uart_3		equ	008ch

;##### MACRO #####
all_out	macro	@1
	ld	a,@1
	out	(uart_0+1),a
	out	(uart_1+1),a
	out	(uart_2+1),a
	out	(uart_3+1),a
	endm
pio_set	macro	@1,@2
	ld	a,@2
	out	(@1+1),a
	endm
io_set	macro	@1,@2
	ld	a,@2
	out	(@1+1),a
	endm
io_put	macro	@1,@2
	ld	a,@2
	out	(@1+0),a
	endm
led_res	macro	@1
	ld	a,(led_timer+@1)
	cp	0
	ret	z
	dec	a
	ld	(led_timer+@1),a
	ret	nz
	ld	a,(led_data)
	res	@1,a
	ld	(led_data),a
	call	led_disp
	ret
	endm
led_set	macro	@1
	ld	a,20
	ld	(led_timer+@1),a
	ld	a,(led_data)
	set	@1,a
	ld	(led_data),a
	call	led_disp
	endm
rx_set	macro	@1,@2,@3
	ld	de,(@3)
	ld	a,@1
	or	d
	ld	h,a
	ld	l,e
	in	a,(@2+0)
	ld	(hl),a
	inc	de
	res	4,d
	ld	(@3),de
	ret
	endm
tx_set	macro	@1,@3
	ld	de,(@3)
	ld	a,@1
	or	d
	ld	h,a
	ld	l,e
	ld	(hl),b
	inc	de
	res	4,d
	ld	(@3),de
	ret
	endm
tx_chk	macro	@1,@2,@3,@4
	in	a,(@4+1)
	bit	0,a
	ret	z
	ld	de,(@2)
	ld	hl,(@1)
	and	a			; CY <-- 0
	sbc	hl,de
	ret	z
	ld	a,@3
	or	d
	ld	h,a
	ld	l,e
	ld	a,(hl)
	out	(@4),a
	inc	de
	res	4,d
	ld	(@2),de
	ret
	endm
rx_chk	macro	@1,@2,@3,@4
	ld	de,(@2)
	ld	hl,(@1)
	and	a			; CY <-- 0
	sbc	hl,de
	ret	z
	ld	a,@3
	or	d
	ld	h,a
	ld	l,e
	ld	b,(hl)			; [B] = Rx Data
	inc	de
	res	4,d
	ld	(@2),de
	bit	7,b
	jr	z,50$			; running
	ld	a,b
	cp	0f8h
	ret	nc
	cp	0f0h
	jr	c,20$
	xor	a
	ld	(rsb+@4),a
	ret
20$:
	led_set	@4+4
	ld	a,b
	and	11110000b
	ld	(rsb+@4),a
	xor	a
	ld	(dcb+@4),a
	ret
50$:
	led_set	@4+4
	ld	a,(rsb+@4)
	cp	0
	ret	z
	cp	0c0h
	jr	z,70$
	cp	0d0h
	jr	z,70$
	ld	a,(dcb+@4)
	cp	0
	jr	nz,90$
	inc	a
	ld	(dcb+@4),a
	ld	a,b
	ld	(keyno+@4),a
	ret
70$:
	ld	c,b
	ld	a,(rsb+@4)
	or	0fh
	ld	b,a
	call	tx_fifo_set_0
	ld	b,c
	call	tx_fifo_set_0
	led_set	7
	ret
90$:
	xor	a
	ld	(dcb+@4),a
	ld	c,b
	ld	a,(rsb+@4)
	or	0fh
	ld	b,a
	call	tx_fifo_set_0
	ld	a,(keyno+@4)
	ld	b,a
	call	tx_fifo_set_0
	ld	b,c
	call	tx_fifo_set_0
	led_set	7
	ret
	endm

;##### RESET #####
	org	0000h
	ld      sp,0ffffh
	di
	im      1
	call    initialize
	ei
	jp      loop

;##### INT / NMI #####
	org     0038h
	ex	af,af'
	exx
	call	int_sequence
	exx
	ex	af,af'
	ei
	reti
	org     0066h
	ei
	retn
int_sequence:
	in	a,(uart_3+1)
	bit	1,a
	jr	z,_next_1
	rx_set	10110000b,uart_3,rx_top_3
_next_1:
	in	a,(uart_1+1)
	bit	1,a
	jr	z,_next_2
	rx_set	10010000b,uart_1,rx_top_1
_next_2:
	in	a,(uart_2+1)
	bit	1,a
	jr	z,_next_3
	rx_set	10100000b,uart_2,rx_top_2
_next_3:
	in	a,(uart_0+1)
	bit	1,a
	ret	z
	rx_set	10000000b,uart_0,rx_top_0

;##### MIDI Transmit Data Set : Input=[B] #####
tx_fifo_set_0:
	tx_set	11000000b,tx_top_0
tx_fifo_set_1:
	tx_set	11010000b,tx_top_1
tx_fifo_set_2:
	tx_set	11100000b,tx_top_2

;##### MIDI Transmit Check #####
tx_check_0:
	tx_chk	tx_top_0,tx_end_0,11000000b,uart_0
tx_check_1:
	tx_chk	tx_top_1,tx_end_1,11010000b,uart_1
tx_check:
	tx_chk	tx_top_2,tx_end_2,11100000b,uart_2

;##### MIDI Receive Check #####
rx_fifo_0_check:
	rx_chk	rx_top_0,rx_end_0,10000000b,0
rx_fifo_1_check:
	rx_chk	rx_top_1,rx_end_1,10010000b,1
rx_fifo_2_check:
	rx_chk	rx_top_2,rx_end_2,10100000b,2
rx_fifo_3_check:
	ld	de,(rx_end_3)
	ld	hl,(rx_top_3)
	and	a			; CY <-- 0
	sbc	hl,de
	ret	z
	ld	a,10110000b
	or	d
	ld	h,a
	ld	l,e
	ld	b,(hl)			; [B] = Rx Data
	inc	de
	res	4,d
	ld	(rx_end_3),de
	bit	7,b
	jr	z,50$			; running
	ld	a,b
	cp	0f8h
	ret	nc
	cp	0f0h
	jr	c,20$
11$:
	xor	a
	ld	(rsb+3),a
	ret
20$:
	ld	a,b
	and	00001111b
	cp	0fh			; !! [F] Channel Only !!
	jr	nz,11$			; !!!!!!!!!!!!!!!!!!!!!!
	ld	(channel+3),a
	ld	a,b
	and	11110000b
	ld	(rsb+3),a
	xor	a
	ld	(dcb+3),a
	led_set	1
	ret
50$:
	ld	a,(rsb+3)
	cp	0
	ret	z
	cp	0c0h
	jr	z,70$
	cp	0d0h
	jr	z,70$
	ld	a,(dcb+3)
	cp	0
	jr	nz,90$
	inc	a
	ld	(dcb+3),a
	ld	a,b
	ld	(keyno+3),a
	ret
70$:
	led_set	1
	ld	c,b
	ld	a,(rsb+3)
	ld	d,a
	ld	a,(channel+3)
	or	d
	ld	b,a
	call	tx_fifo_set_0
	ld	b,c
	call	tx_fifo_set_0
	led_set	7
	ret
90$:
	led_set	1
	xor	a
	ld	(dcb+3),a
	ld	c,b
	ld	a,(rsb+3)
	ld	d,a
	ld	a,(channel+3)
	or	d
	ld	b,a
	call	tx_fifo_set_0
	ld	a,(keyno+3)
	ld	b,a
	call	tx_fifo_set_0
	ld	b,c
	call	tx_fifo_set_0
	led_set	7
	ld	a,(rsb+3)
	cp	0a0h
	ret	nz
	ld	a,(keyno+3)
	cp	96
	jp	z,hit_96
	cp	97
	jp	z,hit_97
	cp	98
	jp	z,hit_98
	cp	99
	jp	z,hit_99
	cp	100
	jp	z,hit_100
	cp	101
	jp	z,hit_101
	cp	102
	jp	z,hit_102
	ret
hit_96:
	ld	a,c
	sla	a			; !!!!! bit[0] = 71051 RESET !!!!!
	out	(pio_a+0),a		; 000000000 - 11111110
	ret
hit_97:
	ld	b,09ch
	call	tx_fifo_set_1
	ld	b,c
	call	tx_fifo_set_1
	ld	b,040h
	call	tx_fifo_set_1
	led_set	2
	ret
hit_98:
	ld	a,c
	ld	(cg_note),a
_hit_98_99:
	ld	b,09dh
	call	tx_fifo_set_2
	ld	a,(cg_note)
	ld	b,a
	call	tx_fifo_set_2
	ld	a,(cg_velocity)
	ld	b,a
	call	tx_fifo_set_2
	led_set	3
	ret
hit_99:
	ld	a,c
	ld	(cg_velocity),a
	jr	_hit_98_99
hit_100:
	ld	b,0bdh
	call	tx_fifo_set_2
	ld	b,1
	call	tx_fifo_set_2
	ld	b,c
	call	tx_fifo_set_2
	led_set	3
	ret
hit_101:
	ld	b,0edh
	call	tx_fifo_set_2
	ld	b,0
	call	tx_fifo_set_2
	ld	b,c
	call	tx_fifo_set_2
	led_set	3
	ret
hit_102:
	ret

;##### Main Program #####
loop:
	call	rx_fifo_0_check
	call	tx_check_0
	call	rx_fifo_1_check
	call	tx_check_1
	call	rx_fifo_2_check
	call	tx_check_2
	call	rx_fifo_3_check
	call	led_display
	call	glove_check
	jr	loop

;##### INITIALIZE #####
initialize:
	ld      hl,0f000h
	ld      a,0f8h
_ram_clear_loop:
	ld      (hl),0
	inc     hl
	cp      h
	jr	nc,_ram_clear_loop
	pio_set	pio_a,0cfh		; Mode 3
	pio_set	pio_a,00000000b		; 0:Out / 1:In
	pio_set	pio_a,007h		; Interrupt Disable
	ld	a,00000001b
	out	(pio_a+0),a
	call	wait_long
	ld	a,0
	out	(pio_a+0),a
	pio_set	pio_b,0cfh		; Mode 3
	pio_set	pio_b,00000000b		; 0:Out / 1:In
	pio_set	pio_b,007h		; Interrupt Disable
	ld	a,00000001b
	ld	(led_data),a
	call	led_disp
	call	wait_long
	io_set	sio_a,00011000b		; Channel Reset A
	io_set	sio_a,00000100b		; Resister Point = 4
	io_set	sio_a,00000000b		; Mode
	io_set	sio_b,00011000b		; Channel Reset B
	io_set	sio_b,00000100b		; Resister Point = 4
	io_set	sio_b,00000000b		; Mode
	io_set	sio_a,00010000b		; clear
	io_set	sio_b,00010000b		; clear
	all_out	0
	all_out	0
	all_out	0
	call	wait_long
	all_out	01000000b
	all_out	01001110b
	all_out	00000101b
	ld	a,0fch
	out	(uart_0+0),a
	out	(uart_1+0),a
	out	(uart_2+0),a
	ld	a,04ch
	ld	(cg_velocity),a
	ret
wait_long:
	ld	c,15
_wait_long_loop:
	call	wait_mid
	dec	c
	jr	nz,_wait_long_loop
	ret
wait_mid:
	ld	a,15
_wait_mid_loop:
	call	wait_short
	dec	a
	jr	nz,_wait_mid_loop
	ret
wait_short:
	db	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
	ret

;##### LED Display Routine #####
led_disp:
	ld	a,(led_data)
	xor	0ffh
	out	(pio_b+0),a
	ret
led_display:
	call	power_timer_chk
	ld	a,(led_mode)
	inc	a
	and	00000111b
	ld	(led_mode),a
	cp	1
	jp	z,led_1
	cp	2
	jp	z,led_2
	cp	3
	jp	z,led_3
	cp	4
	jp	z,led_4
	cp	5
	jp	z,led_5
	cp	6
	jp	z,led_6
	cp	7
	jp	z,led_7
	ret
led_1:
	led_res	1
led_2:
	led_res	2
led_3:
	led_res	3
led_4:
	led_res	4
led_5:
	led_res	5
led_6:
	led_res	6
led_7:
	led_res	7
power_timer_chk:
	ld	a,(power_timer+0)
	dec	a
	ld	(power_timer+0),a
	ret	nz
	ld	a,100
	ld	(power_timer+0),a
	ld	a,(power_timer+1)
	dec	a
	ld	(power_timer+1),a
	ret	nz
	ld	a,50
	ld	(power_timer+1),a
	ld	a,(led_data)
	xor	00000001b
	ld	(led_data),a
	call	led_disp
	ret

;##### Wireless Power Glove Check Routine #####
glove_check:
	ld	a,(glove_tim)
	dec	a
	ld	(glove_tim),a
	ret	nz
	ld	a,20
	ld	(glove_tim),a
	ld	e,0
	in	a,(sio_a+1)
	bit	3,a
	jr	nz,_gl_1
	set	0,e
_gl_1:
	bit	5,a
	jr	nz,_gl_2
	set	1,e
_gl_2:
	in	a,(sio_b+1)
	bit	3,a
	jr	nz,_gl_3
	set	2,e
_gl_3:
	bit	5,a
	jr	nz,_gl_4
	set	3,e
_gl_4:
	ld	a,(glove_old)
	cp	e
	ret	z
	ld	a,e
	ld	(glove_old),a
	ld	b,0afh
	call	tx_fifo_set_0
	ld	b,127
	call	tx_fifo_set_0
	ld	a,(glove_old)
	ld	b,a
	call	tx_fifo_set_0
	io_set	sio_a,00010000b		; clear
	io_set	sio_b,00010000b		; clear
	ret

	end

Z80アセンブラ その4 プリピアードピアノ用衝撃センサ

;-----------------------------------------------------------------------
;	[PEGASUS-7] Sensor Box for "Strange Attractor" : October.1994
;-----------------------------------------------------------------------

;##### RAM Map #####
	dseg
	org	0000h
rx_fifo		ds	8192
tx_fifo		ds	8192
rx_top		ds	2
rx_end		ds	2
tx_top		ds	2
tx_end		ds	2
rsb		ds	1
dcb		ds	1
channel		ds	1
keyno		ds	1
timer		ds	1
led		ds	1
ch_ctr		ds	1
ch_status	ds	8
bit_mask	ds	1
sw		ds	1
para		ds	3

;##### I/O Map #####
	cseg
sio_a		equ	0018h
sio_b		equ	001ah
pio_a		equ	001ch
pio_b		equ	001eh

;##### MACRO #####
io_set	macro	@1,@2
	ld	a,@2
	out	(@1+1),a
	endm
io_put	macro	@1,@2
	ld	a,@2
	out	(@1+0),a
	endm

;##### RESET #####
	org	0000h
	ld      sp,0ffffh
	di
	jp      main

;##### INT / NMI #####
	org     0020h
	dw	_midi_
_midi_:
	ex	af,af'
	exx
	ld	de,(rx_top)
	ld	a,10000000b
	or	d
	ld	h,a
	ld	l,e
	in	a,(sio_a+0)
	ld	(hl),a
	inc	de
	res	5,d
	ld	(rx_top),de
	exx
	ex	af,af'
	ei
	reti
	org     0066h
	retn

;##### Main #####
main:
	ld      hl,08000h
	ld      a,0f0h
_ram_clear_loop:
	ld      (hl),0
	inc     hl
	cp      h
	jr	nc,_ram_clear_loop
	io_set	pio_a,0cfh		; Mode 3
	io_set	pio_a,00000000b		; 0:Out / 1:In
	io_set	pio_a,007h		; Interrupt Disable
	io_put	pio_a,11111111b		;
	io_set	pio_b,0cfh		; Mode 3
	io_set	pio_b,11111111b		; 0:Out / 1:In
	io_set	pio_b,007h		; Interrupt Disable
	io_set	sio_b,00011000b		; Channel Reset B
	io_set	sio_b,00000100b		; Resister Point = 4
	io_set	sio_b,11000100b		; Mode
	io_set	sio_b,00000001b		; Resister Point = 1
	io_set	sio_b,00000000b		; Interrupt Mode
	io_set	sio_b,00000010b		; Resister Point = 2
	io_set	sio_b,20h		; Vector Address
	io_set	sio_a,00011000b		; Channel Reset A
	io_set	sio_a,00000100b		; Resister Point = 4
	io_set	sio_a,11000100b		; Mode
	io_set	sio_a,00000001b		; Resister Point = 1
	io_set	sio_a,00010000b		; Interrupt Mode
	io_set	sio_a,00000101b		; Resister Point = 5
	io_set	sio_a,01101000b		; Transmit Start
	io_set	sio_a,00000011b		; Resister Point = 3
	io_set	sio_a,11000001b		; Receive Start
	im	2
	ei
	ld	a,10
	ld	(timer),a
	ld	a,10
	ld	(para+0),a
	ld	a,20
	ld	(para+1),a
	ld	a,25
	ld	(para+2),a
	in	a,(sio_a+0)		; dummy read
loop:
	call	rx_data_check
	call	tx_data_check
	call	timer_check
	jr	loop

;##### Subroutines #####
led_display:
	ld	c,0
	ld	a,(led)
	bit	0,a
	jr	z,1$
	set	7,c
1$:
	bit	1,a
	jr	z,2$
	set	6,c
2$:
	bit	2,a
	jr	z,3$
	set	5,c
3$:
	bit	3,a
	jr	z,4$
	set	4,c
4$:
	bit	4,a
	jr	z,5$
	set	3,c
5$:
	bit	5,a
	jr	z,6$
	set	2,c
6$:
	bit	6,a
	jr	z,7$
	set	1,c
7$:
	bit	7,a
	jr	z,8$
	set	0,c
8$:
	ld	a,c
	xor	0ffh
	out	(pio_a+0),a
	ret
get_mask:
	ld	c,0
	ld	a,(ch_ctr)
	cp	0
	jr	nz,11$
	set	0,c
	jr	18$
11$:
	cp	1
	jr	nz,12$
	set	1,c
	jr	18$
12$:
	cp	2
	jr	nz,13$
	set	2,c
	jr	18$
13$:
	cp	3
	jr	nz,14$
	set	3,c
	jr	18$
14$:
	cp	4
	jr	nz,15$
	set	4,c
	jr	18$
15$:
	cp	5
	jr	nz,16$
	set	5,c
	jr	18$
16$:
	cp	6
	jr	nz,17$
	set	6,c
	jr	18$
17$:
	cp	7
	jr	nz,18$
	set	7,c
18$:
	ld	a,c
	ret
tx_data_check:
	ld	de,(tx_end)
	ld	hl,(tx_top)
	and	a			; CY <-- 0
	sbc	hl,de
	ret	z
	io_set	sio_a,00000000b		; Resister Point = 0
	in	a,(sio_a+1)
	bit	2,a
	ret	z
	ld	a,10100000b
	or	d
	ld	h,a
	ld	l,e
	ld	a,(hl)
	out	(sio_a),a
	inc	de
	res	5,d
	ld	(tx_end),de
	ret
tx_fifo_set:
	push	hl
	push	de
	ld	de,(tx_top)
	ld	a,10100000b
	or	d
	ld	h,a
	ld	l,e
	ld	(hl),b
	inc	de
	res	5,d
	ld	(tx_top),de
	pop	de
	pop	hl
	ret
rx_data_check:
	ld	de,(rx_end)
	ld	hl,(rx_top)
	and	a			; CY <-- 0
	sbc	hl,de
	ret	z
	ld	a,10000000b
	or	d
	ld	h,a
	ld	l,e
	ld	b,(hl)
	inc	de
	res	5,d
	ld	(rx_end),de
	bit	7,b
	jr	z,50$			; running
	ld	a,b
	cp	0f8h
	ret	nc
	cp	0f0h
	jr	c,10$
	xor	a
	ld	(rsb),a
	ret
10$:
	ld	a,b
	and	00001111b
	ld	(channel),a
	ld	a,b
	and	11110000b
	ld	(rsb),a
	xor	a
	ld	(dcb),a
	ret
50$:
	ld	a,(rsb)
	cp	0
	ret	z
	cp	0c0h
	jr	z,70$
	cp	0d0h
	jr	z,70$
	ld	a,(dcb)
	cp	0
	jr	nz,90$
	inc	a
	ld	(dcb),a
	ld	a,b
	ld	(keyno),a
	ret
70$:
	ld	c,b
	ld	a,(rsb)
	ld	d,a
	ld	a,(channel)
	or	d
	ld	b,a
	call	tx_fifo_set
	ld	b,c
	call	tx_fifo_set
	ret
90$:
	xor	a
	ld	(dcb),a
	ld	c,b
	ld	a,(rsb)
	ld	d,a
	ld	a,(channel)
	or	d
	ld	b,a
	call	tx_fifo_set
	ld	a,(keyno)
	ld	b,a
	call	tx_fifo_set
	ld	b,c
	call	tx_fifo_set
	ld	a,(rsb)
	cp	0a0h
	ret	nz
	ld	a,(channel)
	cp	00eh
	ret	nz
	ld	a,(keyno)
	cp	0
	jr	z,_00
	cp	1
	jr	z,_01
	cp	2
	jr	z,_02
	ret
_00:
	ld	a,c
	ld	(para+0),a
	ret
_01:
	ld	a,c
	ld	(para+1),a
	ret
_02:
	ld	a,c
	ld	(para+2),a
	ret
timer_check:
	ld	a,(timer)
	dec	a
	ld	(timer),a
	cp	0
	ret	nz
	ld	a,(para+0)		; Init = 10
	ld	(timer),a
	ld	a,(ch_ctr)
	inc	a
	and	00000111b
	ld	(ch_ctr),a
	ld	e,a
	ld	d,0
	ld	hl,ch_status
	add	hl,de			; (hl) = address
	ld	a,(hl)			; Get Channel Status
	cp	0
	jr	z,_sw_check
	dec	a
	ld	(hl),a
	cp	0
	jr	nz,_tim_1
	call	get_mask
	xor	0ffh
	ld	b,a
	ld	a,(led)
	and	b
	ld	(led),a
	call	led_display
	ret
_tim_1:
	ld	b,a
	ld	a,(para+1)		; Init = 20
	cp	b
	ret	nz
	ld	b,09fh
	call	tx_fifo_set
	ld	a,(ch_ctr)
	ld	b,a
	call	tx_fifo_set
	ld	b,0
	call	tx_fifo_set
	ret
_sw_check:
	call	get_mask
	ld	e,a			; Mask
	in	a,(pio_b)
	xor	0ffh
	and	e
	ld	c,a			; New Status
	ld	a,(sw)
	and	e
	ld	d,a			; Old Status
	cp	c
	ret	z			; not event
	ld	a,e
	xor	0ffh
	ld	b,a
	ld	a,(sw)
	and	b
	or	c
	ld	(sw),a
	ld	a,c
	cp	0
	ret	z			; off event
	ld	b,09fh
	call	tx_fifo_set
	ld	a,(ch_ctr)
	ld	b,a
	call	tx_fifo_set
	ld	b,1
	call	tx_fifo_set
	ld	a,(para+2)		; Init = 25
	ld	(hl),a
	ld	a,(led)
	or	e
	ld	(led),a
	call	led_display
	ret

	end

Z80アセンブラ その5 ビデオエフェクタ用リレーBOX

;-----------------------------------------------------------------------
;	[PEGASUS-9] Relay Box for Video Effect Switcher : Nov. 1994
;-----------------------------------------------------------------------

;##### RAM Map #####
	dseg
	org	0000h
rx_fifo		ds	8192
rx_top		ds	2
rx_end		ds	2
rsb		ds	1
dcb		ds	1
channel		ds	1
keyno		ds	1
relay		ds	2
led		ds	2
mode		ds	1
timer		ds	3
pointer		ds	1

;##### I/O Map #####
	cseg
sio_a		equ	0018h
sio_b		equ	001ah
pio_a		equ	001ch
pio_b		equ	001eh

;##### MACRO #####
io_set	macro	@1,@2
	ld	a,@2
	out	(@1+1),a
	endm
io_put	macro	@1,@2
	ld	a,@2
	out	(@1+0),a
	endm

;##### RESET #####
	org	0000h
	ld      sp,0ffffh
	di
	jp      main

;##### INT / NMI #####
	org     0020h
	dw	_midi_
_midi_:
	ex	af,af'
	exx
	ld	de,(rx_top)
	ld	a,10000000b
	or	d
	ld	h,a
	ld	l,e
	in	a,(sio_a+0)
	ld	(hl),a
	inc	de
	res	5,d
	ld	(rx_top),de
	exx
	ex	af,af'
	ei
	reti
	org     0066h
	retn

;##### Main #####
main:
	ld      hl,08000h
	ld      a,0f0h
_ram_clear_loop:
	ld      (hl),0
	inc     hl
	cp      h
	jr	nc,_ram_clear_loop
	io_set	pio_a,0cfh		; Mode 3
	io_set	pio_a,00000000b		; 0:Out / 1:In
	io_set	pio_a,007h		; Interrupt Disable
	io_set	pio_b,0cfh		; Mode 3
	io_set	pio_b,00000000b		; 0:Out / 1:In
	io_set	pio_b,007h		; Interrupt Disable
	io_set	sio_b,00011000b		; Channel Reset B
	io_set	sio_b,00000100b		; Resister Point = 4
	io_set	sio_b,11000100b		; Mode
	io_set	sio_b,00000001b		; Resister Point = 1
	io_set	sio_b,00000000b		; Interrupt Mode
	io_set	sio_b,00000010b		; Resister Point = 2
	io_set	sio_b,20h		; Vector Address
	io_set	sio_a,00011000b		; Channel Reset A
	io_set	sio_a,00000100b		; Resister Point = 4
	io_set	sio_a,11000100b		; Mode
	io_set	sio_a,00000001b		; Resister Point = 1
	io_set	sio_a,00010000b		; Interrupt Mode
	io_set	sio_a,00000011b		; Resister Point = 3
	io_set	sio_a,11000001b		; Receive Start
	im	2
	ei
	in	a,(sio_a+0)		; dummy read
	call	relay_output
	call	led_output
loop:
	call	not_busy
	call	rx_data_check
	jr	loop

;##### Subroutines #####
out_1_8:
	xor	11111111b
	out	(c),a
	ret
out_9_10:
	ld	b,a
	ld	a,00000101b		; Resister Point = 5
	out	(c),a
	ld	a,00001000b
	bit	0,b			; No. 9 check
	jr	z,_9_10_1
	set	7,a			; DTR ON
_9_10_1:
	out	(c),a
	ret
relay_output:
	ld	a,(relay+0)
	xor	11111111b
	ld	c,pio_a
	call	out_1_8
	ld	a,(relay+1)
	xor	11111111b
	ld	c,sio_a+1
	call	out_9_10
	ret
led_output:
	ld	a,(led+0)
	ld	c,pio_b
	call	out_1_8
	ld	a,(led+1)
	ld	c,sio_b+1
	call	out_9_10
	ret
rx_data_check:
	ld	de,(rx_end)
	ld	hl,(rx_top)
	and	a			; CY <-- 0
	sbc	hl,de
	ret	z
	ld	a,10000000b
	or	d
	ld	h,a
	ld	l,e
	ld	b,(hl)
	inc	de
	res	5,d
	ld	(rx_end),de
	bit	7,b
	jr	z,50$			; running
	ld	a,b
	cp	0f8h
	ret	nc
	cp	0f0h
	jr	c,10$
	xor	a
	ld	(rsb),a
	ret
10$:
	ld	a,b
	and	00001111b
	ld	(channel),a
	ld	a,b
	and	11110000b
	ld	(rsb),a
	xor	a
	ld	(dcb),a
	ret
50$:
	ld	a,(rsb)
	cp	0
	ret	z
	cp	0c0h
	ret	z
	cp	0d0h
	ret	z
	ld	a,(dcb)
	cp	0
	jr	nz,90$
	inc	a
	ld	(dcb),a
	ld	a,b
	ld	(keyno),a
	ret
90$:
	xor	a
	ld	(dcb),a
	ld	a,(rsb)
	cp	0a0h
	ret	nz
	ld	a,(channel)
	cp	00eh
	ret	nz
	ld	a,(keyno)
	cp	11
	ret	c
	cp	20
	ret	nc
	ld	a,1
	ld	(mode),a
	ld	a,(relay+0)
	ld	d,a
	ld	a,(keyno)
	cp	11
	jp	z,_11
	cp	12
	jp	z,_12
	cp	13
	jp	z,_13
	cp	14
	jp	z,_14
	cp	15
	jp	z,_15
	cp	16
	jp	z,_16
	cp	17
	jp	z,_17
	cp	18
	jp	z,_18
	cp	19
	ret	nz
	ld	a,(relay+1)
	res	0,a
	bit	6,b
	jr	z,_19_mix
	set	0,a
_19_mix:
	ld	(relay+1),a
	ld	(led+1),a
	call	relay_output
	call	led_output
	ret
_11:
	res	0,d
	bit	6,b
	jp	z,_all_mix
	set	0,d
	jp	_all_mix
_12:
	res	1,d
	bit	6,b
	jp	z,_all_mix
	set	1,d
	jp	_all_mix
_13:
	res	2,d
	bit	6,b
	jp	z,_all_mix
	set	2,d
	jp	_all_mix
_14:
	res	3,d
	bit	6,b
	jp	z,_all_mix
	set	3,d
	jp	_all_mix
_15:
	res	4,d
	bit	6,b
	jp	z,_all_mix
	set	4,d
	jp	_all_mix
_16:
	res	5,d
	bit	6,b
	jp	z,_all_mix
	set	5,d
	jp	_all_mix
_17:
	res	6,d
	bit	6,b
	jp	z,_all_mix
	set	6,d
	jp	_all_mix
_18:
	res	7,d
	bit	6,b
	jp	z,_all_mix
	set	7,d
	jp	_all_mix
_all_mix:
	ld	a,d
	ld	(relay+0),a
	ld	(led+0),a
	call	relay_output
	call	led_output
	ret

;##### Hobby(^_^;) #####
not_busy:
	ld	a,(mode)
	cp	0
	ret	nz
	ld	a,(timer+0)
	dec	a
	ld	(timer+0),a
	cp	0
	ret	nz
	ld	a,(timer+1)
	dec	a
	ld	(timer+1),a
	cp	0
	ret	nz
	ld	a,15
	ld	(timer+1),a
	ld	a,(pointer)
	ld	c,a
	ld	b,0
	ld	hl,table
	add	hl,bc
	ld	a,(hl)
	ld	(led+0),a
	call	led_output
	ld	a,(pointer)
	inc	a
	ld	(pointer),a
	cp	66
	ret	nz
	ld	a,0
	ld	(pointer),a
	ret
table:
	db	00000001b,00000100b,00010000b,01000000b		; 0-3
	db	00010000b,00000100b,00000001b			; 4-6
	db	00000010b,00001000b,00100000b,10000000b		; 7-10
	db	00100000b,00001000b,00000010b			; 11-13
	db	00000001b,00000010b,00000100b,00001000b		; 14-17
	db	00010000b,00100000b,01000000b,10000000b		; 18-21
	db	01000000b,00100000b,00010000b,00001000b		; 22-25
	db	00000100b,00000010b,00000001b			; 26-28
	db	00000010b,00001000b,00100000b			; 48-50
	db	10000000b,01100000b,00011000b,00000110b		; 29-32
	db	00000001b,00000110b,00011000b,01100000b		; 33-36
	db	10000000b,11100000b,01111000b,00011110b		; 37-40
	db	00000111b,00000001b,00000111b,00011110b		; 41-44
	db	01111000b,11100000b,10000000b			; 45-47
	db	11000000b,11010000b,11010100b,11010101b		; 51-54
	db	11010111b,11011111b,11111111b			; 55-57
	db	11111101b,11110101b,11010101b,01010101b		; 58-61
	db	00010101b,00000101b,00000001b,00000000b		; 62-65

	end

Z80アセンブラ その6 Indy用MIDIフィルタ

;-----------------------------------------------------------------------
;	[PEGASUS-10] : [Indy Box] CPU (2) Indy Filter : May 1995
;-----------------------------------------------------------------------
;	[AE] [nn] [para] : Polyphonic Key Pressure
;		nn =  5 : 100 * n	LED Display
;		      6 :  10 * n	LED Display
;		      7 :   1 * n	LED Display
;		      8 :  (off) 	LED OFF
;		     21 : Channel Filter bitmap : CH1-CH4 [1=on]
;		     22 : Channel Filter bitmap : CH5-CH8 [1=on]
;		     23 : Channel Filter bitmap : CH9-CH12 [1=on]
;		     24 : Channel Filter bitmap : CH13-CH16 [1=on]
;						(Default : CH16 only ON)
;		     25 : Status Filter bitmap : [1=on]
;                              bit6   5   4   3   2   1   0
;				 8n  9n  An  Bn  Cn  Dn  En
;						(Default :  ON)
;		     26 : Setting Default Paratemers (only  ON)
;-----------------------------------------------------------------------

;##### RAM Map #####
	dseg
	org	0000h
rx_fifo		ds	8192
tx_fifo		ds	8192
rx_top		ds	2
rx_end		ds	2
rsb		ds	1
dcb		ds	1
channel		ds	1
keyno		ds	1
rxdata		ds	1
tx_top		ds	2
tx_end		ds	2
timer		ds	3
pass_map	ds	256
mode		ds	1
map_0_3		ds	1
map_4_7		ds	1
map_8_B		ds	1
map_C_F		ds	1
map_status	ds	1

;##### I/O Map #####
	cseg
sio_b		equ	001ah
pio_a		equ	001ch
pio_b		equ	001eh

;##### MACRO #####
io_set	macro	@1,@2
	ld	a,@2
	out	(@1+1),a
	endm
io_put	macro	@1,@2
	ld	a,@2
	out	(@1+0),a
	endm

;##### RESET #####
	org	0000h
	ld      sp,0ffffh
	di
	jp      main

;##### INT / NMI #####
	org     0020h
	dw	_midi_
_midi_:
	ex	af,af'
	exx
	ld	de,(rx_top)
	ld	a,10000000b
	or	d
	ld	h,a
	ld	l,e
	in	a,(sio_b+0)
	ld	(hl),a
	inc	de
	res	5,d
	ld	(rx_top),de
	exx
	ex	af,af'
	ei
	reti
	org     0066h
	retn

;##### Main #####
main:
	ld      hl,08000h
	ld      c,0e0h
_ram_check_loop:
	ld	b,11111111b
	ld      (hl),b
	ld	a,(hl)
	cp	b
	jr	nz,_ram_check_loop
	ld	b,00000000b
	ld      (hl),b
	ld	a,(hl)
	cp	b
	jr	nz,_ram_check_loop
	inc     hl
	ld	a,c
	cp      h
	jr	nc,_ram_check_loop
	io_set	pio_a,0cfh		; Mode 3
	io_set	pio_a,00000000b		; 0:Out / 1:In
	io_set	pio_a,007h		; Interrupt Disable
	io_put	pio_a,11111111b		;
	io_set	pio_b,0cfh		; Mode 3
	io_set	pio_b,00000000b		; 0:Out / 1:In
	io_set	pio_b,007h		; Interrupt Disable
	io_put	pio_b,11111111b		;
	io_put	pio_a,11111000b		;
	io_put	pio_a,11111111b		;
	io_set	sio_b,00011000b		; Channel Reset B
	io_set	sio_b,00000100b		; Resister Point = 4
	io_set	sio_b,01000100b		; Mode
	io_set	sio_b,00000001b		; Resister Point = 1
	io_set	sio_b,00011000b		; Interrupt Mode
	io_set	sio_b,00000010b		; Resister Point = 2
	io_set	sio_b,020h		; Vector Address
	xor	a
	ld	i,a			; High Address
	io_set	sio_b,00000101b		; Resister Point = 5
	io_set	sio_b,01101000b		; Transmit Start
	io_set	sio_b,00000011b		; Resister Point = 3
	io_set	sio_b,11000001b		; Receive Start
	call	default_set
	im	2
	ei
	in	a,(sio_b+0)		; dummy read
loop:
	call	rx_data_check
	call	tx_fifo_check
	call	timer_check
	jr	loop

tx_fifo_set:
	push	hl
	push	de
	ld	de,(tx_top)
	ld	a,10100000b
	or	d
	ld	h,a
	ld	l,e
	ld	(hl),b
	inc	de
	res	5,d
	ld	(tx_top),de
	pop	de
	pop	hl
	ret
tx_fifo_check:
	ld	de,(tx_end)
	ld	hl,(tx_top)
	and	a			; CY <-- 0
	sbc	hl,de
	ret	z
	io_set	sio_b,00000000b		; Resister Point = 0
	in	a,(sio_b+1)
	bit	2,a
	ret	z
	ld	a,10100000b
	or	d
	ld	h,a
	ld	l,e
	ld	a,(hl)
	out	(sio_b+0),a
	inc	de
	res	5,d
	ld	(tx_end),de
	ret

timer_check:
	ld	a,(mode)
	cp	0
	ret	nz
	ld	a,(timer+0)
	inc	a
	ld	(timer+0),a
	ret	nz
	ld	a,(timer+1)
	inc	a
	ld	(timer+1),a
	and	00001111b
	ret	nz
	ld	a,(timer+2)
	inc	a
	cp	13
	jr	nz,_demo
	xor	a
_demo:
	ld	(timer+2),a
	call	led_display
	ld	a,11111011b
	out	(pio_a+0),a
	ld	a,11111111b
	out	(pio_a+0),a
	ld	a,(timer+2)
	dec	a
	call	led_display
	ld	a,11111101b
	out	(pio_a+0),a
	ld	a,11111111b
	out	(pio_a+0),a
	ld	a,(timer+2)
	dec	a
	dec	a
	call	led_display
	ld	a,11111110b
	out	(pio_a+0),a
	ld	a,11111111b
	out	(pio_a+0),a
	ret

rx_data_check:
	ld	de,(rx_end)
	ld	hl,(rx_top)
	and	a			; CY <-- 0
	sbc	hl,de
	ret	z
	ld	a,10000000b
	or	d
	ld	h,a
	ld	l,e
	ld	b,(hl)
	inc	de
	res	5,d
	ld	(rx_end),de
	bit	7,b
	jr	z,50$			; running
	ld	a,b
	cp	0f8h
	ret	nc
	cp	0f0h
	jr	c,10$
	xor	a
	ld	(rsb),a
	ret
10$:
	ld	a,b
	and	00001111b
	ld	(channel),a
	ld	a,b
	and	11110000b
	ld	(rsb),a
	xor	a
	ld	(dcb),a
	ret
50$:
	ld	a,(rsb)
	cp	0
	ret	z
	cp	0c0h
	jr	z,70$
	cp	0d0h
	jr	z,70$
	ld	a,(dcb)
	cp	0
	jr	nz,90$
	inc	a
	ld	(dcb),a
	ld	a,b
	ld	(keyno),a
	ret
70$:
	ld	c,b
	ld	a,(rsb)
	ld	d,a
	ld	a,(channel)
	or	d
	ld	e,a
	ld	d,0
	ld	hl,pass_map
	add	hl,de
	ld	a,(hl)
	cp	0
	ret	z
	ld	b,e
	call	tx_fifo_set
	ld	b,c
	call	tx_fifo_set
	call	st_disp
	ret
90$:
	ld	a,b
	ld	(rxdata),a
	xor	a
	ld	(dcb),a
	ld	c,b
	ld	a,(rsb)
	ld	d,a
	ld	a,(channel)
	or	d
	ld	e,a
	ld	d,0
	ld	hl,pass_map
	add	hl,de
	ld	a,(hl)
	cp	0
	jr	z,_passed
	ld	b,e
	call	tx_fifo_set
	ld	a,(keyno)
	ld	b,a
	call	tx_fifo_set
	ld	b,c
	call	tx_fifo_set
	call	st_disp
_passed:
	ld	a,(rsb)
	cp	0a0h
	ret	nz
	ld	a,(channel)
	cp	00eh
	ret	nz
	ld	a,(keyno)
	cp	5
	jr	z,__100
	cp	6
	jr	z,__10
	cp	7
	jr	z,__1
	cp	8
	jr	z,__0
	cp	21
	jp	z,__21
	cp	22
	jp	z,__22
	cp	23
	jp	z,__23
	cp	24
	jp	z,__24
	cp	25
	jp	z,__25
	cp	26
	jp	z,__26
	ret
__100:
	ld	a,c
	call	led_display
	ld	a,11111110b
	out	(pio_a+0),a
	ld	a,11111111b
	out	(pio_a+0),a
	ld	a,2
	ld	(mode),a
	ret
__10:
	ld	a,c
	call	led_display
	ld	a,11111101b
	out	(pio_a+0),a
	ld	a,11111111b
	out	(pio_a+0),a
	ld	a,2
	ld	(mode),a
	ret
__1:
	ld	a,c
	call	led_display
	ld	a,11111011b
	out	(pio_a+0),a
	ld	a,11111111b
	out	(pio_a+0),a
	ld	a,2
	ld	(mode),a
	ret
__0:
	ld	a,0fh
	call	led_display
	ld	a,11111000b
	out	(pio_a+0),a
	ld	a,11111111b
	out	(pio_a+0),a
	ld	a,2
	ld	(mode),a
	ret
led_display:
	and	00001111b
	ld	l,a
	ld	h,0
	ld	bc,data_table
	add	hl,bc
	ld	a,(hl)
	out	(pio_b+0),a
	ret
data_table:
	db	10010000b,11011011b,10100010b,10001010b		; 0-3
	db	11001001b,10001100b,10000100b,11011000b		; 4-7
	db	10000000b,11001000b,11111111b,11111111b		; 8-B
	db	11111111b,11111111b,11111111b,11111111b		; C-F

default_set:
	ld	hl,pass_map
	ld	a,0
_df_1:
	ld	(hl),0
	inc	hl
	dec	a
	jr	nz,_df_1
	ld	a,1
	ld	(pass_map+0bfh),a
	ld	a,0
	ld	(map_0_3),a
	ld	(map_4_7),a
	ld	(map_8_B),a
	ld	a,00001000b
	ld	(map_C_F),a
	ld	(map_status),a
	ret

st_disp:
	ld	a,(mode)
	cp	2
	ret	z
	ld	a,1
	ld	(mode),a
	res	7,e
	ld	d,0
	ld	hl,table_100
	add	hl,de
	ld	a,(hl)
	call	led_display
	ld	a,11111110b
	out	(pio_a+0),a
	ld	a,11111111b
	out	(pio_a+0),a
	ld	hl,table_10
	add	hl,de
	ld	a,(hl)
	call	led_display
	ld	a,11111101b
	out	(pio_a+0),a
	ld	a,11111111b
	out	(pio_a+0),a
	ld	hl,table_1
	add	hl,de
	ld	a,(hl)
	call	led_display
	ld	a,11111011b
	out	(pio_a+0),a
	ld	a,11111111b
	out	(pio_a+0),a
	ret
table_1:
	db	8,9				; 128-129
	db	0,1,2,3,4,5,6,7,8,9		; 130-139
	db	0,1,2,3,4,5,6,7,8,9		; 140-149
	db	0,1,2,3,4,5,6,7,8,9		; 150-159
	db	0,1,2,3,4,5,6,7,8,9		; 160-169
	db	0,1,2,3,4,5,6,7,8,9		; 170-179
	db	0,1,2,3,4,5,6,7,8,9		; 180-189
	db	0,1,2,3,4,5,6,7,8,9		; 190-199
	db	0,1,2,3,4,5,6,7,8,9		; 200-209
	db	0,1,2,3,4,5,6,7,8,9		; 210-219
	db	0,1,2,3,4,5,6,7,8,9		; 220-229
	db	0,1,2,3,4,5,6,7,8,9		; 230-239
	db	0,1,2,3,4,5,6,7,8,9		; 240-249
	db	0,1,2,3,4,5        		; 250-255
table_10:
	db	2,2				; 128-129
	db	3,3,3,3,3,3,3,3,3,3		; 130-139
	db	4,4,4,4,4,4,4,4,4,4		; 140-149
	db	5,5,5,5,5,5,5,5,5,5		; 150-159
	db	6,6,6,6,6,6,6,6,6,6		; 160-169
	db	7,7,7,7,7,7,7,7,7,7		; 170-179
	db	8,8,8,8,8,8,8,8,8,8		; 180-189
	db	9,9,9,9,9,9,9,9,9,9		; 190-199
	db	0,0,0,0,0,0,0,0,0,0		; 200-209
	db	1,1,1,1,1,1,1,1,1,1		; 210-219
	db	2,2,2,2,2,2,2,2,2,2		; 220-229
	db	3,3,3,3,3,3,3,3,3,3		; 230-239
	db	4,4,4,4,4,4,4,4,4,4		; 240-249
	db	5,5,5,5,5,5        		; 250-255
table_100:
	db	1,1				; 128-129
	db	1,1,1,1,1,1,1,1,1,1		; 130-139
	db	1,1,1,1,1,1,1,1,1,1		; 140-149
	db	1,1,1,1,1,1,1,1,1,1		; 150-159
	db	1,1,1,1,1,1,1,1,1,1		; 160-169
	db	1,1,1,1,1,1,1,1,1,1		; 170-179
	db	1,1,1,1,1,1,1,1,1,1		; 180-189
	db	1,1,1,1,1,1,1,1,1,1		; 190-199
	db	2,2,2,2,2,2,2,2,2,2		; 200-209
	db	2,2,2,2,2,2,2,2,2,2		; 210-219
	db	2,2,2,2,2,2,2,2,2,2		; 220-229
	db	2,2,2,2,2,2,2,2,2,2		; 230-239
	db	2,2,2,2,2,2,2,2,2,2		; 240-249
	db	2,2,2,2,2,2        		; 250-255

__26:
	call	default_set
	ld	a,0fh
	call	led_display
	ld	a,11111000b
	out	(pio_a+0),a
	ld	a,11111111b
	out	(pio_a+0),a
	ld	a,1
	ld	(mode),a
	ret
__21:
	ld	a,(rxdata)
	and	00001111b
	ld	(map_0_3),a
	call	map_setting
	ret
__22:
	ld	a,(rxdata)
	and	00001111b
	ld	(map_4_7),a
	call	map_setting
	ret
__23:
	ld	a,(rxdata)
	and	00001111b
	ld	(map_8_B),a
	call	map_setting
	ret
__24:
	ld	a,(rxdata)
	and	00001111b
	ld	(map_C_F),a
	call	map_setting
	ret
__25:
	ld	a,(rxdata)
	and	01111111b
	ld	(map_status),a
	call	map_setting
	ret
map_setting:
	ld	hl,pass_map+128
	ld	a,128
_mps_loop:
	ld	(hl),0
	inc	hl
	dec	a
	jr	nz,_mps_loop
	ld	e,0			; Channel
	ld	a,(map_0_3)
	bit	0,a
	jr	z,_mps_0
	call	and_status
_mps_0:
	ld	e,1			; Channel
	ld	a,(map_0_3)
	bit	1,a
	jr	z,_mps_1
	call	and_status
_mps_1:
	ld	e,2			; Channel
	ld	a,(map_0_3)
	bit	2,a
	jr	z,_mps_2
	call	and_status
_mps_2:
	ld	e,3			; Channel
	ld	a,(map_0_3)
	bit	3,a
	jr	z,_mps_3
	call	and_status
_mps_3:
	ld	e,4			; Channel
	ld	a,(map_4_7)
	bit	0,a
	jr	z,_mps_4
	call	and_status
_mps_4:
	ld	e,5			; Channel
	ld	a,(map_4_7)
	bit	1,a
	jr	z,_mps_5
	call	and_status
_mps_5:
	ld	e,6			; Channel
	ld	a,(map_4_7)
	bit	2,a
	jr	z,_mps_6
	call	and_status
_mps_6:
	ld	e,7			; Channel
	ld	a,(map_4_7)
	bit	3,a
	jr	z,_mps_7
	call	and_status
_mps_7:
	ld	e,8			; Channel
	ld	a,(map_8_B)
	bit	0,a
	jr	z,_mps_8
	call	and_status
_mps_8:
	ld	e,9			; Channel
	ld	a,(map_8_B)
	bit	1,a
	jr	z,_mps_9
	call	and_status
_mps_9:
	ld	e,10			; Channel
	ld	a,(map_8_B)
	bit	2,a
	jr	z,_mps_10
	call	and_status
_mps_10:
	ld	e,11			; Channel
	ld	a,(map_8_B)
	bit	3,a
	jr	z,_mps_11
	call	and_status
_mps_11:
	ld	e,12			; Channel
	ld	a,(map_C_F)
	bit	0,a
	jr	z,_mps_12
	call	and_status
_mps_12:
	ld	e,13			; Channel
	ld	a,(map_C_F)
	bit	1,a
	jr	z,_mps_13
	call	and_status
_mps_13:
	ld	e,14			; Channel
	ld	a,(map_C_F)
	bit	2,a
	jr	z,_mps_14
	call	and_status
_mps_14:
	ld	e,15			; Channel
	ld	a,(map_C_F)
	bit	3,a
	jr	z,_mps_15
	call	and_status
_mps_15:
	ret

and_status:
	ld	bc,pass_map
	ld	a,(map_status)
	bit	6,a
	jr	z,_ans_6
	ld	a,e
	or	080h
	ld	l,a
	ld	h,0
	add	hl,bc
	ld	a,1
	ld	(hl),a
_ans_6:
	ld	a,(map_status)
	bit	5,a
	jr	z,_ans_5
	ld	a,e
	or	090h
	ld	l,a
	ld	h,0
	add	hl,bc
	ld	a,1
	ld	(hl),a
_ans_5:
	ld	a,(map_status)
	bit	4,a
	jr	z,_ans_4
	ld	a,e
	or	0a0h
	ld	l,a
	ld	h,0
	add	hl,bc
	ld	a,1
	ld	(hl),a
_ans_4:
	ld	a,(map_status)
	bit	3,a
	jr	z,_ans_3
	ld	a,e
	or	0b0h
	ld	l,a
	ld	h,0
	add	hl,bc
	ld	a,1
	ld	(hl),a
_ans_3:
	ld	a,(map_status)
	bit	2,a
	jr	z,_ans_2
	ld	a,e
	or	0c0h
	ld	l,a
	ld	h,0
	add	hl,bc
	ld	a,1
	ld	(hl),a
_ans_2:
	ld	a,(map_status)
	bit	1,a
	jr	z,_ans_1
	ld	a,e
	or	0d0h
	ld	l,a
	ld	h,0
	add	hl,bc
	ld	a,1
	ld	(hl),a
_ans_1:
	ld	a,(map_status)
	bit	0,a
	jr	z,_ans_0
	ld	a,e
	or	0e0h
	ld	l,a
	ld	h,0
	add	hl,bc
	ld	a,1
	ld	(hl),a
_ans_0:
	ret

	end

6502アセンブラ その1 DSPデバッガ

;-----------------------------------------------------------------------
;       DSP-01 Debugger Software
;                                               Produced by Y.Nagashima
;-----------------------------------------------------------------------
name  main
p65c02

;##### Zero Page Registers #####
zpage
; 
        midi_r_top  ds    1     ; MIDI Receive FIFO Top
        midi_r_end  ds    1     ; MIDI Receive FIFO End
        midi_t_top  ds    1     ; MIDI Transmit FIFO Top
        midi_t_end  ds    1     ; MIDI Transmit FIFO Top
        midi_rsb    ds    1     ; Running Status Buffer
        midi_dcb    ds    1     ; Data Count Buffer
        rxdata      ds    1     ; MIDI Received Data
        rxkeyno     ds    1     ; MIDI Received Key Number
        rxspeed     ds    1     ; MIDI Received Velocity
        rxchannel   ds    1     ; MIDI Received Channel
; 
        work        ds   16     ; Work Area
        led_demo    ds    2     ; LED Demonstration Work
        add_s_h_1   ds    1     ; 16bit Adder/Sub Source (H) #1
        add_s_l_1   ds    1     ; 16bit Adder/Sub Source (L) #1
        add_s_h_2   ds    1     ; 16bit Adder/Sub Source (H) #2
        add_s_l_2   ds    1     ; 16bit Adder/Sub Source (L) #2
        add_d_h     ds    1     ; 16bit Adder/Sub Destination (H)
        add_d_l     ds    1     ; 16bit Adder/Sub Destination (L)
        bank_h      ds    1     ; Memory Bank Buffer(H)
        bank_l      ds    1     ; Memory Bank Buffer(L)
        bank_buff_l ds    1     ; Memory Bank Stack Buffer(L)
        bank_buff_h ds    1     ; Memory Bank Stack Buffer(H)
        screen_mode ds    1     ; Main Menu Mode
; 
        comp_level  ds    1     ; Mouse Sleshold Level
        msx_timer   ds    1     ; MSX Mouse Scan Timer
        msx_sw_buf  ds    1     ; Bit5=Left,Bit4=Right : Buffer
        msx_x_data  ds    1     ; MSX Mouse Move Data (X)
        msx_y_data  ds    1     ; MSX Mouse Move Data (Y)
        mouse_sw    ds    1     ; Bit5=Left,Bit4=Right : ON Event="1"
        up_count    ds    1     ; Mouse Up Address
        down_count  ds    1     ; Mouse Down Address
        right_count ds    1     ; Mouse Right Address
        left_count  ds    1     ; Mouse Left Address
        mouse_timer ds    1     ; Mouse Check Routine Timer
        main_point  ds    1     ; Main Menu Mode Pointer
; 
        moni_point  ds    1     ; MIDI Monitor Mode Address
        erase_point ds    1     ; MIDI Monitor Erase Address
        lcd_era_h   ds    1     ; LCD Hex Erase Address (H)
        lcd_add_h   ds    1     ; LCD Hex Display Address (H)
        lcd_add_l   ds    1     ; LCD Hex Display Address (L)
        lcd_hex_data ds   1     ; LCD Hex Display Data
        mask_buffer ds    2     ; seloff buffer
; 
        pcm_point   ds    1     ; PCM Module Mode Point
        pcm_plane   ds    1     ; PCM Module Mode Display Plane NO.(0-F)
; 
        dbg_point   ds    1     ; DEBUG Mode Point
        dbg_plane   ds    1     ; DEBUG Mode Display Plane NO.(0-F)
        dbg_bk      ds   16
        dbg_st      ds   16
        dbg_lt      ds   16
        dbg_le      ds   16
        dbg_fnoh    ds   16
        dbg_fnom    ds   16
        dbg_fnol    ds   16
; 
        wave_s_add  ds    2     ;
        wave_d_bk   ds    1     ;
        wave_d_add  ds    1     ;
        random_buff ds    1     ;
        basic_ch    ds    1     ; Receive Basic Channel (00-0F)
        basic_note  ds   16     ; Receive Note Number (00-7F)
        basic_tone  ds   16     ; Preset Tone Number  (00-FF)
        basic_pan   ds   16     ; Panpot Data         (00-FF)
        dsp_channel ds    1     ;
        tone_access ds    2     ;
        dsp_event   ds    1     ;
; 
        zp_end      ds    3     ; Dummy=<*>

;##### RAM MAP #####
org     0200h
        midi_r_fifo     ds     256     ; MIDI
        midi_t_fifo     ds     256

;##### I/O,ROM Mapping #####
        dsp-01     equ     01c00h       ; PAL #3  Out=F7=
        bankh      equ     01e00h       ; PAL #3  Out=F6=
        bankl      equ     01e04h       ; PAL #3  Out=F5=
        adsp       equ     01e10h       ; PAL #3  Out=F0-F4=
        uart1      equ     01e20h       ; PAL #2  Out=F0=
        uart2      equ     01e24h       ; PAL #2  Out=F1=
        uart3      equ     01e28h       ; PAL #2  Out=F2=
        timer      equ     01e2ch       ; PAL #2  Out=F3=
        pia        equ     01e30h       ; PAL #2  Out=F4=
        lcd2       equ     01e34h       ; PAL #2  Out=F5= **
        lcd        equ     01e38h       ; PAL #2  Out=F6=
        mpu        equ     01e3ch       ; PAL #2  Out=F7=
        prgarea    equ     0c000h
        debug      equ     0ff00h
        vector     equ     0fffah

;##### Constants Defines #####
        tail    equ     0ffh
        up      equ     00000001b
        down    equ     00000010b
        right   equ     00000100b
        left    equ     00001000b
        sw_r    equ     00010000b
        sw_l    equ     00100000b
        wave_ram_bank   equ     80h

;#####  Macro Call Defines #####
; 
        macro   &startup
                sei
                ldx     #0
                txs
                cld
                endmac
        macro   &movi
                lda     #\0
                sta     \1
                endmac
        macro   &move
                lda     \0
                sta     \1
                endmac
        macro   &equal_return
                bne     *+3
                rts
                endmac
        macro   ¬_equal_return
                beq     *+3
                rts
                endmac
        macro   &bank
                &movi   \0,bankl
                sta     bank_l
                &movi   \1,bankh
                sta     bank_h
                endmac
        macro   &bank_system
                &bank   08h,09h
                endmac
        macro   &bank_wave_l
                &bank   0ah,0bh
                endmac
        macro   &bank_wave_h
                &bank   0ch,0dh
                endmac
        macro   &bank_card_1_l
                &bank   00h,01h
                endmac
        macro   &bank_card_1_h
                &bank   02h,03h
                endmac
        macro   &bank_card_2_l
                &bank   04h,05h
                endmac
        macro   &bank_card_2_h
                &bank   06h,07h
                endmac
        macro   &push_bank
                &move   bank_l,bank_buff_l
                &move   bank_h,bank_buff_h
                endmac
        macro   &pop_bank
                &move   bank_buff_l,bank_l
                sta     bankl
                &move   bank_buff_h,bank_h
                sta     bankh
                endmac
; 
        macro   &comtx
                ldx     midi_t_top
                lda     #\0
                sta     midi_t_fifo,x
                inc     midi_t_top
                endmac
        macro   &memtx
                ldx     midi_t_top
                lda     \0
                sta     midi_t_fifo,x
                inc     midi_t_top
                endmac
; 
        macro   &write
                &movi   .low.\0,work+2
                &movi   .high.\0,work+3
                jsr     lcd_write_message
                endmac
        macro   &selon
                &movi   .low.\0,work+2
                sta     mask_buffer
                &movi   .high.\0,work+3
                sta     mask_buffer+1
                stz     work+4
                jsr     lcd_select_mask
                endmac
        macro   &seloff
                &move   mask_buffer,work+2
                &move   mask_buffer+1,work+3
                &movi   0ffh,work+4
                jsr     lcd_select_mask
                endmac
        macro   &inidraw
                &movi   .low.\0,work
                &movi   .high.\0,work+1
                jsr     lcd_draw_initial_sub
                inc     work+7
                endmac
        macro   &hex_disp_with_point
                &movi   .low.\0,lcd_add_l
                &movi   .high.\0,lcd_add_h
                &move   \1,lcd_hex_data
                jsr     lcd_display_hex_data_with_address
                endmac

org   prgarea

;##### Program Start #####
start:
        &startup
        jsr     io_initial
loop:
        jsr     dsp_event_check
        jsr     rx_midi_sequence
        jsr     tx_midi_sequence
        jsr     mouse_point_check
        jsr     msx_mouse_check
        bra     loop

;##### System I/O Initialize #####
io_initial:
        &movi   0,uart1+1
        &movi   0,uart1+1
        &movi   0,uart1+1
        &movi   01000000b,uart1+1       ; Software Reset
; 
        ldx     #0
        lda     #0
io_initial_loop_1:
        sta     0,x             ; ZP
        inx
        bne     io_initial_loop_1
        ldx     #0
io_initial_loop_2:
        txa
        sta     basic_tone,x    ; Preset Tone NO.
        clc
        adc     #36
        sta     basic_note,x    ; Receive Note : C2 - Eb3
        lda     #1
        sta     dbg_fnoh,x
        lda     #0
        sta     dbg_fnom,x
        sta     dbg_fnol,x
        lda     #0c0h
        sta     dbg_bk,x
        lda     #000h
        sta     dbg_st,x
        sta     dbg_lt,x
        sta     dbg_le,x
        lda     #40h
        sta     basic_pan,x     ; Panpot
        inx
        cpx     #10h
        bne     io_initial_loop_2
; 
        jsr     dsp_initial_setting
; <71051(1) UART Initialize>
        &movi   0,uart1+1
        &movi   0,uart1+1
        &movi   0,uart1+1
        &movi   01000000b,uart1+1       ; Software Reset
        &movi   01001110b,uart1+1       ; Mode Set
        &movi   000-0101b,uart1+1       ; Command Set
; <71051(2) UART Initialize>
        &movi   0,uart2+1
        &movi   0,uart2+1
        &movi   0,uart2+1
        &movi   01000000b,uart2+1       ; Software Reset
        &movi   01001110b,uart2+1       ; Mode Set
        &movi   001-0111b,uart2+1       ; Command Set
; <71051(3) UART Initialize>
        &movi   0,uart3+1
        &movi   0,uart3+1
        &movi   0,uart3+1
        &movi   01000000b,uart3+1       ; Software Reset
        &movi   0100-010b,uart3+1       ; Mode Set
        &movi   000-0111b,uart3+1       ; Command Set
; <71054 Timer Initialize>
        &movi   001-0110b,timer+3       ; UART #2 = 9600 bps
        &movi   32,timer
        &movi   0,timer
        &movi   011-0110b,timer+3       ; UART #3 = 1200 bps
        &movi   0,timer+1
        &movi   1,timer+1
        &movi   -01-0110b,timer+3       ; Timer Interrupt
        &movi   43h,timer+2
        &movi   90h,timer+2
; <71055 PIA Initialize>
        &movi   10010010b,pia+3         ; Port<2>=Out,<0>=<1>=In
        &movi   0ffh,pia+2
; 
        &movi   40h,lcd+1       ; System Set
        &movi   30h,lcd         ; Screen
        &movi   85h,lcd         ; Field(X)
        &movi   7,lcd           ; Field(Y)
        &movi   39,lcd          ; C/R
        &movi   48,lcd          ; TC/R
        &movi   3fh,lcd         ; L/F
        &movi   40,lcd          ; APL
        &movi   0,lcd           ; APH
; 
        &movi   44h,lcd+1       ; Scroll
        &movi   0,lcd           ; Start Address #1(L)
        &movi   0,lcd           ;                 (H)
        &movi   40h,lcd         ; Line #1
        &movi   0,lcd           ; Start Address #2(L)
        &movi   10h,lcd         ;                 (H)
        &movi   40h,lcd         ; Line #2
; 
        &movi   5ah,lcd+1       ; HDot Scroll
        &movi   0,lcd           ; Scroll=0
; 
        &movi   5bh,lcd+1       ; Overlay
        &movi   01h,lcd         ; Character + Graphic
; 
        &movi   58h,lcd+1       ; Display OFF
        &movi   14h,lcd         ; Parameter
; 
        &movi   5dh,lcd+1       ; Cursor Form
        &movi   04h,lcd         ; X=5
        &movi   86h,lcd         ; Y=7
; 
        &movi   46h,lcd+1       ; Cursor Write
        &movi   0,lcd           ; Home
        &movi   0,lcd           ;  Position
; 
        &movi   52h,lcd+1       ; Erase
        jsr     wait_long
        jsr     wait_long
        jsr     wait_long
; 
        &movi   1fh,comp_level  ; Mouse
        &movi   00h,basic_ch    ; Receive Channel    ********
        &movi   '*',zp_end      ; Dummy
        &movi   '*',zp_end+1    ;  //
        &movi   '*',zp_end+2    ;  //
; 
        &bank_system
;  Initial Screen Draw>
        jsr     lcd_draw_initial
        &movi   59h,lcd+1       ; Display ON
; 
        cli
        rts

;##### DSP-01 ON Event --- OFF Write Phase #####
dsp_on_event:
        lda     rxchannel
        cmp     basic_ch
        ¬_equal_return
        ldx     #0
dsp_on_event_keyno_check_loop:
        lda     basic_note,x
        cmp     rxkeyno
        beq     dsp_on_event_keyno_check_just
        inx
        cpx     #10h
        bne     dsp_on_event_keyno_check_loop
        rts
dsp_on_event_keyno_check_just:
        stx     dsp_channel
        lda     screen_mode
        cmp     #3
        bne     dsp_pcm_mode
        jmp     dsp_debug_mode_on_event
dsp_pcm_mode:
        lda     #0
        sta     dsp-01+0c0h,x
        lda     #07h
        sta     dsp-01+0d0h,x
        lda     #0              ; KEY OFF
        sta     dsp-01+090h,x
        &movi   1,dsp_event
        rts

;##### DSP ON Event --- ON Write Phase #####
dsp_event_check:
        lda     dsp_event
        &equal_return
        stz     dsp_event
        ldx     dsp_channel
        &movi   .high.tone_data_base,tone_access+1
        lda     basic_tone,x
        asl     a
        asl     a
        asl     a
        sta     tone_access
        bcc     dsp_on_event_low_basic_tone
        inc     tone_access+1
dsp_on_event_low_basic_tone:
        clc
        lda     #.low.tone_data_base
        adc     tone_access
        sta     tone_access
        bcc     dsp_on_event_low_basic_tone_add
        inc     tone_access+1
dsp_on_event_low_basic_tone_add:
        ldy     #4
        lda     (tone_access),y ; F-No Type
        and     #00000111b
        tay
        lda     fno_type_table_l,y
        ldx     dsp_channel
        sta     dsp-01+00h,x
        lda     fno_type_table_m,y
        sta     dsp-01+10h,x
        lda     fno_type_table_h,y
        sta     dsp-01+20h,x
        ldy     #1
        lda     (tone_access),y ; Start Point
        sta     dsp-01+30h,x
        iny
        lda     (tone_access),y ; Loop Top
        sta     dsp-01+40h,x
        iny
        lda     (tone_access),y ; Loop End
        sta     dsp-01+50h,x
        ldy     #0
        lda     (tone_access),y ; Bank
        sta     dsp-01+60h,x
        lda     basic_pan,x
        sta     dsp-01+70h,x
        ldy     #5
        lda     (tone_access),y ; Attack Level
        sta     dsp-01+0a0h,x
        iny
        lda     (tone_access),y ; Attack Speed
        sta     dsp-01+0b0h,x
        lda     #0              ; Decay Level
        sta     dsp-01+0c0h,x
        iny
        lda     (tone_access),y ; Decay Speed
        sta     dsp-01+0d0h,x
        lda     #00000011b      ; KEY ON
        sta     dsp-01+090h,x
        rts

fno_type_table_l:
        db      000h,000h,000h,000h,000h,000h,000h,000h
fno_type_table_m:
        db      080h,000h,080h,000h,080h,000h,080h,000h
fno_type_table_h:
        db      000h,001h,001h,002h,002h,003h,003h,004h

;##### DSP-01 OFF Event #####
dsp_off_event:
        lda     screen_mode
        cmp     #3
        ¬_equal_return
        lda     rxchannel
        cmp     basic_ch
        ¬_equal_return
        ldx     #0
dsp_off_event_keyno_check_loop:
        lda     basic_note,x
        cmp     rxkeyno
        beq     dsp_off_event_keyno_check_just
        inx
        cpx     #10h
        bne     dsp_off_event_keyno_check_loop
        rts
dsp_off_event_keyno_check_just:
        stx     dsp_channel
        jmp     dsp_debug_mode_off_event
        rts

;##### DSP-01 Initial Setting #####
dsp_initial_setting:
        &movi   0,dsp-01+0f5h           ; Memory Bus Close
        &movi   0,dsp-01+090h
        ldx     #0
        lda     #0
dsp_initial_setting_loop_001:
        sta     dsp-01+090h,x           ; Control Word
        inx
        cpx     #10h
        bne     dsp_initial_setting_loop_001
        &movi   0,random_buff
        lda     random_buff
        and     #11-01111b
        sta     dsp-01+0f1h             ; Random Parameter:Clear
        &movi   00-01111b,dsp-01+0f2h   ; Output Parameter
        &movi   11111111b,dsp-01+0f0h   ; Random Bit Pattern
        ldx     #0
dsp_initial_setting_loop_002:
        lda     #0
        sta     dsp-01+000h,x           ; F-No.(L)
        sta     dsp-01+010h,x           ; F-No.(L)
        sta     dsp-01+020h,x           ; F-No.(L)
        sta     dsp-01+030h,x           ; Start Point
        sta     dsp-01+040h,x           ; Loop Top
        sta     dsp-01+050h,x           ; Loop End
        lda     #wave_ram_bank
        sta     dsp-01+060h,x           ; Bank
        lda     #0
        sta     dsp-01+070h,x           ; Panpot
        sta     dsp-01+0a0h,x           ; Attack Level
        lda     #0fh
        sta     dsp-01+0b0h,x           ; Attack Speed
        lda     #0
        sta     dsp-01+0c0h,x           ; Decay Level
        lda     #0fh
        sta     dsp-01+0d0h,x           ; Decay Speed
        inx
        cpx     #10h
        bne     dsp_initial_setting_loop_002
        lda     random_buff
        ora     #00010000b
        sta     dsp-01+0f1h             ; Random Parameter:Setting
        &movi   1,dsp-01+0f5h           ; Memory Bus Recover
        rts

tone_data_base:
; Format : Bank,Start-Point,Loop-Top,Loop-End,F-No-Type
;          Attack-Level,Attack-Speed,Decay-Speed
        db      0c0h,000h,000h,07fh,0,00fh,007h,099h       ; #00
        db      0c0h,080h,080h,0bfh,0,00eh,007h,08ah       ; #01
        db      0c0h,0c0h,0c0h,0cfh,0,00ah,007h,067h       ; #02
        db      0c0h,0d0h,0d0h,0dfh,0,00bh,007h,067h       ; #03
        db      0c1h,000h,000h,00fh,0,00fh,007h,067h       ; #04
        db      0c1h,010h,010h,01fh,0,00fh,007h,067h       ; #05
        db      0c1h,020h,020h,03fh,0,00fh,007h,077h       ; #06
        db      0c1h,040h,040h,04fh,0,00fh,007h,067h       ; #07
        db      0c1h,050h,050h,06fh,0,00fh,007h,077h       ; #08
        db      0c1h,070h,070h,07fh,0,00fh,007h,067h       ; #09
        db      0c1h,080h,080h,09fh,0,00fh,007h,077h       ; #0a
        db      0c1h,000h,000h,09fh,0,00fh,007h,0b7h       ; #0b
        db      0c1h,000h,000h,07fh,0,00fh,007h,0b7h       ; #0c
        db      0c1h,000h,000h,06fh,0,00fh,007h,0b7h       ; #0d
        db      0c1h,000h,000h,04fh,0,00fh,007h,0b7h       ; #0e
        db      0c1h,000h,000h,03fh,0,00fh,007h,0b7h       ; #0f

;##### DSP-01 Debug Mode ON Event #####
dsp_debug_mode_on_event:
        ldx     dsp_channel
        txa                     ; ******
        and     #00000001b      ; * AM-Reset
        ¬_equal_return       ; ******
        lda     dbg_fnol,x
        sta     dsp-01+00h,x
        lda     dbg_fnom,x
        sta     dsp-01+10h,x
        lda     dbg_fnoh,x
        sta     dsp-01+20h,x
        lda     dbg_st,x
        sta     dsp-01+30h,x
        lda     dbg_lt,x
        sta     dsp-01+40h,x
        lda     dbg_le,x
        sta     dsp-01+50h,x
        lda     dbg_bk,x
        sta     dsp-01+60h,x
        lda     basic_pan,x
        sta     dsp-01+70h,x
        lda     #08h
        sta     dsp-01+0a0h,x
        lda     #87h
        sta     dsp-01+0b0h,x
        lda     #08h
        sta     dsp-01+0c0h,x
        lda     #87h
        sta     dsp-01+0d0h,x
        lda     #0001-011b      ; KEY ON  *** AM ***
        sta     dsp-01+090h,x

        inx
        lda     dbg_fnol,x
        sta     dsp-01+00h,x
        lda     dbg_fnom,x
        sta     dsp-01+10h,x
        lda     dbg_fnoh,x
        sta     dsp-01+20h,x
        lda     dbg_st,x
        sta     dsp-01+30h,x
        lda     dbg_lt,x
        sta     dsp-01+40h,x
        lda     dbg_le,x
        sta     dsp-01+50h,x
        lda     dbg_bk,x
        sta     dsp-01+60h,x
        lda     basic_pan,x
        sta     dsp-01+70h,x
        lda     #0ch
        sta     dsp-01+0a0h,x
        lda     #27h
        sta     dsp-01+0b0h,x
        lda     #08h
        sta     dsp-01+0c0h,x
        lda     #87h
        sta     dsp-01+0d0h,x
        lda     #0001-011b      ; KEY ON  *** AM ***
        sta     dsp-01+090h,x
        rts

;##### DSP-01 Debug Mode OFF Event #####
dsp_debug_mode_off_event:
        ldx     dsp_channel
        txa                     ; ******
        and     #00000001b      ; * AM
        ¬_equal_return       ; ******
        lda     #0
        sta     dsp-01+0c0h,x
        lda     #0a7h
        sta     dsp-01+0d0h,x
        lda     #00011000b      ; KEY OFF *** AM ***
        sta     dsp-01+090h,x

        inx
        lda     #0
        sta     dsp-01+0c0h,x
        lda     #0a7h
        sta     dsp-01+0d0h,x
        lda     #00011000b      ; KEY OFF *** AM ***
        sta     dsp-01+090h,x
        rts

;##### Mouse Event Routine #####
;  Input : [mouse_sw]
;       Bit  =  7    6     5      4       3        2        1           0
;              ---  ---  SW(L)  SW(R)  Move(L)  Move(R)  Move(Down)  Move(Up)
msx_mouse_sw_event:
        lda     screen_mode
        asl     a
        tax
        jmp     (mouse_sw_jump),x
mouse_sw_jump:
        dw      mouse_event_init_screen         ; 00
        dw      mouse_event_main_menu           ; 01
        dw      mouse_event_midi_monitor        ; 02
        dw      mouse_event_dsp_debug           ; 03
        dw      mouse_event_pcm_module          ; 04
        dw      mouse_event_midi_control        ; 05
        dw      mouse_event_midi_mtr            ; 06

mouse_event_dsp_debug:
        lda     mouse_sw
        and     #up
        beq     mouse_event_dbg_not_up
        ldx     dbg_point
        lda     dbg_table_01,x
        sta     dbg_point
        jmp     mouse_event_dbg_screen_redraw
mouse_event_dbg_not_up:
        lda     mouse_sw
        and     #down
        beq     mouse_event_dbg_not_down
        ldx     dbg_point
        lda     dbg_table_02,x
        sta     dbg_point
        jmp     mouse_event_dbg_screen_redraw
mouse_event_dbg_not_down:
        lda     mouse_sw
        and     #right
        beq     mouse_event_dbg_not_right
        ldx     dbg_point
        lda     dbg_table_03,x
        sta     dbg_point
        jmp     mouse_event_dbg_screen_redraw
mouse_event_dbg_not_right:
        lda     mouse_sw
        and     #left
        beq     mouse_event_dbg_not_left
        ldx     dbg_point
        lda     dbg_table_04,x
        sta     dbg_point
        jmp     mouse_event_dbg_screen_redraw
mouse_event_dbg_not_left:
        lda     mouse_sw
        and     #00110000b
        beq     mouse_event_dbg_pass
        lda     dbg_point
        asl     a
        tax
        jmp     (dbg_mode_jump),x
mouse_event_dbg_pass:
        jmp     mouse_event_return
dbg_mode_jump:
        dw      dbg_mode_channel
        dw      dbg_mode_note
        dw      dbg_mode_bank
        dw      dbg_mode_panpot
        dw      dbg_mode_st
        dw      dbg_mode_lt
        dw      dbg_mode_le
        dw      dbg_mode_fnoh
        dw      dbg_mode_fnom
        dw      dbg_mode_fnol
        dw      mouse_event_return_back_to_main_menu
        dw      dbg_mode_next
dbg_mode_st:
        ldx     dbg_plane
        lda     dbg_st,x
        jsr     mouse_sw_inc_dec_check
        sta     dbg_st,x
        sta     work+13
        &hex_disp_with_point  172,work+13
        jmp     mouse_event_return
dbg_mode_lt:
        ldx     dbg_plane
        lda     dbg_lt,x
        jsr     mouse_sw_inc_dec_check
        sta     dbg_lt,x
        sta     work+13
        &hex_disp_with_point  184,work+13
        jmp     mouse_event_return
dbg_mode_le:
        ldx     dbg_plane
        lda     dbg_le,x
        jsr     mouse_sw_inc_dec_check
        sta     dbg_le,x
        sta     work+13
        &hex_disp_with_point  196,work+13
        jmp     mouse_event_return
dbg_mode_fnoh:
        ldx     dbg_plane
        lda     dbg_fnoh,x
        jsr     mouse_sw_inc_dec_check
        sta     dbg_fnoh,x
        sta     work+13
        &hex_disp_with_point  212,work+13
        jmp     mouse_event_return
dbg_mode_fnom:
        ldx     dbg_plane
        lda     dbg_fnom,x
        jsr     mouse_sw_inc_dec_check
        sta     dbg_fnom,x
        sta     work+13
        &hex_disp_with_point  224,work+13
        jmp     mouse_event_return
dbg_mode_fnol:
        ldx     dbg_plane
        lda     dbg_fnol,x
        jsr     mouse_sw_inc_dec_check
        sta     dbg_fnol,x
        sta     work+13
        &hex_disp_with_point  236,work+13
        jmp     mouse_event_return
dbg_mode_channel:
        lda     basic_ch
        jsr     mouse_sw_inc_dec_check
        and     #00001111b
        sta     basic_ch
        &hex_disp_with_point  94,basic_ch
        jmp     mouse_event_return
dbg_mode_note:
        ldx     dbg_plane
        lda     basic_note,x
        jsr     mouse_sw_inc_dec_check
        and     #01111111b
        sta     basic_note,x
        sta     work+13
        &hex_disp_with_point  111,work+13
        jmp     mouse_event_return
dbg_mode_bank:
        ldx     dbg_plane
        lda     dbg_bk,x
        jsr     mouse_sw_inc_dec_check
        sta     dbg_bk,x
        sta     work+13
        &hex_disp_with_point  131,work+13
        jmp     mouse_event_return
dbg_mode_panpot:
        ldx     dbg_plane
        lda     basic_pan,x
        jsr     mouse_sw_inc_dec_check
        sta     basic_pan,x
        sta     work+13
        &hex_disp_with_point  146,work+13
        jmp     mouse_event_return
dbg_mode_next:
        lda     dbg_plane
        inc     a
        and     #00001111b
        sta     dbg_plane
        jsr     lcd_draw_dsp_debug
        jmp     mouse_event_return
mouse_event_dbg_screen_redraw:
        &seloff
        jsr     lcd_draw_dbg_module_redraw
        jmp     mouse_event_return
dbg_table_01:
        db      11,11,0,1,2,3,3,4,5,6,10,8
dbg_table_02:
        db      2,3,4,5,7,8,9,11,11,11,10,1
dbg_table_03:
        db      1,1,3,3,5,6,6,8,9,9,11,11
dbg_table_04:
        db      0,0,2,2,4,4,5,7,7,8,10,10

;##### DSP1001 Debug Tester Screen #####
lcd_draw_dsp_debug:
        &movi   11,dbg_point
        jsr     lcd_draw_menu_back_screen
lcd_draw_dbg_module_redraw:
        &write  lcd_draw_dbg_module_data_menu_01
        &hex_disp_with_point  71,dbg_plane
        &write  lcd_draw_dbg_module_data_menu_02
        &write  lcd_draw_dbg_module_data_menu_03
        &hex_disp_with_point  94,basic_ch
        &write  lcd_draw_dbg_module_data_menu_05
        &write  lcd_draw_dbg_module_data_menu_06
        ldx     dbg_plane
        lda     basic_note,x
        sta     work+13
        &hex_disp_with_point  111,work+13
        &write  lcd_draw_dbg_module_data_menu_07
        &write  lcd_draw_dbg_module_data_menu_08
        ldx     dbg_plane
        lda     dbg_bk,x
        sta     work+13
        &hex_disp_with_point  131,work+13
        &write  lcd_draw_dbg_module_data_menu_09
        &write  lcd_draw_dbg_module_data_menu_10
        ldx     dbg_plane
        lda     basic_pan,x
        sta     work+13
        &hex_disp_with_point  146,work+13
        &write  lcd_draw_dbg_module_data_menu_11
        &write  lcd_draw_dbg_module_data_menu_12
        ldx     dbg_plane
        lda     dbg_st,x
        sta     work+13
        &hex_disp_with_point  172,work+13
        &write  lcd_draw_dbg_module_data_menu_13
        &write  lcd_draw_dbg_module_data_menu_14
        ldx     dbg_plane
        lda     dbg_lt,x
        sta     work+13
        &hex_disp_with_point  184,work+13
        &write  lcd_draw_dbg_module_data_menu_15
        &write  lcd_draw_dbg_module_data_menu_16
        ldx     dbg_plane
        lda     dbg_le,x
        sta     work+13
        &hex_disp_with_point  196,work+13
        &write  lcd_draw_dbg_module_data_menu_17
        &write  lcd_draw_dbg_module_data_menu_18
        ldx     dbg_plane
        lda     dbg_fnoh,x
        sta     work+13
        &hex_disp_with_point  212,work+13
        &write  lcd_draw_dbg_module_data_menu_19
        &write  lcd_draw_dbg_module_data_menu_20
        ldx     dbg_plane
        lda     dbg_fnom,x
        sta     work+13
        &hex_disp_with_point  224,work+13
        &write  lcd_draw_dbg_module_data_menu_21
        &write  lcd_draw_dbg_module_data_menu_22
        ldx     dbg_plane
        lda     dbg_fnol,x
        sta     work+13
        &hex_disp_with_point  236,work+13
        &write  lcd_draw_dbg_module_data_menu_23
        &write  lcd_draw_dbg_module_data_menu_24
        lda     dbg_point
        bne     lcd_draw_dbg_module_point_not_0
        &selon  lcd_draw_dbg_module_data_menu_03
        rts
lcd_draw_dbg_module_point_not_0:
        cmp     #1
        bne     lcd_draw_dbg_module_point_not_1
        &selon  lcd_draw_dbg_module_data_menu_06
        rts
lcd_draw_dbg_module_point_not_1:
        cmp     #2
        bne     lcd_draw_dbg_module_point_not_2
        &selon  lcd_draw_dbg_module_data_menu_08
        rts
lcd_draw_dbg_module_point_not_2:
        cmp     #3
        bne     lcd_draw_dbg_module_point_not_3
        &selon  lcd_draw_dbg_module_data_menu_10
        rts
lcd_draw_dbg_module_point_not_3:
        cmp     #4
        bne     lcd_draw_dbg_module_point_not_4
        &selon  lcd_draw_dbg_module_data_menu_12
        rts
lcd_draw_dbg_module_point_not_4:
        cmp     #5
        bne     lcd_draw_dbg_module_point_not_5
        &selon  lcd_draw_dbg_module_data_menu_14
        rts
lcd_draw_dbg_module_point_not_5:
        cmp     #6
        bne     lcd_draw_dbg_module_point_not_6
        &selon  lcd_draw_dbg_module_data_menu_16
        rts
lcd_draw_dbg_module_point_not_6:
        cmp     #7
        bne     lcd_draw_dbg_module_point_not_7
        &selon  lcd_draw_dbg_module_data_menu_18
        rts
lcd_draw_dbg_module_point_not_7:
        cmp     #8
        bne     lcd_draw_dbg_module_point_not_8
        &selon  lcd_draw_dbg_module_data_menu_20
        rts
lcd_draw_dbg_module_point_not_8:
        cmp     #9
        bne     lcd_draw_dbg_module_point_not_9
        &selon  lcd_draw_dbg_module_data_menu_22
        rts
lcd_draw_dbg_module_point_not_9:
        cmp     #10
        bne     lcd_draw_dbg_module_point_not_10
        &selon  lcd_draw_dbg_module_data_menu_23
        rts
lcd_draw_dbg_module_point_not_10:
        &selon  lcd_draw_dbg_module_data_menu_24
        rts
lcd_draw_dbg_module_data_menu_01:
        db      2,3,'### DSP-01 Debug Mode : Ch.=[**] ###',tail
lcd_draw_dbg_module_data_menu_02:
        db      3,4,'MIDI CH = ',tail
lcd_draw_dbg_module_data_menu_03:
        db      3,14,'[**]',tail
lcd_draw_dbg_module_data_menu_05:
        db      3,21,'Note NO.= ',tail
lcd_draw_dbg_module_data_menu_06:
        db      3,31,'[**]',tail
lcd_draw_dbg_module_data_menu_07:
        db      4,4,'Bank = ',tail
lcd_draw_dbg_module_data_menu_08:
        db      4,11,'[**]',tail
lcd_draw_dbg_module_data_menu_09:
        db      4,17,'Panpot = ',tail
lcd_draw_dbg_module_data_menu_10:
        db      4,26,'[**]',tail
lcd_draw_dbg_module_data_menu_11:
        db      5,4,'Start = ',tail
lcd_draw_dbg_module_data_menu_12:
        db      5,12,'[**]',tail
lcd_draw_dbg_module_data_menu_13:
        db      5,18,'Top = ',tail
lcd_draw_dbg_module_data_menu_14:
        db      5,24,'[**]',tail
lcd_draw_dbg_module_data_menu_15:
        db      5,30,'End = ',tail
lcd_draw_dbg_module_data_menu_16:
        db      5,36,'[**]',tail
lcd_draw_dbg_module_data_menu_17:
        db      6,4,'Fn(H) = ',tail
lcd_draw_dbg_module_data_menu_18:
        db      6,12,'[**]',tail
lcd_draw_dbg_module_data_menu_19:
        db      6,18,'(M) = ',tail
lcd_draw_dbg_module_data_menu_20:
        db      6,24,'[**]',tail
lcd_draw_dbg_module_data_menu_21:
        db      6,30,'(L) = ',tail
lcd_draw_dbg_module_data_menu_22:
        db      6,36,'[**]',tail
lcd_draw_dbg_module_data_menu_23:
        db      7,7,'',tail
lcd_draw_dbg_module_data_menu_24:
        db      7,25,'',tail
        rts

;##### PCM Mode #####
mouse_event_pcm_module:
        lda     mouse_sw
        and     #up
        beq     mouse_event_pcm_not_up
        ldx     pcm_point
        lda     pcm_table_01,x
        sta     pcm_point
        jmp     mouse_event_pcm_screen_redraw
mouse_event_pcm_not_up:
        lda     mouse_sw
        and     #down
        beq     mouse_event_pcm_not_down
        ldx     pcm_point
        lda     pcm_table_02,x
        sta     pcm_point
        jmp     mouse_event_pcm_screen_redraw
mouse_event_pcm_not_down:
        lda     mouse_sw
        and     #right
        beq     mouse_event_pcm_not_right
        lda     pcm_point
        cmp     #4
        bne     mouse_event_pcm_pass
        inc     pcm_point
        jmp     mouse_event_pcm_screen_redraw
mouse_event_pcm_pass:
        jmp     mouse_event_return
mouse_event_pcm_not_right:
        lda     mouse_sw
        and     #left
        beq     mouse_event_pcm_not_left
        lda     pcm_point
        cmp     #5
        bne     mouse_event_pcm_pass
        dec     pcm_point
        jmp     mouse_event_pcm_screen_redraw
mouse_event_pcm_not_left:
        lda     mouse_sw
        and     #00110000b
        beq     mouse_event_pcm_pass
        lda     pcm_point
        asl     a
        tax
        jmp     (pcm_mode_jump),x
pcm_mode_jump:
        dw      pcm_mode_channel
        dw      pcm_mode_note
        dw      pcm_mode_tone
        dw      pcm_mode_panpot
        dw      mouse_event_return_back_to_main_menu
        dw      pcm_mode_next
mouse_sw_inc_dec_check:
        sta     work+12
        lda     mouse_sw
        and     #sw_r
        bne     mouse_sw_inc
        lda     work+12
        dec     a
        rts
mouse_sw_inc:
        lda     work+12
        inc     a
        rts
pcm_mode_channel:
        lda     basic_ch
        jsr     mouse_sw_inc_dec_check
        and     #00001111b
        sta     basic_ch
        &hex_disp_with_point  108,basic_ch
        jmp     mouse_event_return
pcm_mode_note:
        ldx     pcm_plane
        lda     basic_note,x
        jsr     mouse_sw_inc_dec_check
        and     #01111111b
        sta     basic_note,x
        sta     work+13
        &hex_disp_with_point  146,work+13
        jmp     mouse_event_return
pcm_mode_tone:
        ldx     pcm_plane
        lda     basic_tone,x
        jsr     mouse_sw_inc_dec_check
        and     #00111111b
        sta     basic_tone,x
        sta     work+13
        &hex_disp_with_point  186,work+13
        jmp     mouse_event_return
pcm_mode_panpot:
        ldx     pcm_plane
        lda     basic_pan,x
        jsr     mouse_sw_inc_dec_check
        sta     basic_pan,x
        sta     work+13
        &hex_disp_with_point  226,work+13
        jmp     mouse_event_return
pcm_mode_next:
        lda     pcm_plane
        inc     a
        and     #00001111b
        sta     pcm_plane
        jsr     lcd_draw_pcm_module
        jmp     mouse_event_return
mouse_event_pcm_screen_redraw:
        &seloff
        jsr     lcd_draw_pcm_module_redraw
        jmp     mouse_event_return
pcm_table_02:
        db      1,2,3,5,4,0
pcm_table_01:
        db      5,0,1,2,4,3

;##### PCM Module Mode Screen #####
lcd_draw_pcm_module:
        &movi   5,pcm_point
        jsr     lcd_draw_menu_back_screen
lcd_draw_pcm_module_redraw:
        &write  lcd_draw_pcm_module_data_menu_01
        &hex_disp_with_point  71,pcm_plane
        &write  lcd_draw_pcm_module_data_menu_02
        &write  lcd_draw_pcm_module_data_menu_03
        &hex_disp_with_point  108,basic_ch
        &write  lcd_draw_pcm_module_data_menu_05
        &write  lcd_draw_pcm_module_data_menu_06
        ldx     pcm_plane
        lda     basic_note,x
        sta     work+13
        &hex_disp_with_point  146,work+13
        &write  lcd_draw_pcm_module_data_menu_07
        &write  lcd_draw_pcm_module_data_menu_08
        ldx     pcm_plane
        lda     basic_tone,x
        sta     work+13
        &hex_disp_with_point  186,work+13
        &write  lcd_draw_pcm_module_data_menu_09
        &write  lcd_draw_pcm_module_data_menu_10
        ldx     pcm_plane
        lda     basic_pan,x
        sta     work+13
        &hex_disp_with_point  226,work+13
        &write  lcd_draw_pcm_module_data_menu_11
        &write  lcd_draw_pcm_module_data_menu_12
        lda     pcm_point
        bne     lcd_draw_pcm_module_point_not_0
        &selon  lcd_draw_pcm_module_data_menu_03
        rts
lcd_draw_pcm_module_point_not_0:
        cmp     #1
        bne     lcd_draw_pcm_module_point_not_1
        &selon  lcd_draw_pcm_module_data_menu_06
        rts
lcd_draw_pcm_module_point_not_1:
        cmp     #2
        bne     lcd_draw_pcm_module_point_not_2
        &selon  lcd_draw_pcm_module_data_menu_08
        rts
lcd_draw_pcm_module_point_not_2:
        cmp     #3
        bne     lcd_draw_pcm_module_point_not_3
        &selon  lcd_draw_pcm_module_data_menu_10
        rts
lcd_draw_pcm_module_point_not_3:
        cmp     #4
        bne     lcd_draw_pcm_module_point_not_4
        &selon  lcd_draw_pcm_module_data_menu_11
        rts
lcd_draw_pcm_module_point_not_4:
        &selon  lcd_draw_pcm_module_data_menu_12
        rts
lcd_draw_pcm_module_data_menu_01:
        db      2,3,'### DSP-01 PCM Module : Ch.=[**] ###',tail
lcd_draw_pcm_module_data_menu_02:
        db      3,5,'MIDI Receive Channel = ',tail
lcd_draw_pcm_module_data_menu_03:
        db      3,28,'[**]',tail
lcd_draw_pcm_module_data_menu_05:
        db      4,5,'Accept Note Number = ',tail
lcd_draw_pcm_module_data_menu_06:
        db      4,26,'[**]',tail
lcd_draw_pcm_module_data_menu_07:
        db      5,5,'Preset TONE Number = ',tail
lcd_draw_pcm_module_data_menu_08:
        db      5,26,'[**]',tail
lcd_draw_pcm_module_data_menu_09:
        db      6,5,'Stereo PANPOT Data = ',tail
lcd_draw_pcm_module_data_menu_10:
        db      6,26,'[**]',tail
lcd_draw_pcm_module_data_menu_11:
        db      7,7,'',tail
lcd_draw_pcm_module_data_menu_12:
        db      7,25,'',tail

;##### Mouse Remains #####
mouse_event_init_screen:
        lda     mouse_sw
        and     #00110000b
        bne     mouse_event_init_screen_to_main
        jmp     mouse_event_return
mouse_event_init_screen_to_main:
        &movi   2,main_point
        jmp     mouse_event_return_back_to_main_menu

mouse_event_midi_control:
mouse_event_midi_mtr:
        lda     mouse_sw
        and     #sw_l
        bne     mouse_event_midi_control_return
        jmp     mouse_event_return
mouse_event_midi_control_return:
        jmp     mouse_event_return_back_to_main_menu

mouse_event_midi_monitor:
        lda     mouse_sw
        and     #sw_l
        bne     mouse_event_return_back_to_main_menu
        jmp     mouse_event_return
mouse_event_return_back_to_main_menu:
        &movi   1,screen_mode
        jsr     lcd_draw_main_menu
        jmp     mouse_event_return

mouse_event_main_menu:
        lda     mouse_sw
        and     #sw_r
        bne     mouse_event_00_sw_seq
        jmp     mouse_event_00_up_move
mouse_event_00_sw_seq:
        lda     main_point
        asl     a
        tax
        jmp     (mouse_event_00_sw_jump),x
mouse_event_00_sw_jump:
        dw      mouse_event_return              ; (Dummy)
        dw      mouse_event_return              ; (Dummy)
        dw      mouse_event_to_midi_monitor     ; 2
        dw      mouse_event_to_dsp_debug        ; 3
        dw      mouse_event_to_pcm_module       ; 4
        dw      mouse_event_to_midi_control     ; 5
        dw      mouse_event_to_midi_mtr         ; 6

mouse_event_to_midi_monitor:
        &movi   2,screen_mode
        jsr     lcd_draw_midi_monitor
        jmp     mouse_event_return
mouse_event_to_dsp_debug:
        &movi   3,screen_mode
        jsr     lcd_draw_dsp_debug
        ldx     #0
        lda     #0
mouse_event_dsp_initial:
        sta     dsp-01+090h,x           ; Control Word
        inx
        cpx     #10h
        bne     mouse_event_dsp_initial
        jmp     mouse_event_return
mouse_event_to_pcm_module:
        &movi   4,screen_mode
        jsr     lcd_draw_pcm_module
        jmp     mouse_event_return
mouse_event_to_midi_control:
        &movi   5,screen_mode
        jsr     lcd_draw_midi_control
        jmp     mouse_event_return
mouse_event_to_midi_mtr:
        &movi   6,screen_mode
        jsr     lcd_draw_midi_mtr
        jmp     mouse_event_return

mouse_event_00_up_move:
        lda     mouse_sw
        and     #up
        bne     mouse_event_00_up_move_seq
        jmp     mouse_event_00_down_move
mouse_event_00_up_move_seq:
        &seloff
        lda     main_point
        cmp     #2
        bne     mouse_event_00_up_move_001
        &movi   6,main_point
        &selon  lcd_draw_main_message_6
        jmp     mouse_event_return
mouse_event_00_up_move_001:
        cmp     #3
        bne     mouse_event_00_up_move_002
        &movi   2,main_point
        &selon  lcd_draw_main_message_2
        jmp     mouse_event_return
mouse_event_00_up_move_002:
        cmp     #4
        bne     mouse_event_00_up_move_003
        &movi   3,main_point
        &selon  lcd_draw_main_message_3
        jmp     mouse_event_return
mouse_event_00_up_move_003:
        cmp     #5
        bne     mouse_event_00_up_move_004
        &movi   4,main_point
        &selon  lcd_draw_main_message_4
        jmp     mouse_event_return
mouse_event_00_up_move_004:
        &movi   5,main_point
        &selon  lcd_draw_main_message_5
        jmp     mouse_event_return
mouse_event_00_down_move:
        lda     mouse_sw
        and     #down
        &equal_return
        &seloff
        lda     main_point
        cmp     #2
        bne     mouse_event_00_down_move_001
        &movi   3,main_point
        &selon  lcd_draw_main_message_3
        jmp     mouse_event_return
mouse_event_00_down_move_001:
        cmp     #3
        bne     mouse_event_00_down_move_002
        &movi   4,main_point
        &selon  lcd_draw_main_message_4
        jmp     mouse_event_return
mouse_event_00_down_move_002:
        cmp     #4
        bne     mouse_event_00_down_move_003
        &movi   5,main_point
        &selon  lcd_draw_main_message_5
        jmp     mouse_event_return
mouse_event_00_down_move_003:
        cmp     #5
        bne     mouse_event_00_down_move_004
        &movi   6,main_point
        &selon  lcd_draw_main_message_6
        jmp     mouse_event_return
mouse_event_00_down_move_004:
        &movi   2,main_point
        &selon  lcd_draw_main_message_2
        jmp     mouse_event_return

mouse_event_return:
        stz     mouse_sw
        rts

;##### MIDI 8-Track MTR Mode Screen #####
lcd_draw_midi_mtr:
        jsr     lcd_draw_menu_back_screen
        &write  lcd_draw_mada_mada
        rts
lcd_draw_mada_mada:
        db      4,5,'・・・ スンマヘンナア。 マダ デキテ オランノドスエ。',tail

;##### MIDI System Controller Screen #####
lcd_draw_midi_control:
        jsr     lcd_draw_menu_back_screen
        &write  lcd_draw_mada_mada
        rts

;##### MIDI Monitor Screen #####
lcd_draw_midi_monitor:
        jsr     lcd_draw_menu_back_screen
        &write  lcd_draw_midi_monitor_data
        &movi   5,erase_point
        stz     lcd_era_h
        stz     moni_point
        &movi   81,lcd_add_l
        stz     lcd_add_h
        &movi   46h,lcd+1
        &move   lcd_add_l,lcd
        &move   lcd_add_h,lcd
        &movi   59h,lcd+1       ; Cousor ON
        &movi   17h,lcd
        rts
lcd_draw_midi_monitor_data:
        db      2,3,'=== MIDI Signal Stream Monitor ===',tail

;##### MIDI Stream Monitor #####
lcd_midi_monitor_display:
        lda     rxdata
        cmp     #0feh
        &equal_return
        sta     lcd_hex_data
        jsr     lcd_display_hex_data
        inc     moni_point
        lda     moni_point
        cmp     #57
        bne     lcd_midi_monitor_001
        &movi   1,lcd_add_h
        bra     lcd_midi_monitor_002
lcd_midi_monitor_001:
        cmp     #65
        bne     lcd_midi_monitor_002
        stz     moni_point
        stz     lcd_add_h
lcd_midi_monitor_002:
        ldx     erase_point
        lda     lcd_midi_display_address_table,x
        sta     lcd_add_l
        jsr     lcd_display_erase
        inc     erase_point
        lda     erase_point
        cmp     #57
        bne     lcd_midi_monitor_003
        &movi   1,lcd_era_h
        rts
lcd_midi_monitor_003:
        cmp     #65
        ¬_equal_return
        stz     erase_point
        stz     lcd_era_h
        rts
lcd_midi_display_address_table:
        db      81,84,87,90,93,96,99,102,105,108,111,114,117
        db      121,124,127,130,133,136,139,142,145,148,151,154,157
        db      161,164,167,170,173,176,179,182,185,188,191,194,197
        db      201,204,207,210,213,216,219,222,225,228,231,234,237
        db      241,244,247,250,253,0,3,6,9,12,15,18,21

;##### LCD Erase Display #####
;   Input : lcd_era_h,lcd_add_l
lcd_display_erase:
        &movi   46h,lcd+1
        &move   lcd_add_l,lcd
        &move   lcd_era_h,lcd
        &movi   42h,lcd+1
        lda     #20h
        sta     lcd
        sta     lcd
        ldx     moni_point
        lda     lcd_midi_display_address_table,x
        sta     lcd_add_l
        &movi   46h,lcd+1
        &move   lcd_add_l,lcd
        &move   lcd_add_h,lcd
        &movi   42h,lcd+1
        &movi   '*',lcd
        &movi   46h,lcd+1
        &move   lcd_add_l,lcd
        &move   lcd_add_h,lcd
        rts

;##### LCD HEX Data Dislay #####
;   Input : (lcd_add_h,lcd_add_l),lcd_hex_data
lcd_display_hex_data_with_address:
        &movi   4ch,lcd+1       ; Right
        &movi   46h,lcd+1
        &move   lcd_add_l,lcd
        &move   lcd_add_h,lcd
lcd_display_hex_data:
        &movi   42h,lcd+1
        lda     lcd_hex_data
        lsr     a
        lsr     a
        lsr     a
        lsr     a
        tax
        lda     hex_table,x
        sta     lcd
        lda     lcd_hex_data
        and     #0fh
        tax
        lda     hex_table,x
        sta     lcd
        rts

;##### LCD Main Menu Draw #####
lcd_draw_main_menu:
        &movi   59h,lcd+1       ; Cousor OFF
        &movi   14h,lcd
        jsr     lcd_draw_menu_back_screen
        &write  lcd_draw_main_message_1
        &write  lcd_draw_main_message_2
        &write  lcd_draw_main_message_3
        &write  lcd_draw_main_message_4
        &write  lcd_draw_main_message_5
        &write  lcd_draw_main_message_6
        lda     main_point
        cmp     #2
        bne     lcd_draw_main_next_03
        &selon  lcd_draw_main_message_2
        rts
lcd_draw_main_next_03:
        cmp     #3
        bne     lcd_draw_main_next_04
        &selon  lcd_draw_main_message_3
        rts
lcd_draw_main_next_04:
        cmp     #4
        bne     lcd_draw_main_next_05
        &selon  lcd_draw_main_message_4
        rts
lcd_draw_main_next_05:
        cmp     #5
        bne     lcd_draw_main_next_06
        &selon  lcd_draw_main_message_5
        rts
lcd_draw_main_next_06:
        &selon  lcd_draw_main_message_6
        rts
lcd_draw_main_message_1:
        db      2,3,'**** MAIN MENU ****   by Y.Nagashima',tail
lcd_draw_main_message_2:
        db      3,7,'[1] MIDI Signal Monitor',tail
lcd_draw_main_message_3:
        db      4,7,'[2] ==DSP-01== Debug Tester',tail
lcd_draw_main_message_4:
        db      5,7,'[3] PCM Percussion Tone Unit',tail
lcd_draw_main_message_5:
        db      6,7,'[4] Japanese Word Processor',tail
lcd_draw_main_message_6:
        db      7,7,'[5] MODEM Communication Control',tail

;##### Menu Back Screen #####
lcd_draw_menu_back_screen:
        &movi   4ch,lcd+1       ; Right
        &movi   46h,lcd+1
        stz     lcd
        stz     lcd
        &movi   42h,lcd+1
        lda     #20h
        ldy     #0
lcd_char_clear_loop_1:
        ldx     #0
lcd_char_clear_loop_2:
        sta     lcd
        inx
        cpx     #40
        bne     lcd_char_clear_loop_2
        iny
        cpy     #8
        bne     lcd_char_clear_loop_1
lcd_draw_back_plane_black:
        &movi   4ch,lcd+1       ; Right
        &movi   46h,lcd+1
        stz     lcd
        &movi   10h,lcd
        &movi   42h,lcd+1
        jsr     lcd_draw_main_draw_sub_1
        jsr     lcd_draw_main_draw_sub_2
        jsr     lcd_draw_main_draw_sub_3
        jsr     lcd_draw_main_draw_sub_2
        jsr     lcd_draw_main_draw_sub_1
        rts
lcd_draw_main_draw_sub_1:
        ldx     #0
        lda     #0ffh
lcd_draw_main_loop_1:
        sta     lcd
        inx
        cpx     #40
        bne     lcd_draw_main_loop_1
        rts
lcd_draw_main_draw_sub_2:
        &movi   10000000b,lcd
        ldx     #0
        lda     #0
lcd_draw_main_loop_2:
        sta     lcd
        inx
        cpx     #38
        bne     lcd_draw_main_loop_2
        &movi   00000111b,lcd
        rts
lcd_draw_main_draw_sub_3:
        ldy     #0
lcd_draw_main_loop_3:
        &movi   -0111111b,lcd
        ldx     #0
        lda     #0ffh
lcd_draw_main_loop_4:
        sta     lcd
        inx
        cpx     #38
        bne     lcd_draw_main_loop_4
        &movi   111-0111b,lcd
        iny
        cpy     #60
        bne     lcd_draw_main_loop_3
        rts

;##### LCD Message Mask Cursor ON/OFF Routine #####
lcd_select_mask:
        ldy     #0
        lda     (work+2),y
        sta     work
        iny
        lda     (work+2),y
        sta     work+1
        ldx     #0
lcd_select_mask_loop_1:
        inx
        iny
        lda     (work+2),y
        cmp     #tail
        bne     lcd_select_mask_loop_1
        stx     work+5
        dec     work+5
        &movi   4fh,lcd+1       ; Down
        dec     work
        asl     work
        asl     work
        asl     work
        dec     work
        &move   work,adsp
        &movi   40,adsp+4
        &move   adsp+8,add_s_l_1
        &move   adsp+12,add_s_h_1
        dec     work+1
        dec     work+1
        &move   work+1,add_s_l_2
        stz     add_s_h_2
        jsr     adder_16
        &move   add_d_l,work+8
        lda     add_d_h
        ora     #00010000b
        sta     work+9
        &movi   46h,lcd+1
        &move   work+8,lcd
        &move   work+9,lcd
        &movi   42h,lcd+1
        &movi   0ffh,work+6
        lda     work+4
        bne     lcd_select_mask_loop_2
        &movi   11111000b,work+6
lcd_select_mask_loop_2:
        ldx     #0
lcd_select_mask_loop_3:
        &move   work+6,lcd
        inx
        cpx     #9
        bne     lcd_select_mask_loop_3
        ldy     #0
lcd_select_mask_loop_4:
        inc     work+8
        &movi   46h,lcd+1
        &move   work+8,lcd
        &move   work+9,lcd
        &movi   42h,lcd+1
        ldx     #0
lcd_select_mask_loop_5:
        &move   work+4,lcd
        inx
        cpx     #9
        bne     lcd_select_mask_loop_5
        iny
        cpy     work+5
        bne     lcd_select_mask_loop_4
        inc     work+8
        &movi   46h,lcd+1
        &move   work+8,lcd
        &move   work+9,lcd
        &movi   42h,lcd+1
        &movi   0ffh,work+6
        lda     work+4
        bne     lcd_select_mask_loop_6
        &movi   01111111b,work+6
lcd_select_mask_loop_6:
        ldx     #0
lcd_select_mask_loop_7:
        &move   work+6,lcd
        inx
        cpx     #9
        bne     lcd_select_mask_loop_7
        rts

;##### LCD Message Write Subroutine : Called from [&write] #####
lcd_write_message:
        ldy     #0
        lda     (work+2),y
        sta     work
        iny
        lda     (work+2),y
        sta     work+1
        jsr     lcd_write_address_set
        &movi   42h,lcd+1
lcd_write_message_loop:
        iny
        lda     (work+2),y
        cmp     #tail
        &equal_return
        sta     lcd
        bra     lcd_write_message_loop
lcd_write_address_set:
        &movi   4ch,lcd+1       ; Right
        &movi   46h,lcd+1
        dec     work
        dec     work+1
        &move   work,adsp
        &movi   40,adsp+4
        &move   adsp+8,add_s_l_1
        &move   adsp+12,add_s_h_1
        &move   work+1,add_s_l_2
        stz     add_s_h_2
        jsr     adder_16
        &move   add_d_l,lcd
        &move   add_d_h,lcd
        rts

;##### LCD Window Initial Screen Draw #####
lcd_draw_initial:
        stz     work+7
        &movi   4fh,lcd+1       ; Down
lcd_draw_initial_loop_1:
        &movi   46h,lcd+1
        &move   work+7,lcd
        &movi   10h,lcd
        &movi   42h,lcd+1
        ldx     #0
lcd_draw_initial_loop_2:
        &movi   55h,lcd
        &movi   0aah,lcd
        inx
        cpx     #32
        bne     lcd_draw_initial_loop_2
        inc     work+7
        lda     work+7
        cmp     #40
        bne     lcd_draw_initial_loop_1
        &movi   0cah,work+7
lcd_draw_initial_loop_3:
        &movi   46h,lcd+1
        &move   work+7,lcd
        &movi   13h,lcd
        &movi   42h,lcd+1
        ldx     #0
lcd_draw_initial_loop_4:
        stz     lcd
        inx
        cpx     #16
        bne     lcd_draw_initial_loop_4
        inc     work+7
        lda     work+7
        cmp     #0dch
        bne     lcd_draw_initial_loop_3
        &movi   095h,work+7
        &inidraw lcd_draw_initial_data_1
        &inidraw lcd_draw_initial_data_2
        &inidraw lcd_draw_initial_data_3
        &inidraw lcd_draw_initial_data_4
        &inidraw lcd_draw_initial_data_5
        &inidraw lcd_draw_initial_data_6
        &inidraw lcd_draw_initial_data_5
        &inidraw lcd_draw_initial_data_6
        &inidraw lcd_draw_initial_data_7
        &inidraw lcd_draw_initial_data_8
        &inidraw lcd_draw_initial_data_9
        &inidraw lcd_draw_initial_data_10
        stz     screen_mode
        rts
lcd_draw_initial_sub:
        &movi   46h,lcd+1
        &move   work+7,lcd
        &movi   14h,lcd
        &movi   42h,lcd+1
        ldy     #0
lcd_draw_initial_loop_5:
        lda     (work),y
        sta     lcd
        iny
        cpy     #7
        bne     lcd_draw_initial_loop_5
        rts
lcd_draw_initial_data_1:
        db      11100000b
        db      11100000b
        db      11100000b
        db      11111100b
        db      11100000b
        db      11100000b
        db      11100000b
lcd_draw_initial_data_2:
        db      01110000b
        db      01110000b
        db      01110000b
        db      11110000b
        db      01110000b
        db      01110000b
        db      01110000b
lcd_draw_initial_data_3:
        db      11111100b
        db      11100000b
        db      11100000b
        db      11111100b
        db      11100000b
        db      11100000b
        db      11111100b
lcd_draw_initial_data_4:
        db      11110000b
        db      00000000b
        db      00000000b
        db      11000000b
        db      00000000b
        db      00000000b
        db      11110000b
lcd_draw_initial_data_5:
        db      11100000b
        db      11100000b
        db      11100000b
        db      11100000b
        db      11100000b
        db      11100000b
        db      11111100b
lcd_draw_initial_data_6:
        db      00000000b
        db      00000000b
        db      00000000b
        db      00000000b
        db      00000000b
        db      01110000b
        db      11110000b
lcd_draw_initial_data_7:
        db      01111100b
        db      11100000b
        db      11100000b
        db      11100000b
        db      11100000b
        db      11100000b
        db      01111100b
lcd_draw_initial_data_8:
        db      11100000b
        db      01110000b
        db      01110000b
        db      01110000b
        db      01110000b
        db      01110000b
        db      11100000b
lcd_draw_initial_data_9:
        db      01110000b
        db      01110000b
        db      01110000b
        db      01110000b
        db      01110000b
        db      00000000b
        db      01110000b
lcd_draw_initial_data_10:
        db      11100000b
        db      11100000b
        db      11100000b
        db      11100000b
        db      11100000b
        db      00000000b
        db      11100000b

;##### Mouse Address Check #####
mouse_point_check:
        inc     mouse_timer
        lda     mouse_timer
        cmp     #3fh
        ¬_equal_return
        stz     mouse_timer
        lda     right_count
        cmp     comp_level
        bmi     mouse_check_next_left
        stz     right_count
        stz     left_count
        lda     #00000100b
        sta     mouse_sw
        bra     mouse_check_next_up
mouse_check_next_left:
        lda     left_count
        cmp     comp_level
        bmi     mouse_check_next_up
        stz     right_count
        stz     left_count
        lda     #00001000b
        sta     mouse_sw
mouse_check_next_up:
        lda     up_count
        cmp     comp_level
        bmi     mouse_check_next_down
        stz     up_count
        stz     down_count
        lda     mouse_sw
        ora     #00000001b
        sta     mouse_sw
mouse_check_next_down:
        lda     down_count
        cmp     comp_level
        bmi     mouse_check_next_result
        stz     up_count
        stz     down_count
        lda     mouse_sw
        ora     #00000010b
        sta     mouse_sw
mouse_check_next_result:
        lda     mouse_sw
        &equal_return
        jsr     msx_mouse_sw_event
        rts

;##### MSX Mouse Event Scan Routine #####
msx_mouse_check:
        inc     msx_timer
        lda     msx_timer
        beq     msx_mouse_check_fourth
        cmp     #7
        beq     msx_mouse_check_third
        cmp     #6
        beq     msx_mouse_check_second
        cmp     #5
        beq     msx_mouse_check_first
        rts
msx_mouse_check_fourth:
        &movi   000-0101b,uart3+1       ; MSX Strobe to High
        rts
msx_mouse_check_third:
        lda     pia+1
        and     #00001111b
        ora     msx_y_data
        sta     msx_y_data
        lda     msx_x_data
        cmp     #0ffh
        beq     msx_point_change_next
        lda     msx_x_data
        bmi     msx_point_change_minus_x
        clc
        adc     left_count
        sta     left_count
        bra     msx_point_change_next
msx_point_change_minus_x:
        eor     #0ffh
        and     #01111111b
        clc
        adc     right_count
        sta     right_count
msx_point_change_next:
        lda     msx_y_data
        cmp     #0ffh
        &equal_return
        lda     msx_y_data
        bmi     msx_point_change_minus_y
        clc
        adc     up_count
        sta     up_count
        rts
msx_point_change_minus_y:
        eor     #0ffh
        and     #01111111b
        clc
        adc     down_count
        sta     down_count
        rts
msx_mouse_check_second:
        lda     pia+1
        tax
        &movi   000-0111b,uart3+1       ; MSX Strobe to Low
        txa
        asl     a
        asl     a
        asl     a
        asl     a
        sta     msx_y_data
        rts
msx_mouse_check_first:
        lda     pia+1
        tax
        &movi   000-0111b,uart3+1       ; MSX Strobe to Low
        txa
        asl     a
        asl     a
        asl     a
        asl     a
        sta     msx_x_data
        &move   msx_sw_buf,work+11
        lda     pia+1
        lsr     a
        eor     #0ffh
        and     #00010000b
        sta     work+12
        lda     pia+1
        asl     a
        eor     #0ffh
        and     #00100000b
        ora     work+12
        sta     work+12
        and     #00110000b
        sta     msx_sw_buf      ; ** This Position !!
        beq     msx_mouse_scan_next
        eor     work+11
        beq     msx_mouse_scan_next
        sta     work+13
        lda     work+12
        and     work+13
        beq     msx_mouse_scan_next
        sta     mouse_sw
msx_mouse_scan_next:
        jsr     led_display     ; DEMO / Wait
        lda     pia+1
        and     #00001111b
        ora     msx_x_data
        sta     msx_x_data
        &movi   000-0101b,uart3+1       ; MSX Strobe to High
        lda     mouse_sw
        &equal_return
        jsr     msx_mouse_sw_event
        rts

;##### MIDI Rx Sequence #####
rx_midi_sequence:
        ldx     midi_r_end
        cpx     midi_r_top
        &equal_return
        inc     midi_r_end
        lda     midi_r_fifo,x
        sta     rxdata
        lda     screen_mode
        cmp     #2
        bne     rx_midi_not_monitor_mode
        jmp     lcd_midi_monitor_display
rx_midi_not_monitor_mode:
        lda     rxdata
        bpl     rxseq_running
        and     #0f0h
        cmp     #090h
        beq     rxseq_status_event
        cmp     #080h
        beq     rxseq_status_event
        lda     rxdata
        and     #11111000b
        cmp     #11111000b
        &equal_return
        stz     midi_rsb
        rts
rxseq_status_event:
        &move   rxdata,midi_rsb
        stz     midi_dcb
        rts
rxseq_running:
        lda     midi_rsb
        &equal_return
        and     #0f0h
        cmp     #90h
        bne     rxseq_not_note_on
        lda     midi_dcb
        bne     rxseq_on_velocity
        inc     midi_dcb
        &move   rxdata,rxkeyno
        rts
rxseq_on_velocity:
        stz     midi_dcb
        lda     midi_rsb
        and     #0fh
        sta     rxchannel
        lda     rxdata
        beq     rxseq_on_velo_zero
        sta     rxspeed
        jsr     dsp_on_event
        rts
rxseq_on_velo_zero:
        jsr     dsp_off_event
        rts
rxseq_not_note_on:
        cmp     #80h
        bne     rxseq_not_note_off
        lda     midi_dcb
        bne     rxseq_off_velocity
        inc     midi_dcb
        &move   rxdata,rxkeyno
        rts
rxseq_off_velocity:
        stz     midi_dcb
        lda     midi_rsb
        and     #0fh
        sta     rxchannel
        jsr     dsp_off_event
        rts
rxseq_not_note_off:
        stz     midi_rsb
        rts

;##### MIDI Data Transfer Routine #####
tx_midi_sequence:
        ldx     midi_t_end
        cpx     midi_t_top
        &equal_return
        lda     uart1+1
        and     #00000001b
        &equal_return
        inc     midi_t_end
        lda     midi_t_fifo,x
        sta     uart1
        rts

;##### Interrupt -- TIMER #####
int_timer:
        jmp     int_seq_ret

;##### Interrupt -- MPU401 #####
int_mpu:
        jmp     int_seq_ret

;##### Interrupt -- SIO #####
int_sio:
        lda     uart3
        jmp     int_seq_ret

;##### Interrupt -- RS232C #####
int_rs232c:
        lda     uart2
        jmp     int_seq_ret

;##### IRQ Sequence #####
int:
        pha
        phx
        phy
        lda     pia
        eor     #0ffh
        and     #0fh
        asl     a
        tax
        jmp     (int_table),x
int_seq_ret:
        ply
        plx
        pla
        rti
int_table:
        dw      int_seq_ret
        dw      int_mpu
        dw      int_sio,int_sio
        dw      int_timer,int_timer
        dw      int_timer,int_timer
        dw      int_rs232c,int_rs232c,int_rs232c,int_rs232c
        dw      int_rs232c,int_rs232c,int_rs232c,int_rs232c

;##### NMI Sequence #####
nmi:
        pha
        phx
        lda     uart1
        cmp     #0feh
        beq     nmiret
        cmp     #0f8h
        beq     nmiret
        ldx     midi_r_top
        sta     midi_r_fifo,x
        inc     midi_r_top
nmiret:
        plx
        pla
        rti

;##### LED Display Demonstration Routine #####
led_display:
        inc     led_demo
        lda     led_demo
        cmp     #3
        bne     led_display_seq_1
        stz     led_demo
        inc     led_demo+1
led_display_seq_1:
        lda     led_demo+1
        and     #01111111b
        tax
        lda     led_display_data,x
        eor     #0ffh
        sta     pia+2
        rts
led_display_data:
        db      00000001b,00000010b,00000100b,00001000b,00010000b,00100000b,01000000b,10000000b
        db      11000000b,11100000b,11110000b,11111000b,11111100b,11111110b,01111111b,00111111b
        db      00011111b,00001111b,00000111b,00000011b,00000001b,00000000b,00010000b,00000000b
        db      00001000b,00000000b,00011000b,00100100b,01000010b,10000001b,01000010b,00100100b
        db      00011000b,00000000b,00011000b,00111100b,01111110b,11111111b,01111110b,00111100b
        db      00011000b,00000000b,00000001b,00000011b,00000101b,00001010b,00010100b,00101000b
        db      01010000b,10100000b,01000000b,10000000b,00000010b,00001000b,00100000b,10000000b
        db      00000001b,00000010b,00000101b,00001010b,00010101b,00101010b,01010101b,10101010b
        db      01010100b,10101000b,01010000b,10100000b,01000000b,10000000b,11000000b,01100000b
        db      10110000b,11011000b,01101100b,10110110b,11011011b,01101101b,00110110b,00011011b
        db      00001101b,00000110b,00000011b,00000001b,00000000b,10000001b,01000010b,00100100b
        db      00011000b,00100100b,01000010b,10000001b,11000011b,11100111b,11111111b,11100111b
        db      11000011b,10000001b,00000000b,10000000b,00000001b,01000000b,00000010b,00100000b
        db      00000100b,00010000b,00001000b,00010000b,00000100b,00100000b,00000010b,01000000b
        db      00000001b,10000000b,00000000b,10000000b,11000000b,01100000b,00110000b,00011000b
        db      00001100b,00000110b,00000011b,00000001b,10000000b,00100000b,00001000b,00000010b

;##### 16 Bit Adder #####
adder_16:
        clc
        lda     add_s_l_1
        adc     add_s_l_2
        sta     add_d_l
        lda     add_s_h_1
        adc     add_s_h_2
        sta     add_d_h
        rts

;##### 16 Bit Substract #####
sub_16:
        sec
        lda     add_s_l_1
        sbc     add_s_l_2
        sta     add_d_l
        lda     add_s_h_1
        sbc     add_s_h_2
        sta     add_d_h
        rts

;##### Waiting Timer #####
wait_long:
        ldx     #0
wait_loop_1:
        jsr     wait_short
        inx
        bne     wait_loop_1
        rts
wait_short:
        ldy     #0
wait_loop_2:
        nop
        iny
        bne     wait_loop_2
        rts

;##### HEX Data Table #####
hex_table:
        db      '0123456789ABCDEF'

;##### Debug Use : Debug Address Table Area #####
org     debug
        dw      tone_data_base

;##### Interrupt/Reset Vector Table #####
org     vector
        dw      nmi
        dw      start
        dw      int
        end

6502アセンブラ その2 MIDIマトリクス(CPUはZ80)

;-----------------------------------------------------------------------
;   MIDI SW Matrix Module Program [Z-80]
;-----------------------------------------------------------------------

name  x80main
p65c02

;##### Memory・I/O MAP #####
        program_area    equ     00000h
        int_area        equ     00038h
        work            equ     04000h
        ram_area        equ     04100h
        stack_pointer   equ     5fffh
        uart0           equ     08000h
        uart1           equ     08001h
        pia0            equ     09000h
        pia1            equ     09001h
        pia2            equ     09002h
        pia3            equ     09003h
        dipsw           equ     0a000h
        ext1            equ     0b000h
        ext2            equ     0c000h

;##### RAM Map #####
org     work
        work0           ds      1
        work1           ds      1
        work2           ds      1
        work3           ds      1
        rxtop           ds      1
        rxend           ds      1
        rxdata          ds      1
        rsb             ds      1
        exclusive_flag  ds      1
        ex_id           ds      1
        ex_chno         ds      1
        ex_function     ds      1
        ex_group        ds      1
        ex_machine      ds      1
        ex_data         ds      1
        ex_eox          ds      1
        matrix_data     ds      1
        matrix_address  ds      1
org     ram_area
        rxfifo          ds      256

;#####  Macro Defines #####
        macro   &push
                db      0f5h    ; push af
                endmac
        macro   &pop
                db      0f1h    ; pop af
                endmac
        macro   &ex
                db      008h    ; ex af,af'
                db      0d9h    ; exx
                endmac
        macro   &stack
                db      031h    ; ld sp,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &lda
                db      03ah    ; ld a,(nn)
                db      .low.\0
                db      .high.\0
                endmac
        macro   &sta
                db      032h    ; ld (nn),a
                db      .low.\0
                db      .high.\0
                endmac
        macro   &movi
                db      03eh    ; ld a,n
                db      \0
                db      032h    ; ld (nn),a
                db      .low.\1
                db      .high.\1
                endmac
        macro   &move
                db      03ah    ; ld a,(nn)
                db      .low.\0
                db      .high.\0
                db      032h    ; ld (nn),a
                db      .low.\1
                db      .high.\1
                endmac
        macro   &movex
                db      03ah    ; ld a,(nn)
                db      .low.\0
                db      .high.\0
                db      0eeh,0ffh ; xor a,0ffh
                db      032h    ; ld (nn),a
                db      .low.\1
                db      .high.\1
                endmac
        macro   &jsr
                db      0cdh    ; call xx,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &jsrnz
                db      0c4h    ; call nz,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &jmp
                db      0c3h    ; jp xx,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &beq
                db      0cah    ; jp z,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &bne
                db      0c2h    ; jp nz,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &rts
                db      0c9h    ; ret xx
                endmac
        macro   &equal_return
                db      0c8h    ; ret z
                endmac
        macro   ¬_equal_return
                db      0c0h    ; ret nz
                endmac
        macro   &and
                db      0e6h    ; and a,n
                db      \0
                endmac
        macro   &eor
                db      0eeh    ; xor a,n
                db      \0
                endmac
        macro   &ora
                db      0f6h    ; or a,n
                db      \0
                endmac
        macro   &cmp
                db      0feh    ; cp a,n
                db      \0
                endmac
        macro   &inc
                db      03ch    ; inc a
                endmac
        macro   &dec
                db      03dh    ; dec a
                endmac
        macro   &seth
                db      026h    ; ld h,n
                db      .high.\0
                endmac
        macro   &setl
                db      03ah    ; ld a,(nn)
                db      .low.\0
                db      .high.\0
                db      06fh    ; ld l,a
                endmac
        macro   &gethl
                db      07eh    ; ld a,(hl)
                endmac
        macro   &puthl
                db      077h    ; ld (hl),a
                endmac
        macro   &nop
                db      000h    ;
                endmac

;##### Program Start ( <-- Power ON Reset ) #####
org     program_area
        &stack  stack_pointer
        db      0f3h            ; 
        db      0edh,56h        ; 
        &jmp    main_routine

;##### MIDI INT Sequence #####
org     int_area
        &ex
        &setl   rxtop
        &lda    uart0
        &puthl
        db      2ch             ; 
        db      7dh             ; 
        &sta    rxtop
        &ex
        db      0fbh            ; 
        db      0edh,4dh        ; 

;##### Main Program Area #####
main_routine:
        &jsr    waiting
        &jsr    io_initial
loop:
        &jsr    midi_rx_check
        &jmp    loop

;##### Matrix Data Write Routine #####
matrix_write:
        &movi   1,pia0                  ; CS
        &move   matrix_address,pia1     ; Address
        &move   matrix_data,pia2        ; Data
        &nop
        &movi   0,pia0                  ; CS
        &nop
        &movi   1,pia0                  ; CS
        &rts

;##### MIDI Receive Data Check Sequence #####
midi_rx_check:
        &lda    rxend
        db      47h             ; 
        &lda    rxtop
        db      0b8h            ; 
        &equal_return
        db      78h             ; 
        &inc
        &sta    rxend
        db      68h             ; 
        &seth   rxfifo
        &gethl
        &sta    rxdata
        &cmp    0f0h
        &bne    midi_rx_not_exclusive_start_mark
        &movi   1,exclusive_flag
        &movi   0,rsb
        &rts
midi_rx_not_exclusive_start_mark:
        &lda    exclusive_flag
        &cmp    0
        &bne    midi_rx_exclusive
        &rts    ;********************* Exclusive Only !!!
midi_rx_exclusive:
        &lda    exclusive_flag
        &cmp    1
        &bne    midi_exclusive_not_1st
        &movi   2,exclusive_flag
        &move   rxdata,ex_id
        &rts
midi_exclusive_not_1st:
        &cmp    2
        &bne    midi_exclusive_not_2nd
        &movi   3,exclusive_flag
        &move   rxdata,ex_chno
        &rts
midi_exclusive_not_2nd:
        &cmp    3
        &bne    midi_exclusive_not_3rd
        &movi   4,exclusive_flag
        &move   rxdata,ex_function
        &rts
midi_exclusive_not_3rd:
        &cmp    4
        &bne    midi_exclusive_not_4th
        &movi   5,exclusive_flag
        &move   rxdata,ex_group
        &rts
midi_exclusive_not_4th:
        &cmp    5
        &bne    midi_exclusive_not_5th
        &movi   6,exclusive_flag
        &move   rxdata,ex_machine
        &rts
midi_exclusive_not_5th:
        &cmp    6
        &bne    midi_exclusive_not_6th
        &movi   7,exclusive_flag
        &move   rxdata,ex_data
        &rts
midi_exclusive_not_6th:
        &move   rxdata,ex_eox
        &lda    ex_id
        &cmp    40h
        &bne    midi_rx_exclusive_format_error
        &lda    ex_chno
        &cmp    0fh
        &bne    midi_rx_exclusive_format_error
        &lda    ex_function
        &and    11110000b
        &cmp    70h
        &bne    midi_rx_exclusive_format_error
        &lda    ex_group
        &cmp    7fh
        &bne    midi_rx_exclusive_format_error
        &lda    ex_eox
        &cmp    0f7h
        &bne    midi_rx_exclusive_format_error
        &lda    ex_machine
        &cmp    1               ; MACHINE ID = <1> **********
        &bne    midi_rx_exclusive_format_error
        &move   ex_data,matrix_data
        &lda    ex_function
        &cmp    70h
        &beq    midi_rx_exclusive_70
        &cmp    71h
        &beq    midi_rx_exclusive_71
        &cmp    72h
        &beq    midi_rx_exclusive_72
        &cmp    73h
        &beq    midi_rx_exclusive_73
        &cmp    74h
        &beq    midi_rx_exclusive_74
        &cmp    75h
        &beq    midi_rx_exclusive_75
        &cmp    76h
        &beq    midi_rx_exclusive_76
        &cmp    77h
        &beq    midi_rx_exclusive_77
        &rts
midi_rx_exclusive_70:
        &movi   0,matrix_address
        &jsr    matrix_write
        &rts
midi_rx_exclusive_71:
        &movi   1,matrix_address
        &jsr    matrix_write
        &rts
midi_rx_exclusive_72:
        &movi   2,matrix_address
        &jsr    matrix_write
        &rts
midi_rx_exclusive_73:
        &movi   3,matrix_address
        &jsr    matrix_write
        &rts
midi_rx_exclusive_74:
        &movi   4,matrix_address
        &jsr    matrix_write
        &rts
midi_rx_exclusive_75:
        &movi   5,matrix_address
        &jsr    matrix_write
        &rts
midi_rx_exclusive_76:
        &movi   6,matrix_address
        &jsr    matrix_write
        &rts
midi_rx_exclusive_77:
        &movi   7,matrix_address
        &jsr    matrix_write
        &rts
midi_rx_exclusive_format_error:
        &movi   0,exclusive_flag
        &rts

;##### System I/O Initialize #####
io_initial:
; 
        &ex
        &seth   rxfifo
        &ex
; 
        &movi   0,work0
        &seth   work
ram_clear_work:
        &setl   work0
        db      3eh,00h         ; ld a,#0
        &puthl
        &lda    work0
        &inc
        &sta    work0
        &cmp    0
        &bne    ram_clear_work
        &seth   rxfifo
ram_clear_rxfifo:
        &setl   work0
        db      3eh,00h         ; ld a,#0
        &puthl
        &lda    work0
        &inc
        &sta    work0
        &cmp    0
        &bne    ram_clear_rxfifo
; <71055 PIA Initialize>
        &movi   10000000b,pia3          ; Port<2>=<1>=<0>=Out
        &movi   1,pia0          ; CS
        &movi   0,pia1          ; Address
        &movi   0,pia2          ; Data
; <71051 UART Initialize>
        &movi   0,uart1
        &movi   0,uart1
        &movi   0,uart1
        &movi   01000000b,uart1         ; Software Reset
        &movi   01001110b,uart1         ; Mode Set
        &movi   00010100b,uart1         ; Command Set
; 
        &movi   0,matrix_address
        &movi   0,matrix_data
        &jsr    matrix_write
        &movi   1,matrix_address
        &movi   1,matrix_data
        &jsr    matrix_write
        &movi   2,matrix_address
        &movi   2,matrix_data
        &jsr    matrix_write
        &movi   3,matrix_address
        &movi   3,matrix_data
        &jsr    matrix_write
        &movi   4,matrix_address
        &movi   4,matrix_data
        &jsr    matrix_write
        &movi   5,matrix_address
        &movi   5,matrix_data
        &jsr    matrix_write
        &movi   6,matrix_address
        &movi   6,matrix_data
        &jsr    matrix_write
        &movi   7,matrix_address
        &movi   7,matrix_data
        &jsr    matrix_write
; 
        db      0fbh            ; 
        &rts

;##### Waiting Timer #####
waiting:
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        &rts

        end

6502アセンブラ その3 RS232C --> MIDI (CPUはZ80)

;-----------------------------------------------------------------------
;   RS232C ---> MIDI Out Module Program [Z-80]
;-----------------------------------------------------------------------

name  y80main
p65c02

;##### Memory・I/O MAP #####
        program_area    equ     00000h
        int_area        equ     00038h
        work_area       equ     04000h
        ram_area        equ     04100h
        stack_pointer   equ     5fffh
        uart0           equ     08000h
        uart1           equ     08001h
        rs232c          equ     0b000h

;##### RAM Map #####
org     work_area
        work            ds      1
        hlflag          ds      1
        hlbuf           ds      1
        databuf         ds      1
        rxtop           ds      1
        rxend           ds      1
        rxdata          ds      1
        txtop           ds      1
        txend           ds      1
        txdata          ds      1
org     ram_area
        rxfifo          ds      256
        txfifo          ds      256

;#####  Macro Defines #####
        macro   &push
                db      0f5h    ; push af
                endmac
        macro   &pop
                db      0f1h    ; pop af
                endmac
        macro   &ex
                db      008h    ; ex af,af'
                db      0d9h    ; exx
                endmac
        macro   &stack
                db      031h    ; ld sp,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &lda
                db      03ah    ; ld a,(nn)
                db      .low.\0
                db      .high.\0
                endmac
        macro   &sta
                db      032h    ; ld (nn),a
                db      .low.\0
                db      .high.\0
                endmac
        macro   &movi
                db      03eh    ; ld a,n
                db      \0
                db      032h    ; ld (nn),a
                db      .low.\1
                db      .high.\1
                endmac
        macro   &move
                db      03ah    ; ld a,(nn)
                db      .low.\0
                db      .high.\0
                db      032h    ; ld (nn),a
                db      .low.\1
                db      .high.\1
                endmac
        macro   &movex
                db      03ah    ; ld a,(nn)
                db      .low.\0
                db      .high.\0
                db      0eeh,0ffh ; xor a,0ffh
                db      032h    ; ld (nn),a
                db      .low.\1
                db      .high.\1
                endmac
        macro   &jsr
                db      0cdh    ; call xx,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &jsrnz
                db      0c4h    ; call nz,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &jmp
                db      0c3h    ; jp xx,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &beq
                db      0cah    ; jp z,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &bne
                db      0c2h    ; jp nz,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &rts
                db      0c9h    ; ret xx
                endmac
        macro   &and
                db      0e6h    ; and a,n
                db      \0
                endmac
        macro   &eor
                db      0eeh    ; xor a,n
                db      \0
                endmac
        macro   &ora
                db      0f6h    ; or a,n
                db      \0
                endmac
        macro   &cmp
                db      0feh    ; cp a,n
                db      \0
                endmac
        macro   &inc
                db      03ch    ; inc a
                endmac
        macro   &dec
                db      03dh    ; dec a
                endmac
        macro   &seth
                db      026h    ; ld h,n
                db      .high.\0
                endmac
        macro   &setl
                db      03ah    ; ld a,(nn)
                db      .low.\0
                db      .high.\0
                db      06fh    ; ld l,a
                endmac
        macro   &gethl
                db      07eh    ; ld a,(hl)
                endmac
        macro   &puthl
                db      077h    ; ld (hl),a
                endmac

        macro   &nop
                db      000h    ;
                endmac

;##### Program Start ( <-- Power ON Reset ) #####
org     program_area
        &stack  stack_pointer
        db      0f3h            ; 
        db      0edh,56h        ; 
        &jmp    main_routine

;##### RS232C INT Sequence #####
org     int_area
        &ex
        &setl   rxtop
        &lda    rs232c
        &puthl
        db      2ch             ; 
        db      7dh             ; 
        &sta    rxtop
        &ex
        db      0fbh            ; 
        db      0edh,4dh        ; 

;##### Main Program Area #####
main_routine:
        &jsr    io_initial
loop:
        &jsr    rs232c_rx_check
        &jsr    midi_tx_sequence
        &jmp    loop

;##### RS232C Receive Data Check Sequence #####
rs232c_rx_check:
        &lda    rxend
        db      47h             ; 
        &lda    rxtop
        db      0b8h            ; 
        &bne    rs232c_rx_data_found
        &rts
rs232c_rx_data_found:
        db      78h             ; 
        &inc
        &sta    rxend
        db      68h             ; 
        &seth   rxfifo
        &gethl
        &sta    rxdata
        &cmp    03ah
        &bne    rs232c_rx_check_not_colon
        &movi   0,hlflag
        &rts
rs232c_rx_check_not_colon:
        &cmp    030h
        &bne    rs232c_rx_check_not_0
        &movi   0,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_0:
        &cmp    031h
        &bne    rs232c_rx_check_not_1
        &movi   1,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_1:
        &cmp    032h
        &bne    rs232c_rx_check_not_2
        &movi   2,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_2:
        &cmp    033h
        &bne    rs232c_rx_check_not_3
        &movi   3,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_3:
        &cmp    034h
        &bne    rs232c_rx_check_not_4
        &movi   4,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_4:
        &cmp    035h
        &bne    rs232c_rx_check_not_5
        &movi   5,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_5:
        &cmp    036h
        &bne    rs232c_rx_check_not_6
        &movi   6,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_6:
        &cmp    037h
        &bne    rs232c_rx_check_not_7
        &movi   7,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_7:
        &cmp    038h
        &bne    rs232c_rx_check_not_8
        &movi   8,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_8:
        &cmp    039h
        &bne    rs232c_rx_check_not_9
        &movi   9,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_9:
        &cmp    041h
        &bne    rs232c_rx_check_not_10
        &movi   10,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_10:
        &cmp    042h
        &bne    rs232c_rx_check_not_11
        &movi   11,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_11:
        &cmp    043h
        &bne    rs232c_rx_check_not_12
        &movi   12,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_12:
        &cmp    044h
        &bne    rs232c_rx_check_not_13
        &movi   13,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_13:
        &cmp    045h
        &bne    rs232c_rx_check_not_14
        &movi   14,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_14:
        &cmp    046h
        &bne    rs232c_rx_check_not_15
        &movi   15,hlbuf
        &jmp    rs232c_hl_check
rs232c_rx_check_not_15:
        &rts
rs232c_hl_check:
        &lda    hlflag
        &cmp    0
        &bne    rs232c_rx_low_data
        &movi   1,hlflag
        &lda    hlbuf
        db      0cbh            ; 
        db      027h            ;
        db      0cbh            ; 
        db      027h            ;
        db      0cbh            ; 
        db      027h            ;
        db      0cbh            ; 
        db      027h            ;
        &sta    databuf
        &rts
rs232c_rx_low_data:
        &movi   0,hlflag
        &lda    hlbuf
        db      47h             ; 
        &lda    databuf
        db      0b0h            ; 
        &sta    databuf
        &seth   txfifo
        &setl   txtop
        &lda    databuf
        &puthl
        db      2ch             ; 
        db      7dh             ; 
        &sta    txtop
        &rts

;##### MIDI Transmit Data Check Sequence #####
midi_tx_sequence:
        &lda    txend
        db      47h             ; 
        &lda    txtop
        db      0b8h            ; 
        &bne    midi_tx_data_found
        &rts
midi_tx_data_found:
        &lda    uart1
        &and    00000001b
        &cmp    0
        &bne    midi_tx_uart_ok
        &rts
midi_tx_uart_ok:
        db      78h             ; 
        &inc
        &sta    txend
        db      68h             ; 
        &seth   txfifo
        &gethl
        &sta    uart0
        &rts

;##### System I/O Initialize #####
io_initial:
; <71051 MIDI UART Initialize>
        &movi   0,uart1
        &movi   0,uart1
        &movi   0,uart1
        &movi   01000000b,uart1         ; Software Reset
        &movi   01001110b,uart1         ; Mode Set
        &movi   00010001b,uart1         ; Command Set
; <71051 RS232C UART Initialize>
        &movi   0,rs232c+1
        &movi   0,rs232c+1
        &movi   0,rs232c+1
        &movi   01000000b,rs232c+1      ; Software Reset
        &movi   01001110b,rs232c+1      ; Mode Set
        &movi   00010100b,rs232c+1      ; Command Set
; 
        &ex
        &seth   rxfifo
        &ex
; 
        &movi   0,work
        &seth   work_area
ram_clear_work:
        &setl   work
        db      3eh,00h         ; ld a,#0
        &puthl
        &lda    work
        &inc
        &sta    work
        &cmp    0
        &bne    ram_clear_work
; 
        db      0fbh            ; 
        &rts

        end

6502アセンブラ その4 キーボード (CPUはZ80)

;-----------------------------------------------------------------------
;   KEBOARD Module Program [Z-80]
;-----------------------------------------------------------------------

name  z80main
p65c02

;##### Memory・I/O MAP #####
        program_area    equ     00000h
        int_area        equ     00038h
        work            equ     04000h
        ram_area        equ     04100h
        stack_pointer   equ     5fffh
        uart0           equ     08000h
        uart1           equ     08001h
        pia0            equ     09000h
        pia1            equ     09001h
        pia2            equ     09002h
        pia3            equ     09003h
        dipsw           equ     0a000h
        ext1            equ     0b000h
        ext2            equ     0c000h

;##### RAM Map #####
org     work
        work0           ds      1
        work1           ds      1
        work2           ds      1
        work3           ds      1
        rxtop           ds      1
        rxend           ds      1
        rxdata          ds      1
        exclusive_flag  ds      1
        ex_id           ds      1
        ex_chno         ds      1
        ex_function     ds      1
        ex_group        ds      1
        ex_machine      ds      1
        ex_data         ds      1
        ex_eox          ds      1
        txtop           ds      1
        txend           ds      1
        txdata          ds      1
        channel         ds      1
        velocity        ds      1
        offset          ds      1
        sw_scan_phase   ds      1
        group_offset    ds      1
        group_event_bit ds      1
        group_status    ds      1
        rsb             ds      1
        dcb             ds      1
        note_buffer     ds      1
        all_off_timer   ds      1
        all_off_flag    ds      1
        pedal_new       ds      1
        pedal_old       ds      1
        pedal_timer     ds      1
        group_low_offset        ds      1
        sw_group_1_phase_0      ds      1
        sw_group_2_phase_0      ds      1
        sw_group_3_phase_0      ds      1
        sw_group_4_phase_0      ds      1
        sw_group_5_phase_0      ds      1
        sw_group_6_phase_0      ds      1
        sw_group_7_phase_0      ds      1
        sw_group_8_phase_0      ds      1
        sw_group_1_phase_2      ds      1
        sw_group_2_phase_2      ds      1
        sw_group_3_phase_2      ds      1
        sw_group_4_phase_2      ds      1
        sw_group_5_phase_2      ds      1
        sw_group_6_phase_2      ds      1
        sw_group_7_phase_2      ds      1
        sw_group_8_phase_2      ds      1
org     ram_area
        rxfifo          ds      256
        txfifo          ds      256

;#####  Macro Defines #####
        macro   &push
                db      0f5h    ; push af
                endmac
        macro   &pop
                db      0f1h    ; pop af
                endmac
        macro   &ex
                db      008h    ; ex af,af'
                db      0d9h    ; exx
                endmac
        macro   &stack
                db      031h    ; ld sp,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &lda
                db      03ah    ; ld a,(nn)
                db      .low.\0
                db      .high.\0
                endmac
        macro   &sta
                db      032h    ; ld (nn),a
                db      .low.\0
                db      .high.\0
                endmac
        macro   &movi
                db      03eh    ; ld a,n
                db      \0
                db      032h    ; ld (nn),a
                db      .low.\1
                db      .high.\1
                endmac
        macro   &move
                db      03ah    ; ld a,(nn)
                db      .low.\0
                db      .high.\0
                db      032h    ; ld (nn),a
                db      .low.\1
                db      .high.\1
                endmac
        macro   &movex
                db      03ah    ; ld a,(nn)
                db      .low.\0
                db      .high.\0
                db      0eeh,0ffh ; xor a,0ffh
                db      032h    ; ld (nn),a
                db      .low.\1
                db      .high.\1
                endmac
        macro   &jsr
                db      0cdh    ; call xx,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &jsrnz
                db      0c4h    ; call nz,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &jmp
                db      0c3h    ; jp xx,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &beq
                db      0cah    ; jp z,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &bne
                db      0c2h    ; jp nz,nn
                db      .low.\0
                db      .high.\0
                endmac
        macro   &rts
                db      0c9h    ; ret xx
                endmac
        macro   &equal_return
                db      0c8h    ; ret z
                endmac
        macro   ¬_equal_return
                db      0c0h    ; ret nz
                endmac
        macro   &and
                db      0e6h    ; and a,n
                db      \0
                endmac
        macro   &eor
                db      0eeh    ; xor a,n
                db      \0
                endmac
        macro   &ora
                db      0f6h    ; or a,n
                db      \0
                endmac
        macro   &cmp
                db      0feh    ; cp a,n
                db      \0
                endmac
        macro   &inc
                db      03ch    ; inc a
                endmac
        macro   &dec
                db      03dh    ; dec a
                endmac
        macro   &seth
                db      026h    ; ld h,n
                db      .high.\0
                endmac
        macro   &setl
                db      03ah    ; ld a,(nn)
                db      .low.\0
                db      .high.\0
                db      06fh    ; ld l,a
                endmac
        macro   &gethl
                db      07eh    ; ld a,(hl)
                endmac
        macro   &puthl
                db      077h    ; ld (hl),a
                endmac
        macro   &nop
                db      000h    ;
                endmac

;##### Program Start ( <-- Power ON Reset ) #####
org     program_area
        &stack  stack_pointer
        db      0f3h            ; 
        db      0edh,56h        ; 
        &jmp    main_routine

;##### MIDI INT Sequence #####
org     int_area
        &ex
        &setl   rxtop
        &lda    uart0
        &puthl
        db      2ch             ; 
        db      7dh             ; 
        &sta    rxtop
        &ex
        db      0fbh            ; 
        db      0edh,4dh        ; 

;##### Main Program Area #####
main_routine:
        &jsr    waiting
        &jsr    io_initial
loop:
        &jsr    sw_scan
        &jsr    pedal_scan
        &jsr    all_note_off_check
        &jsr    midi_rx_check
        &jsr    midi_tx_sequence
        &jmp    loop

;##### Pedal Check Routine #####
pedal_scan:
        &lda    pedal_timer
        &inc
        &sta    pedal_timer
        &cmp    10
        ¬_equal_return
        &movi   0,pedal_timer
        &lda    dipsw
        &and    00000001b
        &sta    pedal_new
        db      47h             ; 
        &lda    pedal_old
        db      0a8h            ; 
        &equal_return
        &seth   txfifo
        &setl   txtop
        &lda    channel
        &and    00001111b
        &ora    10110000b
        &puthl
        db      2ch             ; 
        db      3eh,40h         ; ld a,#40h
        &puthl
        db      2ch             ; 
        &lda    pedal_new
        &cmp    0
        &beq    pedal_off
        db      3eh,7fh         ; ld a,#7fh
        &jmp    pedal_final
pedal_off:
        db      0afh            ; 
pedal_final:
        &puthl
        db      2ch             ; 
        db      7dh             ; 
        &sta    txtop
        &move   pedal_new,pedal_old
        &rts

;##### All Note Off Check Routine #####
all_note_off_check:
        &lda    all_off_timer
        &inc
        &sta    all_off_timer
        &cmp    50
        ¬_equal_return
        &movi   0,all_off_timer
        &lda    all_off_flag
        &cmp    0
        &equal_return
        &lda    sw_group_1_phase_0
        &cmp    0
        ¬_equal_return
        &lda    sw_group_2_phase_0
        &cmp    0
        ¬_equal_return
        &lda    sw_group_3_phase_0
        &cmp    0
        ¬_equal_return
        &lda    sw_group_4_phase_0
        &cmp    0
        ¬_equal_return
        &lda    sw_group_5_phase_0
        &cmp    0
        ¬_equal_return
        &lda    sw_group_6_phase_0
        &cmp    0
        ¬_equal_return
        &lda    sw_group_7_phase_0
        &cmp    0
        ¬_equal_return
        &lda    sw_group_8_phase_0
        &cmp    0
        ¬_equal_return
        &seth   txfifo
        &setl   txtop
        &lda    channel
        &and    00001111b
        &ora    10110000b
        &puthl
        db      2ch             ; 
        db      3eh,7bh         ; ld a,#7bh
        &puthl
        db      2ch             ; 
        db      3eh,00h         ; ld a,#0
        &puthl
        db      2ch             ; 
        db      7dh             ; 
        &sta    txtop
        &movi   0,all_off_flag
        &rts

;##### SW Event Check Routine #####
sw_scan_event_check:
        &lda    group_event_bit
        &and    00000001b
        &cmp    0
        &jsrnz  sw_scan_001
        &lda    group_event_bit
        &and    00000010b
        &cmp    0
        &jsrnz  sw_scan_002
        &lda    group_event_bit
        &and    00000100b
        &cmp    0
        &jsrnz  sw_scan_003
        &lda    group_event_bit
        &and    00001000b
        &cmp    0
        &jsrnz  sw_scan_004
        &lda    group_event_bit
        &and    00010000b
        &cmp    0
        &jsrnz  sw_scan_005
        &lda    group_event_bit
        &and    00100000b
        &cmp    0
        &jsrnz  sw_scan_006
        &lda    group_event_bit
        &and    01000000b
        &cmp    0
        &jsrnz  sw_scan_007
        &rts

sw_scan_001:
        &movi   0,group_low_offset
        &jmp    sw_scan_010
sw_scan_002:
        &movi   1,group_low_offset
        &jmp    sw_scan_010
sw_scan_003:
        &movi   2,group_low_offset
        &jmp    sw_scan_010
sw_scan_004:
        &movi   3,group_low_offset
        &jmp    sw_scan_010
sw_scan_005:
        &movi   4,group_low_offset
        &jmp    sw_scan_010
sw_scan_006:
        &movi   5,group_low_offset
        &jmp    sw_scan_010
sw_scan_007:
        &movi   6,group_low_offset
        &jmp    sw_scan_010

sw_scan_010:
        &seth   txfifo
        &setl   txtop
        &lda    channel
        &and    00001111b
        &ora    10010000b
        &puthl
        db      2ch             ; 
        &lda    group_offset
        db      47h             ; 
        &lda    group_low_offset
        db      80h             ; 
        db      47h             ; 
        &lda    offset
        db      80h             ; 
        db      47h             ; 
        &lda    group_offset
        &cmp    0
        &beq    sw_scan_adjust
        db      05h             ; 
sw_scan_adjust:
        db      78h             ; 
        &and    01111111b
        &puthl
        db      2ch             ; 
        &lda    group_event_bit
        db      47h             ; 
        &lda    group_status
        db      0a0h            ; 
        &cmp    0
        &beq    sw_scan_key_off
        &movi   1,all_off_flag
        &lda    velocity
        &jmp    sw_scan_final
sw_scan_key_off:
        db      0afh            ; 
sw_scan_final:
        &puthl
        db      2ch             ; 
        db      7dh             ; 
        &sta    txtop
        &rts

;##### SW Scan Routine #####
sw_scan:
        &lda    sw_scan_phase
        db      47h             ; 
        &inc
        &sta    sw_scan_phase
        db      78h             ; 
        &cmp    1
        &beq    sw_scan_seq_1
        &cmp    2
        &beq    sw_scan_seq_2
        &cmp    3
        &beq    sw_scan_seq_3
        &cmp    4
        &beq    sw_scan_seq_4
        &cmp    5
        &beq    sw_scan_seq_5
        &cmp    6
        &beq    sw_scan_seq_6
        &cmp    7
        &beq    sw_scan_seq_7
        &cmp    8
        &beq    sw_scan_seq_8
        &movi   1,sw_scan_phase
        &rts

sw_scan_seq_1:
        &movex  pia0,sw_group_1_phase_2
        &movi   11111101b,pia1
        &lda    sw_group_1_phase_2
        db      47h             ; 
        &lda    sw_group_1_phase_0
        db      0a8h            ; 
        &equal_return
        &sta    group_event_bit
        db      78h             ; 
        &sta    sw_group_1_phase_0
        &sta    group_status
        &movi   0,group_offset
        &jsr    sw_scan_event_check
        &rts
sw_scan_seq_2:
        &movex  pia0,sw_group_2_phase_2
        &movi   11111011b,pia1
        &lda    sw_group_2_phase_2
        db      47h             ; 
        &lda    sw_group_2_phase_0
        db      0a8h            ; 
        &equal_return
        &sta    group_event_bit
        db      78h             ; 
        &sta    sw_group_2_phase_0
        &sta    group_status
        &movi   7,group_offset
        &jsr    sw_scan_event_check
        &rts
sw_scan_seq_3:
        &movex  pia0,sw_group_3_phase_2
        &movi   11110111b,pia1
        &lda    sw_group_3_phase_2
        db      47h             ; 
        &lda    sw_group_3_phase_0
        db      0a8h            ; 
        &equal_return
        &sta    group_event_bit
        db      78h             ; 
        &sta    sw_group_3_phase_0
        &movi   13,group_offset
        &jsr    sw_scan_event_check
        &rts
sw_scan_seq_4:
        &movex  pia0,sw_group_4_phase_2
        &movi   11101111b,pia1
        &lda    sw_group_4_phase_2
        db      47h             ; 
        &lda    sw_group_4_phase_0
        db      0a8h            ; 
        &equal_return
        &sta    group_event_bit
        db      78h             ; 
        &sta    sw_group_4_phase_0
        &movi   19,group_offset
        &jsr    sw_scan_event_check
        &rts
sw_scan_seq_5:
        &movex  pia0,sw_group_5_phase_2
        &movi   11011111b,pia1
        &lda    sw_group_5_phase_2
        db      47h             ; 
        &lda    sw_group_5_phase_0
        db      0a8h            ; 
        &equal_return
        &sta    group_event_bit
        db      78h             ; 
        &sta    sw_group_5_phase_0
        &movi   25,group_offset
        &jsr    sw_scan_event_check
        &rts
sw_scan_seq_6:
        &movex  pia0,sw_group_6_phase_2
        &movi   10111111b,pia1
        &lda    sw_group_6_phase_2
        db      47h             ; 
        &lda    sw_group_6_phase_0
        db      0a8h            ; 
        &equal_return
        &sta    group_event_bit
        db      78h             ; 
        &sta    sw_group_6_phase_0
        &movi   31,group_offset
        &jsr    sw_scan_event_check
        &rts
sw_scan_seq_7:
        &movex  pia0,sw_group_7_phase_2
        &movi   01111111b,pia1
        &lda    sw_group_7_phase_2
        db      47h             ; 
        &lda    sw_group_7_phase_0
        db      0a8h            ; 
        &equal_return
        &sta    group_event_bit
        db      78h             ; 
        &sta    sw_group_7_phase_0
        &movi   37,group_offset
        &jsr    sw_scan_event_check
        &rts
sw_scan_seq_8:
        &movex  pia0,sw_group_8_phase_2
        &movi   11111111b,pia1
        &lda    sw_group_8_phase_2
        db      47h             ; 
        &lda    sw_group_8_phase_0
        db      0a8h            ; 
        &equal_return
        db      78h             ; 
        &sta    sw_group_8_phase_0
        &movi   43,group_offset
        &jsr    sw_scan_event_check
        &movi   11111110b,pia1
        &movi   1,sw_scan_phase
        &rts

;##### MIDI Receive Data Check Sequence #####
midi_rx_check:
        &lda    rxend
        db      47h             ; 
        &lda    rxtop
        db      0b8h            ; 
        &equal_return
        db      78h             ; 
        &inc
        &sta    rxend
        db      68h             ; 
        &seth   rxfifo
        &gethl
        &sta    rxdata
        &cmp    0f0h
        &bne    midi_rx_not_exclusive_start_mark
        &movi   1,exclusive_flag
        &movi   0,rsb
        &rts
midi_rx_not_exclusive_start_mark:
        &lda    exclusive_flag
        &cmp    0
        &bne    midi_rx_exclusive
        &rts    ;********************* Exclusive Only !!!
        &lda    rxdata
        &and    10000000b
        &cmp    0
        &beq    midi_rx_running
        &lda    rxdata
        &and    11110000b
        &cmp    0f0h
        &bne    midi_rx_not_real_time
        &rts
midi_rx_not_real_time:
        &cmp    90h
        &beq    midi_rx_note_on_status
midi_rx_inhibit_status:
        &movi   0,rsb
        &rts
midi_rx_note_on_status:
        &lda    rxdata
        &and    00001111b
        db      47h             ; 
        &lda    channel
        db      0b8h            ; 
        &beq    midi_rx_inhibit_status
        &move   rxdata,rsb
        &movi   0,dcb
        &rts
midi_rx_running:
        &lda    rsb
        &cmp    0
        &bne    midi_rx_true_running
        &rts
midi_rx_true_running:
        &lda    dcb
        &cmp    0
        &bne    midi_rx_3rd_data
        &movi   1,dcb
        &move   rxdata,note_buffer
        &rts
midi_rx_3rd_data:
        &movi   0,dcb
        &seth   txfifo
        &setl   txtop
        &lda    rsb
        &puthl
        db      2ch             ; 
        &lda    note_buffer
        &puthl
        db      2ch             ; 
        &lda    rxdata
        &puthl
        db      2ch             ; 
        db      7dh             ; 
        &sta    txtop
        &rts
midi_rx_exclusive:
        &lda    exclusive_flag
        &cmp    1
        &bne    midi_exclusive_not_1st
        &movi   2,exclusive_flag
        &move   rxdata,ex_id
        &rts
midi_exclusive_not_1st:
        &cmp    2
        &bne    midi_exclusive_not_2nd
        &movi   3,exclusive_flag
        &move   rxdata,ex_chno
        &rts
midi_exclusive_not_2nd:
        &cmp    3
        &bne    midi_exclusive_not_3rd
        &movi   4,exclusive_flag
        &move   rxdata,ex_function
        &rts
midi_exclusive_not_3rd:
        &cmp    4
        &bne    midi_exclusive_not_4th
        &movi   5,exclusive_flag
        &move   rxdata,ex_group
        &rts
midi_exclusive_not_4th:
        &cmp    5
        &bne    midi_exclusive_not_5th
        &movi   6,exclusive_flag
        &move   rxdata,ex_machine
        &rts
midi_exclusive_not_5th:
        &cmp    6
        &bne    midi_exclusive_not_6th
        &movi   7,exclusive_flag
        &move   rxdata,ex_data
        &rts
midi_exclusive_not_6th:
        &move   rxdata,ex_eox
        &lda    ex_id
        &cmp    40h
        &bne    midi_rx_exclusive_format_error
        &lda    ex_chno
        &cmp    0fh
        &bne    midi_rx_exclusive_format_error
        &lda    ex_function
        &and    11110000b
        &cmp    70h
        &bne    midi_rx_exclusive_format_error
        &lda    ex_group
        &cmp    7fh
        &bne    midi_rx_exclusive_format_error
        &lda    ex_eox
        &cmp    0f7h
        &bne    midi_rx_exclusive_format_error
        &lda    ex_machine
        &cmp    0               ; MACHINE ID = <0> **********
        &bne    midi_rx_exclusive_format_error
        &lda    ex_function
        &cmp    70h
        &beq    midi_rx_exclusive_70_channel_set
        &cmp    71h
        &beq    midi_rx_exclusive_71_velocity_set
        &cmp    72h
        &beq    midi_rx_exclusive_72_offset_set
        &rts
midi_rx_exclusive_70_channel_set:
        &move   ex_data,channel
        &rts
midi_rx_exclusive_71_velocity_set:
        &move   ex_data,velocity
        &rts
midi_rx_exclusive_72_offset_set:
        &move   ex_data,offset
        &rts
midi_rx_exclusive_format_error:
        &movi   0,exclusive_flag
        &rts

;##### MIDI Transmit Data Check Sequence #####
midi_tx_sequence:
        &lda    txend
        db      47h             ; 
        &lda    txtop
        db      0b8h            ; 
        &bne    midi_tx_data_found
        &rts
midi_tx_data_found:
        &lda    uart1
        &and    00000001b
        &cmp    0
        &bne    midi_tx_uart_ok
        &rts
midi_tx_uart_ok:
        db      78h             ; 
        &inc
        &sta    txend
        db      68h             ; 
        &seth   txfifo
        &gethl
        &sta    uart0
        &rts

;##### System I/O Initialize #####
io_initial:
; 
        &ex
        &seth   rxfifo
        &ex
; 
        &movi   0,work0
        &seth   work
ram_clear_work:
        &setl   work0
        db      3eh,00h         ; ld a,#0
        &puthl
        &lda    work0
        &inc
        &sta    work0
        &cmp    0
        &bne    ram_clear_work
        &seth   rxfifo
ram_clear_rxfifo:
        &setl   work0
        db      3eh,00h         ; ld a,#0
        &puthl
        &lda    work0
        &inc
        &sta    work0
        &cmp    0
        &bne    ram_clear_rxfifo
; <71055 PIA Initialize>
        &movi   10010000b,pia3          ; Port<2>=<1>=Out,<0>=In
        &movi   11111110b,pia1
        &movi   0ffh,pia2
; <71051 UART Initialize>
        &movi   0,uart1
        &movi   0,uart1
        &movi   0,uart1
        &movi   01000000b,uart1         ; Software Reset
        &movi   01001110b,uart1         ; Mode Set
        &movi   00010101b,uart1         ; Command Set
; 
        &movi   1,all_off_flag
        &movi   36,offset
        &movi   14,channel
        &movi   7fh,velocity
; 
        db      0fbh            ; 
        &rts

;##### Waiting Timer #####
waiting:
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        db      0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
        &rts

        end

TLCS90アセンブラ その1 中村滋延マシン

;----------------------------------------------------------------:
;                                                                |
;       N A K A M U R A    S P E C I A L    M O D U L E          |
;                                                                |
;           for NO.1 : MIDI Performance Trigger System           |
;                                                                |
;----------------------------------------------------------------:

;========== Data Area =====================================
        dseg    abs d_area(04000h)
para_area:      ds      128     ; (dummy)
rx_fifo:        ds      256     ; Internal UART : MIDI(1) RX
tx_fifo:        ds      256*16  ;                         TX
mode_fifo:      ds      256*8   ; Echo Mode
time_fifo:      ds      256*8   ; Echo Time Value
        iseg    abs i_area(0ff00h)
int_flg:        ds      1       ; Interrupt Check Flags
rx_top:         ds      1       ; MIDI Receive FIFO Top Pointer
rx_end:         ds      1       ; MIDI Receive FIFO End Pointer
rx_data:        ds      1       ; MIDI Received 1 Byte Data Buffer
status:         ds      1       ; MIDI Status Buffer
channel:        ds      1       ; MIDI Channel Buffer
rsb:            ds      1       ; Running Status Buffer
dcb:            ds      1       ; Data Count Buffer
data_1st:       ds      1       ; ( Key Number Buffer )
data_2nd:       ds      1       ; ( Velocity Buffer )
tx_top:         ds      2       ; MIDI Transmit FIFO Top Pointer
tx_end:         ds      2       ; MIDI Transmit FIFO End Pointer
echo_mode:      ds      1       ; Echo Mode Selector
echo_counter:   ds      1       ; Echo Check Counter
echo_top:       ds      8       ; Echo FIFO Top Pointer
echo_end:       ds      8       ; Echo FIFO End Pointer
timer_1msec:    ds      1       ; 1msec Timer
ad_buffer:      ds      2       ; A/D Data Buffer
note_status:    ds      2       ; Note Event Status
note_ctr:       ds      2       ; Note Event Counter
select_ctr:     ds      2       ; Phrase Select Number Counter
select_buf:     ds      2       ; Phrase Select Buffer
selected:       ds      1       ; Phrase Select Number
point_buff:     ds      4       ; Pointer [HL] Buffer
event_mode:     ds      1       ; MSB : [0]=ON / [1]=OFF
free_timer:     ds      1       ; Semi-Random Counter
end_mark:       ds      3       ; Use for Debug !

;========== Bit Map Defines Area ==========================
int_bit_midi    equ     0       ; Int_Flg       ; MIDI
int_bit_tim_1   equ     1                       ; 1msec

;========== RAM / Port Area ===============================
user_area       equ     04000h  ; Universal RAM Area Top Address
dsp-01          equ     0fa00h  ; Tone Generator [DSP-01] : FA00-FBFF
internal_ram    equ     0fdc0h  ; Internal RAM Top Address
port_p01cr      equ     0ffc2h  ; Port 0/1 Control
port_p2cr       equ     0ffc5h  ; Port-2 Control
port_iop3       equ     0ffc6h  ; Port-3
port_p3cr       equ     0ffc7h  ; Port-3 Control
port_p4cr       equ     0ffc9h  ; Port-4 Control
port_iop5       equ     0ffcah  ; Port-5
port_smmod      equ     0ffcbh  ; Stepping Motor Mode
port_iop6       equ     0ffcch  ; Port-6
port_iop7       equ     0ffcdh  ; Port-7
port_p67cr      equ     0ffceh  ; Port-6/7 Control
port_iop8       equ     0ffd0h  ; Port-8
port_p8cr       equ     0ffd1h  ; Port-8 Control
port_wdmod      equ     0ffd2h  ; Watch Dog Timer Mode
port_wdcr       equ     0ffd3h  ; Watch Dog Timer Control
port_treg0      equ     0ffd4h  ; 16bit Timer <0/1>
port_treg2      equ     0ffd6h  ; 8bit Timer <2>
port_treg3      equ     0ffd7h  ; 8bit Timer <3>
port_tclk       equ     0ffd8h  ; Timer Source Control
port_tmod       equ     0ffdah  ; Timer Mode
port_trun       equ     0ffdbh  ; Timer/Serial Baud Rate
port_treg5      equ     0ffe2h  ; 16bit Timer <5> [low]
port_t4mod      equ     0ffe4h  ; 16bit Timer Mode
port_t4ffcr     equ     0ffe5h  ; 16bit Timer Control
port_intel      equ     0ffe6h  ; Interrupt Enable Mask [low]
port_inteh      equ     0ffe7h  ; Interrupt Enable Mask [high]
port_dmaeh      equ     0ffe8h  ; Micro DMA Enable Mask [high]
port_scmod      equ     0ffe9h  ; Serial Channel Mode
port_sccr       equ     0ffeah  ; Serial Channel Control
port_scbuf      equ     0ffebh  ; Serial Channel Buffer
port_bx         equ     0ffech  ; Bank Register x
port_by         equ     0ffedh  ; Bank Register y
port_option_x   equ     0fff0h  ; DSR Bit : [Read] --> Active [Low]
port_option_y   equ     0fff1h  ; DRR Bit : [Read] --> Active [Low]
port_option_z   equ     0fff2h  ; CLR DRR : [Read] --> Clear INT0
port_option_d   equ     0fff3h  ; CLR DSR : Initial=[Read]
port_rs232c     equ     0fff8h  ; External UART(1) --> INT1
port_ex_midi    equ     0fffah  ; External UART(2) --> INT2
port_data       equ     0fffch  ; MPU Data Get Port (RD) : INT0
port_command    equ     0fffdh  ; MPU Command Get Port (RD) : INT0
port_send       equ     0fffeh  ; MPU Data Send Port (WR)
port_system     equ     0ffffh  ; System Port (WR) 6bit

;========== MACRO Defines =================================
%define(midi_put)
        local   ex(
        push    hl
        ld      hl,(tx_top)
        ld      de,tx_fifo
        add     hl,de
        ld      (hl),b
        incw    (tx_top)
        bit     4,(tx_top+1)
        jr      z,%ex
        ldw     (tx_top),0
%ex:
        pop     hl
        )
%define(midi_set(memory))(
        ld      b,(%memory)
        %midi_put
        )
%define(echo_machine(ch))
        local   pass off echo note back loop(
        cp      (echo_mode),%ch
        jp      nz,%pass
        ld      a,(echo_end+%ch)
        cp      a,(echo_top+%ch)
        jp      z,_int_1msec_next       ; None Event !
        ld      (echo_counter),a
%loop:
        ld      hl,time_fifo+256*%ch
        dec     (hl+a)
        jr      z,%echo
%back:
        inc     (echo_counter)
        ld      a,(echo_counter)
        cp      a,(echo_top+%ch)
        jp      z,_int_1msec_next
        jr      %loop
%echo:
        inc     (echo_end+%ch)
        ld      hl,mode_fifo+256*%ch
        ld      l,(hl+a)                ; Phrase Number
        ld      e,l
        ld      (event_mode),l
        and     l,01111111b
        mul     hl,45
        ld      bc,percus_bank+5*(%ch+1)
        add     hl,bc
%if(%ch ne 7)then(
        push    hl
        ld      a,2+5
        ld      b,(hl+a)
        ld      a,(echo_top+%ch+1)
        ld      hl,time_fifo+256*(%ch+1)
        ld      (hl+a),b                ; Next Time Set !
        ld      hl,mode_fifo+256*(%ch+1)
        ld      (hl+a),e                ; Next Mode Set !
        inc     (echo_top+%ch+1)
        pop     hl
)fi
        ld      a,3
        bit     7,(hl+a)
        jr      z,%note
        bit     7,(event_mode)
        jp      nz,%back
        ld      b,(hl)
        dec     b
        and     b,00001111b
        or      b,0c0h
        %midi_put
        ld      a,1
        ld      b,(hl+a)
        %midi_put
        jp      %back
%note:
        ld      a,4
        cp      (hl+a),0
        jp      z,%back
        ld      b,(hl)
        dec     b
        and     b,00001111b
        or      b,090h
        %midi_put
        ld      a,3
        ld      b,(hl+a)
        %midi_put
        bit     7,(event_mode)
        jr      nz,%off
        ld      a,4
        ld      b,(hl+a)
        %midi_put
        jp      %back
%off:
        ld      b,0
        %midi_put
        jp      %back
%pass:
        )
%define(get_ad_port)(
        ld      a,(port_iop5)
        )

;========== Head Program Area =============================
        cseg    abs c_area(0)
cold_start:
        di
        ld      sp,0ffc0h
        call    system_initial          ; System Initialize
        jp      main_loop

;========== Interrupt Vector/Sequence =====================
        org     10h                     ; Software Interrupt : SWI
        reti
        org     18h                     ; Non Mascable Interrupt : NMI
        reti
        org     20h                     ; Watch Dog Timer : INTWD
        reti
        org     28h                     ; External Interrupt #0 : INT0
        ld      a,(port_option_z)
        reti
        org     30h                     ; Timer Interrupt #0 : INTT0
        reti
        org     38h                     ; Timer Interrupt #1 : INTT1
        reti
        org     40h                     ; Timer Interrupt #2 : INTT2
        reti
        org     48h                     ; Timer Interrupt #3 : INTT3
        set     int_bit_tim_1,(int_flg)
        reti
        org     50h                     ; Timer Interrupt #4 : INTT4
        reti
        org     58h                     ; External Interrupt #1 : INT1
        reti
        org     60h                     ; Timer Interrupt #5 : INTT5
        reti
        org     68h                     ; External Interrupt #2 : INT2
        reti
        org     70h                     ; UART Receive Interrupt : INTRX
        push    bc
        push    hl
        ld      b,(port_scbuf)
        jr      _int_midi_rx
        org     78h                     ; UART Transmit Interrupt : INTTX
        set     int_bit_midi,(int_flg)
        reti
_int_midi_rx:
        cp      b,0f8h
        jr      z,_int_rx_pass
        ld      hl,rx_fifo
        ld      a,(rx_top)
        ld      (hl+a),b                ; MIDI Receive FIFO Set !
        inc     (rx_top)
_int_rx_pass:
        pop     hl
        pop     bc
        reti

;========== System Initialize Routine =====================
system_initial:
        ld      a,(port_option_x)         ; DSR Clear
        ld      a,(port_option_y)         ; DRR Clear
        ld      a,(port_option_z)         ; INT Clear
        ld      a,(port_option_d)         ; DSR F/F Clear
        ld      (port_option_z),00000000b ;
        ld      (port_option_y),00000000b ;
        ld      (port_option_x),00001000b ; MIDI Gate ON
        ld      (port_wdmod),01110000b  ; Watch Dog OFF
        ld      (port_wdcr),0b1h        ; Watch Dog OFF
        ld      (port_intel),01011111b  ; Interrupt Enable
        ld      (port_inteh),00000101b  ; Interrupt Enable
        ld      (port_dmaeh),00000000b  ; Micro DMA Disable
        ld      (port_p01cr),00000110b  ; Port 0/1 = Data/Address Bus
        ld      (port_p2cr),11111111b   ; Port 2 = Address Bus
        ld      (port_p3cr),10101001b   ; Port 3 = UART
        ld      (port_sccr),00000000b   ; Port 3 = Constant
        ld      (port_p4cr),00001111b   ; Port 4 = Bank Address
        ld      (port_smmod),00000000b  ; Port 6/7 = Port
        ld      (port_p67cr),11111111b  ; Port 6/7 = Output
        ld      (port_p8cr),00001000b   ; Port 8 = Output/Buzzer
        ld      (port_scmod),00101000b  ; UART Control
        ld      (port_trun),10101111b   ; Baud Rate
        ld      (port_tmod),00000100b   ; 8bit Timer Mode
        ld      (port_tclk),10010001b   ; Timer Assign
        ld      (port_t4mod),00000110b  ; 16bit Timer Mode
        ld      (port_t4ffcr),00001011b ; 16bit Timer Control
        ldw     (port_treg0),0ffffh     ; (dummy) Tempo
zzz:
        ld      (port_treg2),2          ; MIDI Divide Rate : 12MHz=[3]
        ld      (port_treg3),187        ; 1msec Interval Constant(1)
        ldw     (port_treg5),017h       ; Start Buzzer Pitch
        ld      (port_bx),00h           ; ROM/RAM Area
        ld      (port_by),08h           ; Debug Bank : Address = 80000-8FFFF
        ld      (port_iop6),0ffh        ; LED Off
        ld      (port_iop7),0ffh        ;
        ld      (port_iop3),11111011b   ; DSP-01 Reset !
        call    wait_timer              ; (wait)
        ld      (port_iop3),11111111b   ; DSP-01 Start !
        call    wait_timer              ; (wait)
        ld      (dsp-01+0f5h),0         ; Memory Bus Close
        nop
        ld      (dsp-01+0f3h),0         ; Memory Bus Close
        ld      hl,04000h
        ld      bc,08000h               ; 4000-BFFF Clear
_ps_ram_clear:
        ld      (hl),0
        inc     hl
        djnz    bc,_ps_ram_clear
        ld      hl,internal_ram         ; Internal RAM Area
        ld      bc,512-16
_int_ram_clear:
        ld      (hl),0
        inc     hl
        djnz    bc,_int_ram_clear
        ld      (select_ctr+0),0
        ld      (select_ctr+1),32
        ld      (end_mark),'*'
        ld      (end_mark+1),'*'
        ld      (end_mark+2),'*'
        %get_ad_port
        ld      b,a
        and     a,00000001b
        ld      (ad_buffer+0),a
        ld      a,b
        and     a,00000010b
        ld      (ad_buffer+1),a
        ld      (port_option_z),00000000b
        ld      (port_option_y),00000000b
        ld      (port_option_x),00001000b
        ld      a,(port_option_z)
        ld      a,(port_scbuf)          ; Dummy Rx
        ld      (port_scbuf),0fch       ; Dummy Tx
        ld      a,(port_option_y)
        ei
        ret
wait_timer:
        ld      bc,0ffffh
_wait_loop:
        nop
        djnz    bc,_wait_loop
        ret

;========== Main Program ==================================
main_loop:
        call    rx_midi_check
        call    tx_midi_check
        call    ad_port_check
        call    int_timer_check
        jr      main_loop

rx_midi_check:;--------------------------------------------
        ld      a,(rx_end)
        cp      a,(rx_top)
        ret     z
        ld      hl,rx_fifo
        ld      b,(hl+a)
        ld      (rx_data),b
        inc     (rx_end)
        bit     7,b
        jr      z,midi_running
        cp      b,0f0h
        ret     nc
        ld      a,b
        ld      (status),a
        and     b,11110000b
        ld      (rsb),b
        and     a,00001111b
        ld      (channel),a
        ld      (dcb),0
        ret
midi_running:
        ld      a,(rsb)
        cp      a,0
        ret     z
        cp      (dcb),0
        jr      nz,_midi_run_2nd
        ld      (data_1st),b
        cp      a,0c0h
        jr      z,midi_prog_change
        ld      (dcb),1
        ret
midi_prog_change:
        %midi_set(status)
        %midi_set(data_1st)
        ret
_midi_run_2nd:
        ld      (data_2nd),b
        ld      (dcb),0
        cp      a,080h
        jr      z,midi_note_off
        cp      a,090h
        jr      z,midi_note_on
        cp      a,0e0h
        jr      z,midi_pitch_bend
        cp      a,0b0h
        jp      z,_midi_run_2nd_ct
        ld      (rsb),0
        ret
midi_note_off:
        ld      a,(status)
        add     a,10h
        ld      (status),a
        ld      (data_2nd),0
midi_note_on:
        %midi_set(status)
        %midi_set(data_1st)
        %midi_set(data_2nd)
        ret
midi_pitch_bend:
        %midi_set(status)
        %midi_set(data_1st)
        %midi_set(data_2nd)
        ret
_midi_run_2nd_ct:
        cp      (data_1st),121
        jr      c,midi_cont_change
        ret
midi_cont_change:
        %midi_set(status)
        %midi_set(data_1st)
        %midi_set(data_2nd)
        ret

tx_midi_check:;--------------------------------------------
        ld      hl,(tx_end)
        cp      hl,(tx_top)             ; FIFO Event ?
        ret     z
        bit     int_bit_midi,(int_flg)  ; TX OK ?
        ret     z
        ld      de,tx_fifo
        add     hl,de
        ld      a,(hl)
        ld      (port_scbuf),a
        res     int_bit_midi,(int_flg)
        incw    (tx_end)
        bit     4,(tx_end+1)
        ret     z
        ldw     (tx_end),0
        ret

ad_port_check:;--------------------------------------------
        inc     (free_timer)
        %get_ad_port
        ld      b,a
        and     a,00000001b
        cp      a,(ad_buffer+0)
        jr      nz,_ad_event_0
        ld      a,b
        and     a,00000010b
        cp      a,(ad_buffer+1)
        jr      nz,_ad_event_1
        ret
_ad_event_0:
        ld      (ad_buffer+0),a
        cp      (note_status+0),0
        jr      nz,_ad_eve_off_0
        cp      (ad_buffer+0),0
        ret     z
        ld      (note_status+0),1       ; Note ON !!
        call    perc_eve_1_on
_ad_eve_off_0:
        ld      (note_ctr+0),0
        ret
_ad_event_1:
        ld      (ad_buffer+1),a
        cp      (note_status+1),0
        jr      nz,_ad_eve_off_1
        cp      (ad_buffer+1),0
        ret     z
        ld      (note_status+1),1       ; Note ON !!
        call    perc_eve_2_on
_ad_eve_off_1:
        ld      (note_ctr+1),0
        ret

int_timer_check:;------------------------------------------
        bit     int_bit_tim_1,(int_flg)
        ret     z
        res     int_bit_tim_1,(int_flg)
        bit     0,(timer_1msec)         ; Odd/Even ?
        jr      z,_int_1msec_even
        ld      (port_treg3),125        ; Constant(1) : 12MHz=[187]
        jr      _int_1msec_cont
_int_1msec_even:
        ld      (port_treg3),125        ; Constant(2) : 12MHz=[188]
_int_1msec_cont:
        inc     (timer_1msec)
        dec     (echo_mode)
        and     (echo_mode),00000111b
        %echo_machine(0)
        %echo_machine(1)
        %echo_machine(2)
        %echo_machine(3)
        %echo_machine(4)
        %echo_machine(5)
        %echo_machine(6)
        %echo_machine(7)
_int_1msec_next:
        cp      (note_status+0),0
        jr      z,_int_eve_off_1
        inc     (note_ctr+0)
        cp      (note_ctr+0),20         ; Gate Time
        ret     nz
        ld      (note_status+0),0       ; Note OFF !!
        ld      a,(select_buf+0)
        or      a,10000000b
        ld      (selected),a
        ld      hl,(point_buff+0)
        call    perc_event_off
        ret
_int_eve_off_1:
        cp      (note_status+1),0
        ret     z
        inc     (note_ctr+1)
        cp      (note_ctr+1),20         ; Gate Time
        ret     nz
        ld      (note_status+1),0       ; Note OFF !!
        ld      a,(select_buf+1)
        or      a,10000000b
        ld      (selected),a
        ld      hl,(point_buff+2)
        call    perc_event_off
        ret

perc_eve_1_on:;--------------------------------------------
        ld      a,(select_ctr+0)        ; Phrase Number
        inc     (select_ctr+0)
        and     (select_ctr+0),00011111b ; Max = 32
        ld      (select_buf+0),a
        ld      (selected),a
        ld      l,45
        mul     hl,a
        ld      bc,percus_bank
        add     hl,bc
        ld      (point_buff+0),hl
        jr      _perc_eve
perc_eve_2_on:
        ld      a,(free_timer)
        and     a,00011111b             ; Max = 32
        add     a,32
        cp      a,(select_ctr+1)
        jr      nz,_perc_2_ok
        add     a,19
        and     a,00011111b
        add     a,32
_perc_2_ok:
        ld      (select_ctr+1),a        ; Phrase Number
        ld      (select_buf+1),a
        ld      (selected),a
        ld      l,45
        mul     hl,a
        ld      bc,percus_bank
        add     hl,bc
        ld      (point_buff+2),hl
_perc_eve:
        ld      a,3
        bit     7,(hl+a)
        jr      z,_eve_note
        ld      b,(hl)
        or      b,0c0h
        %midi_put
        ld      a,1
        ld      b,(hl+a)
        %midi_put
        jp      _eve_joint
_eve_note:
        ld      a,4
        cp      (hl+a),0
        jp      z,_eve_joint
        ld      b,(hl)
        dec     b
        and     b,00001111b
        or      b,090h
        %midi_put
        ld      a,3
        ld      b,(hl+a)
        %midi_put
        ld      a,4
        ld      b,(hl+a)
        %midi_put
        jr      _eve_joint
perc_event_off:
        ld      a,3
        bit     7,(hl+a)
        jr      nz,_eve_joint
        ld      a,4
        cp      (hl+a),0
        jr      z,_eve_joint
        ld      b,(hl)
        dec     b
        and     b,00001111b
        or      b,090h
        %midi_put
        ld      a,3
        ld      b,(hl+a)
        %midi_put
        ld      b,0
        %midi_put
_eve_joint:
        ld      a,2+5
        ld      b,(hl+a)
        ld      a,(echo_top)
        ld      hl,time_fifo
        ld      (hl+a),b
        ld      b,(selected)
        ld    hl,mode_fifo
        ld      (hl+a),b
        inc     (echo_top)
        ret

;========== Data Base Area ================================
;    Format :           Channel / Prog / Time / KeyNo / Speed
;               Master    1-16   0-127    0     0-127   0-127
;               Echo 1    1-16   0-127  1-127   0-127   0-127
;                 |        |      |      |       |       |
;               Echo 8    1-16   0-127  1-127   0-127   0-127
;         --- If KeyNo < 128 , then Prog.Change is ignored.
percus_bank:
;---------------------------------------------------------------
;        CH    Prog    Time     Note     Vel
;---------------------------------------------------------------
    db    4 ,    20 ,     0 ,     60 ,    64      ; Master   NO.0
    db    8 ,    21 ,    48 ,     67 ,    64      ; Echo #1
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #2
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #3
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    6 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    2 ,     4 ,     0 ,     58 ,    64      ; Master   NO.1
    db    5 ,    28 ,    28 ,     60 ,    64      ; Echo #1
    db    9 ,    29 ,    36 ,     67 ,    48      ;      #2
    db    8 ,    30 ,    11 ,    222 ,     0      ;      #3
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    6 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    3 ,    12 ,     0 ,     60 ,    64      ; Master   NO.2
    db    8 ,    30 ,    56 ,     67 ,    48      ; Echo #1
    db    5 ,    28 ,    40 ,     65 ,    72      ;      #2
    db    2 ,     4 ,    24 ,     60 ,    64      ;      #3
    db    4 ,    14 ,    11 ,    222 ,     0      ;      #4
    db    6 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    2 ,     4 ,     0 ,     60 ,    64      ; Master   NO.3
    db    6 ,     5 ,    32 ,     67 ,    48      ; Echo #1
    db    3 ,     6 ,    11 ,    222 ,     0      ;      #2
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #3
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    4 ,    14 ,     0 ,     67 ,    64      ; Master   NO.4
    db    7 ,    13 ,    44 ,     60 ,    56      ; Echo #1
    db    3 ,     6 ,    20 ,     65 ,    64      ;      #2
    db    2 ,    22 ,    11 ,    222 ,     0      ;      #3
    db    6 ,    15 ,    11 ,    222 ,     0      ;      #4
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    2 ,    22 ,     0 ,     67 ,    48       ; Maste   NO.5
    db    8 ,    30 ,    56 ,     60 ,    72      ; Echo #1
    db    3 ,     6 ,    40 ,     67 ,    64      ;      #2
    db    6 ,    15 ,    32 ,     65 ,    48      ;      #3
    db    4 ,    14 ,    48 ,     67 ,    72      ;      #4
    db    9 ,     7 ,    11 ,    222 ,     0      ;      #5
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    3 ,    16 ,     0 ,     67 ,    64      ; Master   NO.6
    db    6 ,    15 ,    24 ,     60 ,    48      ; Echo #1
    db    9 ,     7 ,    48 ,     68 ,    56      ;      #2
    db    4 ,    14 ,    64 ,     65 ,    72      ;      #3
    db    5 ,    23 ,    11 ,    222 ,     0      ;      #4
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    4 ,    14 ,     0 ,     58 ,    64      ; Master   NO.7
    db    9 ,     7 ,    48 ,     60 ,    48      ; Echo #1
    db    5 ,    23 ,    11 ,     65 ,    56      ;      #2
    db    7 ,    13 ,    11 ,     60 ,    64      ;      #3
    db    3 ,     6 ,    11 ,     67 ,    72      ;      #4
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    6 ,    15 ,     0 ,     60 ,    64      ; Master   NO.8
    db    9 ,     7 ,    56 ,     66 ,    64      ; Echo #1
    db    5 ,    13 ,    12 ,     73 ,    72      ;      #2
    db    7 ,    31 ,    11 ,    222 ,     0      ;      #3
    db    3 ,     4 ,    11 ,    222 ,     0      ;      #4
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    8 ,    30 ,     0 ,     73 ,    64      ; Master   NO.9
    db    3 ,     4 ,    40 ,     66 ,    48      ; Echo #1
    db    7 ,    31 ,    80 ,     67 ,    72      ;      #2
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #3
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    6 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    7 ,    31 ,     0 ,     60 ,    64      ; Master   NO.10
    db    3 ,     4 ,    48 ,     60 ,    56      ; Echo #1
    db    4 ,     5 ,    11 ,    222 ,     0      ;      #2
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #3
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    6 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    4 ,     5 ,     0 ,     60 ,    64      ; Master   NO.11
    db    6 ,    15 ,    32 ,     67 ,    56      ; Echo #1
    db    3 ,     4 ,    12 ,     65 ,    72      ;      #2
    db    7 ,    31 ,    80 ,     58 ,    48      ;      #3
    db    9 ,     7 ,    48 ,     66 ,    64      ;      #4
    db    5 ,    23 ,    16 ,     73 ,    72      ;      #5
    db    2 ,    12 ,    11 ,    222 ,     0      ;      #6
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    4 ,    15 ,     0 ,     68 ,    64      ; Master   NO.12
    db    7 ,    31 ,    72 ,     67 ,    56      ; Echo #1
    db    2 ,    12 ,    40 ,     60 ,    72      ;      #2
    db    5 ,    23 ,    16 ,     65 ,    48      ;      #3
    db    8 ,    13 ,    11 ,    222 ,     0      ;      #4
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    6 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    3 ,     4 ,     0 ,     67 ,    64      ; Master   NO.13
    db    9 ,     7 ,    16 ,     60 ,    72      ; Echo #1
    db    5 ,    23 ,    72 ,     67 ,    48      ;      #2
    db    7 ,    31 ,    40 ,     73 ,    56      ;      #3
    db    2 ,    12 ,     8 ,     66 ,    64      ;      #4
    db    8 ,    13 ,    56 ,     62 ,    48      ;      #5
    db    6 ,    20 ,    11 ,    222 ,     0      ;      #6
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    5 ,     4 ,     0 ,     60 ,    64      ; Master   NO.14
    db    2 ,    12 ,    72 ,     58 ,    56      ; Echo #1
    db    8 ,    13 ,    40 ,     67 ,    64      ;      #2
    db    3 ,     4 ,    56 ,     73 ,    72      ;      #3
    db    6 ,    20 ,     8 ,     66 ,    48      ;      #4
    db    7 ,    28 ,    11 ,    222 ,     0      ;      #5
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    2 ,    12 ,     0 ,     60 ,    64      ; Master   NO.15
    db    7 ,    28 ,    72 ,     62 ,    72      ; Echo #1
    db    3 ,     4 ,    48 ,     69 ,    48      ;      #2
    db    9 ,    21 ,    11 ,    222 ,     0      ;      #3
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    6 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    6 ,    20 ,     0 ,     66 ,    64      ; Master   NO.16
    db    9 ,    21 ,    32 ,     73 ,    48      ; Echo #1
    db    4 ,     5 ,    12 ,     67 ,    72      ;      #2
    db    7 ,    28 ,    88 ,     60 ,    56      ;      #3
    db    5 ,     6 ,    11 ,    222 ,     0      ;      #4
    db    3 ,    14 ,    11 ,    222 ,     0      ;      #5
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    5 ,     6 ,     0 ,     62 ,    64      ; Master   NO.17
    db    8 ,    13 ,    56 ,     60 ,    72      ; Echo #1
    db    3 ,    14 ,    80 ,     67 ,    48      ;      #2
    db    6 ,    20 ,    20 ,     69 ,    56      ;      #3
    db    4 ,    29 ,    11 ,    222 ,     0      ;      #4
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    3 ,    14 ,     0 ,     67 ,    64      ; Master   NO.18
    db    8 ,    13 ,    24 ,     68 ,    48      ; Echo #1
    db    4 ,    29 ,    40 ,     75 ,    64      ;      #2
    db    2 ,     7 ,    11 ,    222 ,     0      ;      #3
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    6 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    6 ,    20 ,     0 ,     73 ,    64      ; Master   NO.19
    db    2 ,     7 ,    48 ,     73 ,    64      ; Echo #1
    db    8 ,    13 ,    24 ,     66 ,    72      ;      #2
    db    5 ,     6 ,    40 ,     69 ,    48      ;      #3
    db    3 ,    14 ,    88 ,     62 ,    56      ;      #4
    db    7 ,    15 ,    11 ,    222 ,     0      ;      #5
    db    9 ,    31 ,    11 ,    222 ,     0      ;      #6
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    7 ,    15 ,     0 ,     75 ,    64      ; Master   NO.20
    db    2 ,     7 ,    56 ,     68 ,    56      ; Echo #1
    db    5 ,     6 ,    36 ,     66 ,    48      ;      #2
    db    8 ,    13 ,    32 ,     73 ,    72      ;      #3
    db    6 ,    22 ,    11 ,    222 ,     0      ;      #4
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    8 ,    12 ,     0 ,     75 ,    64      ; Master   NO.21
    db    6 ,    22 ,    24 ,     68 ,    64      ; Echo #1
    db    9 ,    31 ,    64 ,     75 ,    64      ;      #2
    db    5 ,     6 ,    40 ,     62 ,    64      ;      #3
    db    3 ,    14 ,    32 ,     69 ,    64      ;      #4
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    5 ,     6 ,     0 ,     69 ,    64      ; Master   NO.22
    db    7 ,    15 ,    52 ,     62 ,    64      ; Echo #1
    db    4 ,    29 ,    24 ,     68 ,    64      ;      #2
    db    8 ,    13 ,    64 ,     75 ,    64      ;      #3
    db    2 ,     7 ,     8 ,     71 ,    64      ;      #4
    db    6 ,    22 ,    40 ,     64 ,    64      ;      #5
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    5 ,     6 ,     0 ,     58 ,    64      ; Master   NO.23
    db    2 ,     7 ,    48 ,     60 ,    64      ; Echo #1
    db    6 ,    22 ,    16 ,     65 ,    56      ;      #2
    db    8 ,     4 ,    11 ,    222 ,     0      ;      #3
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    9 ,    31 ,     0 ,     67 ,    64       ; Maste   NO.24
    db    5 ,     6 ,    56 ,     60 ,    72      ; Echo #1
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #2
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #3
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    6 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    8 ,     4 ,     0 ,     62 ,    64      ; Master   NO.25
    db    3 ,    14 ,    40 ,     69 ,    48      ; Echo #1
    db    6 ,    22 ,    56 ,     68 ,    72      ;      #2
    db    9 ,    31 ,    36 ,     75 ,    56      ;      #3
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    7 ,    15 ,     0 ,     58 ,    64      ; Master   NO.26
    db    4 ,    29 ,    32 ,     65 ,    72      ; Echo #1
    db    8 ,     4 ,    88 ,     67 ,    64      ;      #2
    db    2 ,    30 ,    11 ,    222 ,     0      ;      #3
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    6 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    9 ,    31 ,     0 ,     71 ,    64      ; Master   NO.27
    db    4 ,    29 ,    44 ,     64 ,    64      ; Echo #1
    db    7 ,    15 ,    52 ,     67 ,    64      ;      #2
    db    2 ,    30 ,    56 ,     60 ,    64      ;      #3
    db    6 ,    13 ,    11 ,    222 ,     0      ;      #4
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    9 ,    31 ,     0 ,     60 ,    64      ; Master   NO.28
    db    6 ,    13 ,    64 ,     64 ,    64      ; Echo #1
    db    2 ,    30 ,    56 ,     71 ,    64      ;      #2
    db    5 ,    23 ,    11 ,    222 ,     0      ;      #3
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    9 ,    31 ,     0 ,     62 ,    72      ; Master   NO.29
    db    4 ,    29 ,    72 ,     60 ,    48      ; Echo #1
    db    6 ,    13 ,    16 ,     67 ,    64      ;      #2
    db    2 ,    30 ,    48 ,     69 ,    72      ;      #3
    db    7 ,    15 ,    56 ,     62 ,    48      ;      #4
    db    3 ,     0 ,    11 ,    222 ,     0      ;      #5
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    2 ,    30 ,     0 ,     60 ,    64      ; Master   NO.30
    db    5 ,    23 ,    32 ,     67 ,    72      ; Echo #1
    db    8 ,     4 ,    12 ,     67 ,    48      ;      #2
    db    3 ,    20 ,    80 ,     60 ,    64      ;      #3
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #4
    db    6 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    7 ,    15 ,     0 ,     71 ,    64      ; Master   NO.31
    db    9 ,    31 ,    56 ,     64 ,    72      ; Echo #1
    db    5 ,    23 ,    96 ,     67 ,    68      ;      #2
    db    2 ,    30 ,    32 ,     60 ,    56      ;      #3
    db    6 ,    13 ,    16 ,     60 ,    64      ;      #4
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #5
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #6
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    2 ,     0 ,     0 ,     67 ,    96      ; Master   NO.32
    db    7 ,     0 ,    12 ,     74 ,    80      ; Echo #1
    db    5 ,     0 ,    24 ,     76 ,   112      ;      #2
    db    9 ,     0 ,    16 ,     69 ,    96      ;      #3
    db    3 ,     0 ,    20 ,     68 ,    64      ;      #4
    db    8 ,     0 ,     8 ,     61 ,    80      ;      #5
    db    6 ,     0 ,    12 ,     65 ,    96      ;      #6
    db    4 ,     0 ,    16 ,     72 ,   112      ;      #7
    db    2 ,     0 ,    24 ,     67 ,    96      ;      #8
;---------------------------------------------------------------
    db    3 ,     0 ,     0 ,     67 ,    96      ; Master   NO.33
    db    8 ,     0 ,    16 ,     67 ,   112      ; Echo #1
    db    4 ,     0 ,    12 ,     74 ,    80      ;      #2
    db    7 ,     0 ,    24 ,     67 ,    64      ;      #3
    db    5 ,     0 ,    16 ,     68 ,    96      ;      #4
    db    2 ,     0 ,     8 ,     61 ,    80      ;      #5
    db    6 ,     0 ,    20 ,     65 ,   112      ;      #6
    db    9 ,     0 ,    12 ,     72 ,    96      ;      #7
    db    3 ,     0 ,     8 ,     74 ,    96      ;      #8
;---------------------------------------------------------------
    db    4 ,     0 ,     0 ,     63 ,    96      ; Master   NO.34
    db    2 ,     0 ,    12 ,     70 ,    96      ; Echo #1
    db    5 ,     0 ,     8 ,     74 ,    80      ;      #2
    db    9 ,     0 ,    16 ,     76 ,   112      ;      #3
    db    6 ,     0 ,    20 ,     67 ,    96      ;      #4
    db    8 ,     0 ,    24 ,     66 ,    64      ;      #5
    db    3 ,     0 ,     8 ,     69 ,   112      ;      #6
    db    7 ,     0 ,    16 ,     59 ,    80      ;      #7
    db    4 ,     0 ,    12 ,     65 ,    96      ;      #8
;---------------------------------------------------------------
    db    5 ,     0 ,     0 ,     72 ,    96      ; Master   NO.35
    db    9 ,     0 ,     8 ,     66 ,    64      ; Echo #1
    db    6 ,     0 ,    24 ,     59 ,    96      ;      #2
    db    4 ,     0 ,    20 ,     65 ,   112      ;      #3
    db    8 ,     0 ,    16 ,     67 ,    80      ;      #4
    db    2 ,     0 ,    20 ,     74 ,    96      ;      #5
    db    7 ,     0 ,     8 ,     63 ,   112      ;      #6
    db    3 ,     0 ,    12 ,     70 ,    80      ;      #7
    db    5 ,     0 ,    16 ,     76 ,    96      ;      #8
;---------------------------------------------------------------
    db    6 ,     0 ,     0 ,     76 ,    96      ; Master   NO.36
    db    8 ,     0 ,    16 ,     69 ,   112      ; Echo #1
    db    2 ,     0 ,    16 ,     67 ,   112      ;      #2
    db    5 ,     0 ,     8 ,     74 ,    80      ;      #3
    db    3 ,     0 ,    20 ,     68 ,    64      ;      #4
    db    7 ,     0 ,    12 ,     61 ,    96      ;      #5
    db    9 ,     0 ,    16 ,     63 ,    96      ;      #6
    db    4 ,     0 ,    24 ,     70 ,    80      ;      #7
    db    6 ,     0 ,     8 ,     76 ,    96      ;      #8
;---------------------------------------------------------------
    db    7 ,     0 ,     0 ,     74 ,    96      ; Master   NO.37
    db    2 ,     0 ,     8 ,     67 ,    96      ; Echo #1
    db    5 ,     0 ,    20 ,     61 ,   112      ;      #2
    db    8 ,     0 ,    12 ,     68 ,    64      ;      #3
    db    6 ,     0 ,     8 ,     70 ,   112      ;      #4
    db    3 ,     0 ,    16 ,     63 ,    80      ;      #5
    db    9 ,     0 ,     8 ,     65 ,    96      ;      #6
    db    4 ,     0 ,    12 ,     72 ,    80      ;      #7
    db    7 ,     0 ,     8 ,     67 ,    96      ;      #8
;---------------------------------------------------------------
    db    8 ,     0 ,     0 ,     76 ,    96      ; Master   NO.38
    db    4 ,     0 ,    12 ,     76 ,    64      ; Echo #1
    db    9 ,     0 ,     8 ,     69 ,    80      ;      #2
    db    5 ,     0 ,    16 ,     67 ,    96      ;      #3
    db    3 ,     0 ,     8 ,     74 ,   112      ;      #4
    db    7 ,     0 ,     8 ,     70 ,   112      ;      #5
    db    2 ,     0 ,    20 ,     63 ,    96      ;      #6
    db    6 ,     0 ,    12 ,     59 ,    96      ;      #7
    db    8 ,     0 ,    16 ,     66 ,    80      ;      #8
;---------------------------------------------------------------
    db    9 ,     0 ,     0 ,     67 ,    96      ; Master   NO.39
    db    7 ,     0 ,     8 ,     74 ,    80      ; Echo #1
    db    3 ,     0 ,    12 ,     69 ,   112      ;      #2
    db    6 ,     0 ,     8 ,     76 ,    96      ;      #3
    db    2 ,     0 ,    16 ,     68 ,    64      ;      #4
    db    5 ,     0 ,     8 ,     61 ,    96      ;      #5
    db    8 ,     0 ,     8 ,     72 ,    96      ;      #6
    db    4 ,     0 ,    12 ,     65 ,    80      ;      #7
    db    9 ,     0 ,    16 ,     67 ,   112      ;      #8
;---------------------------------------------------------------
    db    2 ,     0 ,     0 ,     69 ,    96      ; Master   NO.40
    db    7 ,     0 ,    12 ,     76 ,    80      ; Echo #1
    db    5 ,     0 ,    24 ,     67 ,   112      ;      #2
    db    9 ,     0 ,    16 ,     74 ,    96      ;      #3
    db    3 ,     0 ,    20 ,     68 ,    64      ;      #4
    db    8 ,     0 ,     8 ,     61 ,    80      ;      #5
    db    6 ,     0 ,    12 ,     72 ,    96      ;      #6
    db    4 ,     0 ,    16 ,     65 ,   112      ;      #7
    db    2 ,    40 ,    11 ,    222 ,     0      ;      #8
;---------------------------------------------------------------
    db    3 ,     0 ,     0 ,     76 ,    96      ; Master   NO.41
    db    8 ,     0 ,    16 ,     69 ,   112      ; Echo #1
    db    4 ,     0 ,    12 ,     66 ,    80      ;      #2
    db    7 ,     0 ,    24 ,     59 ,    64      ;      #3
    db    5 ,     0 ,    16 ,     63 ,    96      ;      #4
    db    2 ,     0 ,     8 ,     70 ,    80      ;      #5
    db    6 ,     0 ,    20 ,     72 ,   112      ;      #6
    db    9 ,     0 ,    12 ,     65 ,    96      ;      #7
    db    3 ,    41 ,    11 ,    222 ,     0      ;      #8
;---------------------------------------------------------------
    db    4 ,     0 ,     0 ,     74 ,    96      ; Master   NO.42
    db    2 ,     0 ,    12 ,     67 ,    96      ; Echo #1
    db    5 ,     0 ,     8 ,     68 ,    80      ;      #2
    db    9 ,     0 ,    16 ,     61 ,   112      ;      #3
    db    6 ,     0 ,    20 ,     67 ,    96      ;      #4
    db    8 ,     0 ,    24 ,     74 ,    64      ;      #5
    db    3 ,     0 ,     8 ,     74 ,   112      ;      #6
    db    7 ,     0 ,    16 ,     67 ,    80      ;      #7
    db    4 ,    42 ,    11 ,    222 ,     0      ;      #8
;---------------------------------------------------------------
    db    5 ,     0 ,     0 ,     67 ,    96      ; Master   NO.43
    db    9 ,     0 ,     8 ,     74 ,    64      ; Echo #1
    db    6 ,     0 ,    24 ,     68 ,    96      ;      #2
    db    4 ,     0 ,    20 ,     61 ,   112      ;      #3
    db    8 ,     0 ,    16 ,     69 ,    80      ;      #4
    db    2 ,     0 ,    20 ,     76 ,    96      ;      #5
    db    7 ,     0 ,     8 ,     74 ,   112      ;      #6
    db    3 ,     0 ,    12 ,     67 ,    80      ;      #7
    db    5 ,    43 ,    11 ,    222 ,     0      ;      #8
;---------------------------------------------------------------
    db    6 ,     0 ,     0 ,     72 ,    96      ; Master   NO.44
    db    8 ,     0 ,    16 ,     65 ,   112      ; Echo #1
    db    2 ,     0 ,    16 ,     66 ,   112      ;      #2
    db    5 ,     0 ,     8 ,     59 ,    80      ;      #3
    db    3 ,     0 ,    20 ,     63 ,    64      ;      #4
    db    7 ,     0 ,    12 ,     70 ,    96      ;      #5
    db    9 ,     0 ,    16 ,     74 ,    96      ;      #6
    db    4 ,     0 ,    24 ,     74 ,    80      ;      #7
    db    6 ,    44 ,    11 ,    222 ,     0      ;      #8
;---------------------------------------------------------------
    db    7 ,     0 ,     0 ,     67 ,    96      ; Master   NO.45
    db    2 ,     0 ,     8 ,     69 ,    96      ; Echo #1
    db    5 ,     0 ,    20 ,     76 ,   112      ;      #2
    db    8 ,     0 ,    12 ,     69 ,    64      ;      #3
    db    6 ,     0 ,     8 ,     74 ,   112      ;      #4
    db    3 ,     0 ,    16 ,     67 ,    80      ;      #5
    db    9 ,     0 ,     8 ,     68 ,    96      ;      #6
    db    4 ,     0 ,    12 ,     61 ,    80      ;      #7
    db    7 ,    45 ,    11 ,    222 ,     0      ;      #8
;---------------------------------------------------------------
    db    8 ,     0 ,     0 ,     76 ,    96      ; Master   NO.46
    db    4 ,     0 ,    12 ,     74 ,    64      ; Echo #1
    db    9 ,     0 ,     8 ,     67 ,    80      ;      #2
    db    5 ,     0 ,    16 ,     74 ,    96      ;      #3
    db    3 ,     0 ,     8 ,     74 ,   112      ;      #4
    db    7 ,     0 ,     8 ,     67 ,   112      ;      #5
    db    2 ,     0 ,    20 ,     74 ,    96      ;      #6
    db    6 ,     0 ,    12 ,     74 ,    96      ;      #7
    db    8 ,    46 ,    11 ,    222 ,     0      ;      #8
;---------------------------------------------------------------
    db    9 ,     0 ,     0 ,     67 ,    96      ; Master   NO.47
    db    7 ,     0 ,     8 ,     74 ,    80      ; Echo #1
    db    3 ,     0 ,    12 ,     69 ,   112      ;      #2
    db    6 ,     0 ,     8 ,     76 ,    96      ;      #3
    db    2 ,     0 ,    16 ,     69 ,    64      ;      #4
    db    5 ,     0 ,     8 ,     68 ,    96      ;      #5
    db    8 ,     0 ,     8 ,     61 ,    96      ;      #6
    db    4 ,     0 ,    12 ,     68 ,    80      ;      #7
    db    9 ,    47 ,    11 ,    222 ,     0      ;      #8
;---------------------------------------------------------------
    db    2 ,     0 ,     0 ,     67 ,    96      ; Master   NO.48
    db    7 ,     0 ,    12 ,     69 ,    80      ; Echo #1
    db    5 ,     0 ,    24 ,     76 ,   112      ;      #2
    db    9 ,     0 ,    16 ,     74 ,    96      ;      #3
    db    3 ,     0 ,    20 ,     74 ,    64      ;      #4
    db    8 ,     0 ,     8 ,     68 ,    80      ;      #5
    db    6 ,     0 ,    12 ,     61 ,    96      ;      #6
    db    4 ,    20 ,    11 ,    222 ,     0      ;      #7
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    3 ,     0 ,     0 ,     67 ,    96      ; Master   NO.49
    db    8 ,     0 ,    16 ,     74 ,   112      ; Echo #1
    db    4 ,     0 ,    12 ,     68 ,    80      ;      #2
    db    7 ,     0 ,    24 ,     61 ,    64      ;      #3
    db    5 ,     0 ,    16 ,     72 ,    96      ;      #4
    db    2 ,     0 ,     8 ,     65 ,    80      ;      #5
    db    6 ,     0 ,    20 ,     65 ,   112      ;      #6
    db    9 ,    21 ,    11 ,    222 ,     0      ;      #7
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    4 ,     0 ,     0 ,     72 ,    96      ; Master   NO.50
    db    2 ,     0 ,    12 ,     65 ,    96      ; Echo #1
    db    5 ,     0 ,     8 ,     66 ,    80      ;      #2
    db    9 ,     0 ,    16 ,     66 ,   112      ;      #3
    db    6 ,     0 ,    20 ,     59 ,    96      ;      #4
    db    8 ,     0 ,    24 ,     67 ,    64      ;      #5
    db    3 ,     0 ,     8 ,     74 ,   112      ;      #6
    db    7 ,    22 ,    11 ,    222 ,     0      ;      #7
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    5 ,     0 ,     0 ,     76 ,    96      ; Master   NO.51
    db    9 ,     0 ,     8 ,     76 ,    64      ; Echo #1
    db    6 ,     0 ,    24 ,     74 ,    96      ;      #2
    db    4 ,     0 ,    20 ,     67 ,   112      ;      #3
    db    8 ,     0 ,    16 ,     74 ,    80      ;      #4
    db    2 ,     0 ,    20 ,     67 ,    96      ;      #5
    db    7 ,     0 ,     8 ,     69 ,   112      ;      #6
    db    3 ,    23 ,    11 ,    222 ,     0      ;      #7
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    6 ,     0 ,     0 ,     67 ,    96      ; Master   NO.52
    db    8 ,     0 ,    16 ,     74 ,   112      ; Echo #1
    db    2 ,     0 ,    16 ,     74 ,   112      ;      #2
    db    5 ,     0 ,     8 ,     66 ,    80      ;      #3
    db    3 ,     0 ,    20 ,     59 ,    64      ;      #4
    db    7 ,     0 ,    12 ,     70 ,    96      ;      #5
    db    9 ,     0 ,    16 ,     63 ,    96      ;      #6
    db    6 ,    28 ,    11 ,    222 ,     0      ;      #7
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    7 ,     0 ,     0 ,     74 ,    96      ; Master   NO.53
    db    2 ,     0 ,     8 ,     67 ,    96      ; Echo #1
    db    5 ,     0 ,    20 ,     74 ,   112      ;      #2
    db    9 ,     0 ,    12 ,     76 ,    64      ;      #3
    db    3 ,     0 ,     8 ,     69 ,   112      ;      #4
    db    6 ,     0 ,    16 ,     67 ,    80      ;      #5
    db    4 ,     0 ,     8 ,     67 ,    96      ;      #6
    db    8 ,    29 ,    11 ,    222 ,     0      ;      #7
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    8 ,     0 ,     0 ,     74 ,    96      ; Master   NO.54
    db    4 ,     0 ,    12 ,     67 ,    64      ; Echo #1
    db    9 ,     0 ,     8 ,     74 ,    80      ;      #2
    db    6 ,     0 ,    16 ,     76 ,    96      ;      #3
    db    3 ,     0 ,     8 ,     69 ,   112      ;      #4
    db    7 ,     0 ,     8 ,     63 ,   112      ;      #5
    db    2 ,     0 ,    20 ,     70 ,    96      ;      #6
    db    5 ,    30 ,    11 ,    222 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    9 ,     0 ,     0 ,     67 ,    96      ; Master   NO.55
    db    7 ,     0 ,     8 ,     74 ,    80      ; Echo #1
    db    3 ,     0 ,    12 ,     69 ,   112      ;      #2
    db    6 ,     0 ,     8 ,     76 ,    96      ;      #3
    db    4 ,     0 ,    16 ,     68 ,    64      ;      #4
    db    5 ,     0 ,     8 ,     61 ,    96      ;      #5
    db    8 ,     0 ,     8 ,     68 ,    96      ;      #6
    db    2 ,    31 ,    11 ,    222 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    2 ,     0 ,     0 ,     67 ,    96      ; Master   NO.56
    db    7 ,     0 ,    12 ,     69 ,    80      ; Echo #1
    db    5 ,     0 ,    24 ,     76 ,   112      ;      #2
    db    9 ,     0 ,    16 ,     74 ,    96      ;      #3
    db    3 ,     0 ,    20 ,     68 ,    64      ;      #4
    db    8 ,     0 ,     8 ,     61 ,    80      ;      #5
    db    6 ,    40 ,    11 ,    222 ,     0      ;      #6
    db    4 ,    31 ,    11 ,    222 ,     0      ;      #7
    db    2 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    3 ,     0 ,     0 ,     76 ,    96      ; Master   NO.57
    db    8 ,     0 ,    16 ,     74 ,   112      ; Echo #1
    db    4 ,     0 ,    12 ,     67 ,    80      ;      #2
    db    7 ,     0 ,    24 ,     74 ,    64      ;      #3
    db    5 ,     0 ,    16 ,     67 ,    96      ;      #4
    db    2 ,     0 ,     8 ,     69 ,    80      ;      #5
    db    6 ,    30 ,    11 ,    222 ,     0      ;      #6
    db    9 ,    41 ,    11 ,    222 ,     0      ;      #7
    db    3 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    4 ,     0 ,     0 ,     74 ,    96      ; Master   NO.58
    db    2 ,     0 ,    12 ,     67 ,    96      ; Echo #1
    db    5 ,     0 ,     8 ,     67 ,    80      ;      #2
    db    9 ,     0 ,    16 ,     74 ,   112      ;      #3
    db    6 ,     0 ,    20 ,     74 ,    96      ;      #4
    db    8 ,     0 ,    24 ,     67 ,    64      ;      #5
    db    3 ,    42 ,    11 ,    222 ,     0      ;      #6
    db    7 ,    29 ,    11 ,    222 ,     0      ;      #7
    db    4 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    5 ,     0 ,     0 ,     67 ,    96      ; Master   NO.59
    db    9 ,     0 ,     8 ,     74 ,    64      ; Echo #1
    db    6 ,     0 ,    24 ,     76 ,    96      ;      #2
    db    4 ,     0 ,    20 ,     69 ,   112      ;      #3
    db    8 ,     0 ,    16 ,     63 ,    80      ;      #4
    db    2 ,     0 ,    20 ,     70 ,    96      ;      #5
    db    7 ,    43 ,    11 ,    222 ,     0      ;      #6
    db    3 ,    28 ,    11 ,    222 ,     0      ;      #7
    db    5 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    6 ,     0 ,     0 ,     67 ,    96      ; Master   NO.60
    db    8 ,     0 ,    16 ,     74 ,   112      ; Echo #1
    db    2 ,     0 ,    16 ,     66 ,   112      ;      #2
    db    5 ,     0 ,     8 ,     59 ,    80      ;      #3
    db    3 ,     0 ,    20 ,     70 ,    64      ;      #4
    db    7 ,     0 ,    12 ,     63 ,    96      ;      #5
    db    9 ,    23 ,    11 ,    222 ,     0      ;      #6
    db    4 ,    44 ,    11 ,    222 ,     0      ;      #7
    db    6 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    7 ,     0 ,     0 ,     72 ,    96      ; Master   NO.61
    db    4 ,     0 ,     8 ,     65 ,    96      ; Echo #1
    db    5 ,     0 ,    20 ,     66 ,   112      ;      #2
    db    9 ,     0 ,    12 ,     59 ,    64      ;      #3
    db    6 ,     0 ,     8 ,     67 ,   112      ;      #4
    db    3 ,     0 ,    16 ,     74 ,    80      ;      #5
    db    8 ,    45 ,    11 ,    222 ,     0      ;      #6
    db    2 ,    22 ,    11 ,    222 ,     0      ;      #7
    db    7 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    8 ,     0 ,     0 ,     67 ,    96      ; Master   NO.62
    db    4 ,     0 ,    12 ,     74 ,    64      ; Echo #1
    db    9 ,     0 ,     8 ,     68 ,    80      ;      #2
    db    6 ,     0 ,    16 ,     61 ,    96      ;      #3
    db    3 ,     0 ,     8 ,     72 ,   112      ;      #4
    db    7 ,     0 ,     8 ,     65 ,   112      ;      #5
    db    2 ,    46 ,    11 ,    222 ,     0      ;      #6
    db    5 ,    21 ,    11 ,    222 ,     0      ;      #7
    db    8 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------
    db    9 ,     0 ,     0 ,     74 ,    96      ; Master   NO.63
    db    7 ,     0 ,     8 ,     74 ,    80      ; Echo #1
    db    3 ,     0 ,    12 ,     67 ,   112      ;      #2
    db    6 ,     0 ,     8 ,     68 ,    96      ;      #3
    db    2 ,     0 ,    16 ,     61 ,    64      ;      #4
    db    4 ,     0 ,     8 ,     68 ,    96      ;      #5
    db    8 ,    20 ,    11 ,    222 ,     0      ;      #6
    db    5 ,    47 ,    11 ,    222 ,     0      ;      #7
    db    9 ,     0 ,    11 ,      0 ,     0      ;      #8
;---------------------------------------------------------------

        end

TLCS90アセンブラ その2 中村滋延マシン

;----------------------------------------------------------------:
;							 	 |
;	N A K A M U R A    S P E C I A L    M O D U L E		 |
;								 |
;	   Sound Toy [Prelude-1] Board				 |
;								 |
;----------------------------------------------------------------:

;========== Data Area =====================================
	dseg	abs d_area(04000h)
tone_data_bank: ds	128*10  ; Tone Data Parameter
	dseg	abs b_area(08000h)
tx_fifo:	ds	256*16  ; MIDI TX FIFO (8000-8FFF)
fnum_1_l:	ds	256*16  ; F-Number (L) #1 (9000-9FFF)
fnum_1_m:	ds	256*16  ; F-Number (M) #1 (A000-AFFF)
fnum_1_h:	ds	256*16  ; F-Number (H) #1 (B000-BFFF)
	dseg	abs r_area(0fdc0h)
bank_buff:	ds	16	; offset = DSP-01
dsp_vib_ctr:	ds	16	; offset = DSP-01
dsp_vib_spd:	ds	16	; offset = DSP-01
dsp_vib_add:	ds	16	; offset = DSP-01
dsp_vib_dat:	ds	16	; offset = DSP-01
dsp_vib_flg:	ds	16	; offset = DSP-01
bender:	 	ds	16	; offset = MIDI : Default = [40]
volume:	 	ds	16	; offset = MIDI : Volume Buffer
work:	   	ds	10	; Universal Work Area
calc:	   	ds	12	; Calculator Area
loop_counter:   ds	1	; Job [E] Buffer
play_data_1:	ds	4	; Play Data Buffer (1)
play_data_2:	ds	4	; Play Data Buffer (2)
play_data_3:	ds	4	; Play Data Buffer (3)
play_buffer:	ds	4	; Play Mode Status Buffer
play_trk:	ds	1	; Play Check Track NO.
play_bit:	ds	1	; Play Check Track Bit Map
pedals:	 	ds	16	; Danper=[0] Soft=[1]
	iseg	abs i_area(0ff00h)
assign_ctr:	ds	16	; offset = DSP-01 : [0]=Newest ,[10h]=Empty
assign_note:	ds	16	; offset = DSP-01
assign_ch:	ds	16	; offset = DSP-01 : Output = MIDI CH
assign_pc:	ds	16	; offset = MIDI
tempo:	  	ds	1	; ** Tempo : 8-254
relative_tempo: ds	1	; ** Relative Tempo , 40h = [1:1]
time_base_para: ds	1	; 2=TB:48,3=TB:72,...,8=TB:192
temp:	   	ds	4	; Tempo Caluclate Buffer
mode_flags:	ds	1	; 0 = [Default Status]
int_flg:	ds	1	; Interrupt Check Flags
run_mode:	ds	1	; Record/Play/ME Status Mode Flag
time_number:	ds	3	; Time Base Count Number Data
time_counter:   ds	3	; Current Count Data
play_counter:   ds	4	; Play Counter
active_status:  ds	1	; Active Track Status Buffer
tx_top:		ds	2	;
dsp_tx_end:	ds	2	; DSP-01 Tone Generator TX FIFO End
dsp_channel:	ds	1	; MIDI Channel Buffer
dsp_rsb:	ds	1	; Running Status Buffer
dsp_dcb:	ds	1	; Data Count Buffer
dsp_data_1st:   ds	1	; DSP-01 Received 1 Byte Data Buffer
dsp_data_2nd:   ds	1	; DSP-01 Received 1 Byte Data Buffer
on_pointer:	ds	1	; Assign ON Pointer
ch_buff:	ds	1	; Bend Channel Buffer
vib_timer:	ds	1	; Vibrate Speed Timer
stack_hl:	ds	2	; HL Buffer
led_timer:	ds	4	; LED Display Timer
pointer_1:	ds	2	;
pointer_2:	ds	2	;
pointer_3:	ds	2	;
pointer_4:	ds	2	;
ad_sum:		ds	2	;
ad_flg:		ds	1	;

;========== Port Defines Area =============================
dsp-01	  equ	0fa00h  ; Tone Generator [DSP-01] : FA00-FBFF
internal_ram	equ	0fdc0h  ; Internal RAM Top Address
port_p01cr	equ	0ffc2h  ; Port 0/1 Control
port_p2cr	equ	0ffc5h  ; Port-2 Control
port_iop3	equ	0ffc6h  ; Port-3
port_p3cr	equ	0ffc7h  ; Port-3 Control
port_p4cr	equ	0ffc9h  ; Port-4 Control
port_iop5	equ	0ffcah  ; Port-5
port_smmod	equ	0ffcbh  ; Stepping Motor Mode
port_iop6	equ	0ffcch  ; Port-6
port_iop7	equ	0ffcdh  ; Port-7
port_p67cr	equ	0ffceh  ; Port-6/7 Control
port_iop8	equ	0ffd0h  ; Port-8
port_p8cr	equ	0ffd1h  ; Port-8 Control
port_wdmod	equ	0ffd2h  ; Watch Dog Timer Mode
port_wdcr	equ	0ffd3h  ; Watch Dog Timer Control
port_treg0	equ	0ffd4h  ; 16bit Timer <0/1>
port_treg2	equ	0ffd6h  ; 8bit Timer <2>
port_treg3	equ	0ffd7h  ; 8bit Timer <3>
port_tclk	equ	0ffd8h  ; Timer Source Control
port_tmod	equ	0ffdah  ; Timer Mode
port_trun	equ	0ffdbh  ; Timer/Serial Baud Rate
port_treg5	equ	0ffe2h  ; 16bit Timer <5> [low]
port_t4mod	equ	0ffe4h  ; 16bit Timer Mode
port_t4ffcr	equ	0ffe5h  ; 16bit Timer Control
port_intel	equ	0ffe6h  ; Interrupt Enable Mask [low]
port_inteh	equ	0ffe7h  ; Interrupt Enable Mask [high]
port_dmaeh	equ	0ffe8h  ; Micro DMA Enable Mask [high]
port_scmod	equ	0ffe9h  ; Serial Channel Mode
port_sccr	equ	0ffeah  ; Serial Channel Control
port_scbuf	equ	0ffebh  ; Serial Channel Buffer
port_bx	 equ	0ffech  ; Bank Register x
port_by	 equ	0ffedh  ; Bank Register y
port_adreg	equ	0ffeeh  ; A/D Converter Data Buffer
port_admod	equ	0ffefh  ; A/D Converter Control

;========== Bit Map Defines Area ==========================
int_bit_timer   equ	1			; Time Base
int_bit_tim_1   equ	2			; 1msec
tempo_bit	equ	1	; Port_TRUN	; Tempo Count ON/OFF
real_play	equ	4			; 0 = NOP , 1 = Play
pedal_damper	equ	0	; pedals	; 0 = Off , 1 = On
pedal_soft	equ	1			; 0 = Off , 1 = On

;========== MACRO Defines =================================
%define(dsp-01_system(offset,data))(
	ld	(dsp-01+%offset),%data
	nop
	ld	(dsp-01+0f3h),%data
	)
%define(dsp-01_16ch(offset,data))
	local   loop(
	ldw	(work+1),dsp-01+%offset
	ld	(work+0),%data
	call	@_setting_16
	)
%define(move_3byte(dst,src))(
	ld	a,(%src+0)
	ld	(%dst+0),a
	ld	a,(%src+1)
	ld	(%dst+1),a
	ld	a,(%src+2)
	ld	(%dst+2),a
	)
%define(shift(d1,d2,d3))(
	ldw	(work+0),%d1
	ldw	(work+2),%d2
	ldw	(work+4),%d3
	call	_shift_sub
	)
%define(range(d1,d2))(
	ld	hl,%d1
	ld	de,%d2
	ld	bc,00e80h
	lddr
	)
%define(data_set_16(address,data))
	local   loop(
	ldw	(work+1),%address
	ld	(work+0),%data
	call	@_setting_16
	)

;========== Head Program Area =============================
	cseg	abs c_area(0)
	di
	ld	sp,0ffc0h
	call	system_setup		; System Initialize --> Main Loop
	jp	main_start

;========== Interrupt Vector/Sequence =====================
	org	10h			; Software Interrupt : SWI
	reti
	org	18h			; Non Mascable Interrupt : NMI
	reti
	org	20h			; Watch Dog Timer : INTWD
	reti
	org	28h			; External Interrupt #0 : INT0
	reti
	org	30h			; Timer Interrupt #0 : INTT0
	reti
	org	38h			; Timer Interrupt #1 : INTT1
	push	hl
	ld	hl,(time_counter)
	add	hl,(time_number)
	jr	_int_time_base
	org	40h			; Timer Interrupt #2 : INTT2
	reti
	org	48h			; Timer Interrupt #3 : INTT3
	set	int_bit_tim_1,(int_flg)
	reti
	org	50h			; Timer Interrupt #4 : INTT4
	reti
	org	58h			; External Interrupt #1 : INT1
	reti
	org	60h			; Timer Interrupt #5 : INTT5
	reti
	org	68h			; External Interrupt #2 : INT2
	reti
	org	70h			; UART Receive Interrupt : INTRX
	reti
	org	78h			; UART Transmit Interrupt : INTTX
	reti
_int_time_base:
	ld	(time_counter),hl
	ld	a,(time_counter+2)
	adc	a,(time_number+2)
	ld	(time_counter+2),a
	jr	nc,_int_tb_ret
	set	int_bit_timer,(int_flg) ; Time Counter Event !
_int_tb_ret:
	pop	hl
	reti

;========== System Initialize Routine =====================
system_setup:
;<< DSP-01 Initialize >>
	ld	(port_iop3),11111011b   ; DSP-01 Reset !
	call	@_wait_timer		; (wait)
	ld	(port_iop3),11111111b   ; DSP-01 Start !
	call	@_wait_timer		; (wait)
	%dsp-01_system(0f5h,0)	  	; Memory Bus Close
;<< CPU : TMP91C640 Initialize >>
	ld	(port_wdmod),01110000b  ; Watch Dog OFF
	ld	(port_wdcr),0b1h	; Watch Dog OFF
	ld	(port_intel),01000000b  ; Interrupt Enable
	ld	(port_inteh),00000001b  ; Interrupt Enable
	ld	(port_dmaeh),00000000b  ; Micro DMA Disable
	ld	(port_p01cr),00000110b  ; Port 0/1 = Data/Address Bus
	ld	(port_p2cr),11111111b   ; Port 2 = Address Bus
	ld	(port_p3cr),10101001b   ; Port 3 = UART
	ld	(port_sccr),00000000b   ; Port 3 = Constant
	ld	(port_p4cr),00001111b   ; Port 4 = Bank Address
	ld	(port_smmod),00000000b  ; Port 6/7 = Port
	ld	(port_p67cr),11111111b  ; Port 6/7 = Output
	ld	(port_p8cr),00001000b   ; Port 8 = Output/Buzzer
	ld	(port_scmod),00101000b  ; UART Control
	ld	(port_trun),10101111b   ; Baud Rate
	ld	(port_tmod),00000100b   ; 8bit Timer Mode
	ld	(port_tclk),10010001b   ; Timer Assign
	ld	(port_t4mod),00000110b  ; 16bit Timer Mode
	ld	(port_t4ffcr),00001011b ; 16bit Timer Control
	ldw	(port_treg0),0ffffh	; (dummy) Tempo
	ld	(port_treg2),3	  	; MIDI Divide Rate : 12MHz
	ld	(port_treg3),187	; 1msec Interval Constant(1)
	ld	(port_admod),0f0h	; A/D Wait
	ld	(port_bx),00h	   	; ROM/RAM Area
	ld	(port_by),00h	   	;
	ld	(port_iop6),0ffh	; LED Off
	ld	(port_iop7),0ffh	;
;<< DSP-01 Initialize >>
	%dsp-01_system(0f0h,0)	  	; Random Bit Pattern
	%dsp-01_16ch(000h,0)		; F-NO.(L)
	%dsp-01_16ch(010h,0)		; F-NO.(M)
	%dsp-01_16ch(020h,0)		; F-NO.(H)
	%dsp-01_16ch(030h,0)		; Start Point
	%dsp-01_16ch(040h,0)		; Loop Top
	%dsp-01_16ch(050h,0)		; Loop End
	%dsp-01_16ch(060h,0)		; Bank
	%dsp-01_16ch(070h,0)		; Panpot
	%dsp-01_16ch(0a0h,0f0h)	 	; Attack Level
	%dsp-01_16ch(0b0h,00fh)	 	; Attack Speed
	call	@_dsp-01_all_off	; All Note Off
	%dsp-01_system(0f2h,00101111b)  ; Output Parameter
	%dsp-01_system(0f5h,1)	  	; Memory Bus Open
;<< RAM Area Initialize >>
	ld	hl,internal_ram	 	; Internal RAM Area
	ld	bc,512-16
_int_ram_clear:
	ld	(hl),0
	inc	hl
	djnz	bc,_int_ram_clear
	call	init_tone_set	   	; TONE Module
	call	@_dsp-01_all_off	; Tone Generator All Note Off
	ld	(time_base_para),2	; Time Base = 48
	ld	(relative_tempo),40h	; Relative Tempo = 1:1
	ld	a,(sequence_tempo)
	ld	(tempo),a
	call	@_tempo_setting	 	; BIOS
	call	@_rel_tempo_set	 	; BIOS
	%data_set_16(volume,190)	; MIDI Volume
	%data_set_16(bender,40h)	; MIDI Bender
	%data_set_16(dsp_vib_dat,8)	; Vibrate Default Offset
        ld      (port_admod),11111000b	; A/D Start !
	ei
	ret

;========== Main Loop =====================================
main_start:
	nop
main_loop:
	call	@_dsp-01_rx_chk	 	; TGEN
	call	@_dsp-01_vibrate	; TGEN
	call	@_int_1msec_seq	 	; BIOS
	bit	int_bit_timer,(int_flg)
 	call	nz,@_play_check		; BIOS
	jr	main_loop

@_play_check:
	res	int_bit_timer,(int_flg)
	bit	real_play,(run_mode)	; Playing ?
	ret	z
	cp	(active_status),0
	ret	z
	ld	(play_trk),3		; Track NO.
	ld	(play_bit),00001000b	; Track Bit
_play_chk_loop:
	ld	a,(active_status)
	and	a,(play_bit)		; Active Track ?
	jr	z,_play_chk_next
	ld	hl,play_counter
	ld	a,(play_trk)
	cp	(hl+a),2		; Play_Counter End ?
	jr	c,_play_chk_hit
	dec	(hl+a)		 	; NO --> Decrement Only
	jr	_play_chk_next
_play_chk_hit:
	ld	hl,play_data_1
	ld	b,(hl+a)
	bit	7,b			; 1st Event ?
	jr	z,_play_chk_pass
	ld	a,b
	and	a,11110000b
	cp	a,11110000b
	jr	nz,_play_chk_midi
	cp	b,0fch		  	; Track End ?
	jr	z,_play_chk_end
	jr	_play_chk_recov	 	; F8 or F9
_play_chk_midi:
	ld	c,b			; Status
	ld	a,(play_trk)
	slaa
	slaa
	or	a,b
	ld	b,a
	call	@_tx_midi_set	   	; MIDI Status Tx. Set
	ld	hl,play_data_2
	ld	a,(play_trk)
	ld	b,(hl+a)
	call	@_tx_midi_set	   	; MIDI Key-Num. Tx. Set
	and	c,11100000b
	cp	c,11000000b		; Status = [Cn]/[Dn] ?
	jr	z,_play_chk_pass
	ld	hl,play_data_3
	ld	a,(play_trk)
	ld	b,(hl+a)
	call	@_tx_midi_set	   	; MIDI Velocity Tx. Set
_play_chk_pass:
	call	@_play_request	  	; MPU Next Data Request Sequence
	ld	hl,play_counter
	ld	a,(play_trk)
	cp	(hl+a),0		; Soon Get Next Data ?
	jr	z,_play_chk_hit
_play_chk_next:
	dec	(play_trk)
	srl	(play_bit)
	jr	nc,_play_chk_loop
	ret
_play_chk_recov:
	ld	hl,play_buffer
	ld	a,(play_trk)
	ld	b,(hl+a)
	ld	hl,play_data_1
	ld	(hl+a),b
	jr	_play_chk_pass
_play_chk_end:
	ld	a,(play_bit)
	cpl	a
	and	a,(active_status)
	ld	(active_status),a
	jr	nz,_play_chk_next
	res	real_play,(run_mode)
	ret

;<< NO.[ 11 ] Play Data Request >>
@_play_request:
	ld	b,(play_trk)
	call	@_receive_wait	  ; Wait Check
	ld	hl,play_counter
	ld	a,(play_trk)
	ld	(hl+a),b
	cp	b,0f8h		  ; Timing Overflow ?
	jr	nc,_play_hit_f8
	call	@_receive_wait	  ; Wait Check
	ld	c,b
	and	c,0f0h
	cp	c,0f0h
	jr	nz,_play_wait_cont
	ld	hl,play_data_1
	ld	a,(play_trk)
	ld	c,(hl+a)
	ld	(hl+a),b
	ld	hl,play_buffer
	ld	(hl+a),c
	ret
_play_hit_f8:
	ld	(hl+a),240
	ld	hl,play_data_1
	ld	c,(hl+a)
	ld	(hl+a),0f8h
	ld	hl,play_buffer
	ld	(hl+a),c
	ret
_play_wait_cont:
	bit	7,b
	jr	z,_play_running
	ld	hl,play_data_1
	ld	a,(play_trk)
	ld	(hl+a),b
	call	@_receive_wait	  ; Wait Check
_play_running:
	ld	hl,play_data_2
	ld	a,(play_trk)
	ld	(hl+a),b
	ld	hl,play_data_1
	ld	c,(hl+a)
	and	c,11100000b
	cp	c,11000000b		; Status = [Cn]/[Dn] ?
	ret	z
	call	@_receive_wait	  ; Wait Check
	ld	hl,play_data_3
	ld	a,(play_trk)
	ld	(hl+a),b
	ret

;<< NO.[ 16 ] Play Sequence Data Get Waiting Check : Return = [B] >>
@_receive_wait:
	cp	(play_trk),0
	jr	z,_wait_0
	cp	(play_trk),1
	jr	z,_wait_1
	cp	(play_trk),2
	jr	z,_wait_2
	cp	(play_trk),3
	jr	z,_wait_3
_wait_0:
	ld	hl,(pointer_1)
	ld	b,(hl)
	inc	hl
	ld	(pointer_1),hl
	ret
_wait_1:
	ld	hl,(pointer_2)
	ld	b,(hl)
	inc	hl
	ld	(pointer_2),hl
	ret
_wait_2:
	ld	hl,(pointer_3)
	ld	b,(hl)
	inc	hl
	ld	(pointer_3),hl
	ret
_wait_3:
	ld	hl,(pointer_4)
	ld	b,(hl)
	inc	hl
	ld	(pointer_4),hl
	ret

;<< NO.[ 17 ] Relative Tempo Setting >>
@_rel_tempo_set:
	ld	hl,0100h
	div	hl,(relative_tempo)
	mul	hl,40h
	res	tempo_bit,(port_trun)
	ld	(port_treg0),hl	 ; Timer [A] = Time Base
	set	tempo_bit,(port_trun)
	ret

;<< NO.[ 19 ] 16 Byte Data Setting : [work+0] --> [work+1/2] >>
@_setting_16:
	ld	a,(work)
	ld	hl,(work+1)
	ld	b,16
_set_16:
	ld	(hl),a
	inc	hl
	djnz	_set_16
	ret

;<< NO.[ 20 ] Tempo Setting : Input Data = (tempo) >>
@_tempo_setting:
	ld	l,(time_base_para)
	mul	hl,07dh
	ld	(temp+0),hl
	ld	a,(time_base_para)
	slaa
	slaa
	add	a,(temp+1)
	ld	(temp+2),a
	ld	l,(tempo)
	mul	hl,(temp+0)
	ld	(temp+1),h
	ld	(time_number+0),l
	ld	l,(tempo)
	mul	hl,(temp+2)
	ld	(temp+2),hl
	ld	a,(temp+1)
	add	a,(temp+2)
	ld	(time_number+1),a
	ld	a,(temp+3)
	adc	a,0
	ld	(time_number+2),a
	ret

;<< NO.[ 24 ] MIDI Transmit Data Set : Input = [ B ] >>
@_tx_midi_set:
	push	de
	ld	hl,(tx_top)
	ld	de,tx_fifo
	add	hl,de
	ld	(hl),b
	incw	(tx_top)
	bit	4,(tx_top+1)
	jr	z,_tx_midi_ex
	ldw	(tx_top),0
_tx_midi_ex:
	pop	de
	ret

;<< NO.[ 25 ] Waiting Timer >>
@_wait_timer:
	ld	bc,07fffh
_wait_loop:
	nop
	djnz	bc,_wait_loop
	ret

;<< NO.[ 29 ] 24*24-->48 Multiply : [calc+0/2]*[calc+3/5] --> [calc+6/11] >>
@_24_24_to_48:
	ld	l,(calc+0)
	mul	hl,(calc+3)
	ld	(calc+6),l		; LSB 8bit
	ld	c,h
	ld	b,0
	ld	l,(calc+1)
	mul	hl,(calc+3)
	ld	e,l
	ld	d,0
	ld	(calc+8),h
	ld	l,(calc+0)
	mul	hl,(calc+4)
	ld	(calc+9),h
	ld	h,0
	add	hl,bc
	add	hl,de
	ld	(calc+7),l		; 2nd LSB 8bit
	ld	c,(calc+8)
	ld	b,0
	ld	e,(calc+9)
	ld	d,0
	ld	l,h
	ld	h,0
	add	hl,bc
	add	hl,de
	ld	ix,hl
	ld	l,(calc+2)
	mul	hl,(calc+3)
	ld	c,l
	ld	b,0
	ld	(calc+9),h
	ld	l,(calc+1)
	mul	hl,(calc+4)
	ld	e,l
	ld	d,0
	ld	(calc+10),h
	ld	l,(calc+0)
	mul	hl,(calc+5)
	ld	(calc+11),h
	ld	h,0
	add	hl,ix
	add	hl,bc
	add	hl,de
	ld	(calc+8),l		; 3rd LSB 8bit
	ld	c,(calc+9)
	ld	b,0
	ld	ix,bc
	ld	c,(calc+10)
	ld	b,0
	ld	e,(calc+11)
	ld	d,0
	ld	l,h
	ld	h,0
	add	hl,ix
	add	hl,bc
	add	hl,de
	ld	bc,hl
	ld	l,(calc+2)
	mul	hl,(calc+4)
	ld	e,l
	ld	d,0
	ld	(calc+10),h
	ld	l,(calc+1)
	mul	hl,(calc+5)
	ld	(calc+11),h
	ld	h,0
	add	hl,bc
	add	hl,de
	ld	(calc+9),l		; 3rd MSB 8bit
	ld	c,(calc+10)
	ld	b,0
	ld	e,(calc+11)
	ld	d,0
	ld	l,h
	ld	h,0
	add	hl,bc
	add	hl,de
	ld	bc,hl
	ld	l,(calc+2)
	mul	hl,(calc+5)
	add	hl,bc
	ld	(calc+10),l		; 2nd MSB 8bit
	ld	(calc+11),h		; MSB 8bit
	ret

;<< NO.[ 30 ] 48Bit --> 24Bit Cutting : [calc+6/11] --> [calc+0/2] >>
@_48_cut_to_24:
	sla	(calc+8)
	ld	a,0
	adc	a,(calc+9)
	ld	(calc+0),a
	ld	a,0
	adc	a,(calc+10)
	ld	(calc+1),a
	ld	a,0
	adc	a,(calc+11)
	ld	(calc+2),a
	ret

;<< NO.[ 31 ] 48Bit-->24Bit Cut + 1Bit Shift : [calc+6/11] --> [calc+0/2] >>
@_48_shift_24:
	rl	(calc+8)
	ld	a,(calc+9)
	rla
	ld	(calc+0),a
	ld	a,(calc+10)
	rla
	ld	(calc+1),a
	ld	a,(calc+11)
	rla
	ld	(calc+2),a
	ret

;========== Tone ON Event Routine ========
@_dsp_on_send:
	ld	b,0fah		  ; DSP-01 = [FA**]
	ld	c,e
	or	c,0c0h
	ld	(bc),0f0h
	ld	c,e
	or	c,0d0h
	ld	(bc),00fh
	ld	c,e
	or	c,090h
	ld	(bc),0		  ; OFF
	ld	a,(dsp_channel)
	ld	(ch_buff),a
	ld	hl,assign_note
	ld	a,e
	ld	b,(dsp_data_1st)
	ld	(hl+a),b
	call	@_dsp_on_pitch	  ; ### Common Subroutine ###
	ld	hl,(stack_hl)
	inc	hl
	inc	hl			; 2
	ld	a,(hl)
	ld	c,e
	or	c,040h
	ld	(bc),a
	inc	hl			; 3
	ld	a,(hl)
	ld	c,e
	or	c,050h
	ld	(bc),a
	inc	hl			; 4
	ld	d,(hl)
	push	hl
	ld	hl,bank_buff
	ld	a,e
	ld	(hl+a),d
	ld	c,e
	or	c,060h
	ld	(bc),d
	ld	c,e
	or	c,070h
	ld	(bc),127
	pop	hl
	inc	hl			; 5
	ld	c,(hl)
	push	hl
	ld	hl,volume
	ld	a,(dsp_channel)
	ld	l,(hl+a)
	mul	hl,c
	ld	a,h
	ld	l,(dsp_data_2nd)
	mul	hl,a
	ld	a,h
	ld	hl,_speed_table
	ld	d,(hl+a)
	ld	c,e
	or	c,0a0h
	ld	(bc),d
	pop	hl
	inc	hl			; 6
	ld	a,(hl)
	ld	c,e
	or	c,0b0h
	ld	(bc),a
	inc	hl			; 7
	ld	c,(hl)
	cp	c,0
	jr	z,_on_decay_0
	push	hl
	ld	hl,volume
	ld	a,(dsp_channel)
	ld	l,(hl+a)
	mul	hl,c
	ld	a,h
	ld	l,(dsp_data_2nd)
	mul	hl,a
	ld	a,h
	ld	hl,_speed_table
	ld	a,(hl+a)
	pop	hl
	jr	_on_decay_set
_on_decay_0:
	ld	a,0f0h
_on_decay_set:
	ld	c,e
	or	c,0c0h
	ld	(bc),a
	inc	hl			; 8
	ld	a,(hl)
	ld	c,e
	or	c,0d0h
	ld	(bc),a
	ld	c,e
	or	c,090h
	ld	(bc),3		  ; ON !
	ret
_speed_table:
	db	0f0h,08fh,07fh,06ch,06fh,05ch,05fh,04ch
	db	04fh,038h,039h,03ah,03bh,03ch,03dh,03eh
	db	03fh,028h,029h,02ah,02bh,02ch,02dh,02eh
	db	02fh,02fh,018h,018h,019h,019h,01ah,01ah
	db	01bh,01bh,01ch,01ch,01dh,01dh,01eh,01eh
	db	01fh,01fh,008h,008h,009h,009h,009h,00ah
	db	00ah,00ah,00bh,00bh,00bh,00ch,00ch,00ch
	db	00eh,00eh,00eh,00eh,00fh,00fh,00fh,00fh

;========== OFF Event Routine ========
@_dsp_off_send:
	ld	a,(dsp_channel)
	ld	hl,assign_pc
	ld	l,(hl+a)
	mul	hl,10		   ; Parameter Counts
	add	hl,tone_data_bank+9	; Release
	ld	b,0fah		  ; DSP-01 = [FA**]
	ld	c,e
	or	c,0c0h
	ld	(bc),0f0h
	ld	a,(hl)
	ld	c,e
	or	c,0d0h
	ld	(bc),a
	ld	c,e
	or	c,090h
	ld	(bc),0		  ; OFF !
	ret
@_dsp_off_rhythm:
        ld      b,0fah                  ; DSP-01 = [FA**]
        ld      c,e
        or      c,090h
        ld      (bc),0                  ; OFF
        ret

;========== Common Pitch Write Routine ========
@_dsp_on_pitch:
	ld	hl,assign_pc
	ld	a,(ch_buff)
	ld	l,(hl+a)
	mul	hl,10		   ; Parameter Counts
	add	hl,tone_data_bank	; Top
	ld	(stack_hl),hl
	bit	0,(hl)
	jr	z,_on_shift_next
	sub	b,12
_on_shift_next:
	bit	1,(hl)
	jr	z,_on_shift_end
	sub	b,24
_on_shift_end:
	and	b,7fh
	ld	a,(ch_buff)
	ld	hl,bender
	ld	c,(hl+a)
	ld	hl,dsp_vib_dat
	ld	a,(hl+a)
	add	a,c
	ld	c,a
	sub	b,2
	ld	l,32
	mul	hl,b
	ld	b,0
	add	hl,bc
	ld	c,08h
	add	hl,bc
	or	h,10010000b
	ld	b,0fah		  ; DSP-01 = [FA**]
	ld	a,(hl)
	ld	c,e
	ld	(bc),a
	and	h,00001111b
	or	h,10100000b
	ld	a,(hl)
	or	c,010h
	ld	(bc),a
	or	h,00010000b
	ld	a,(hl)
	ld	c,e
	or	c,020h
	ld	(bc),a
	ld	hl,(stack_hl)
	inc	hl			; 1
	ld	a,(hl)
	ld	c,e
	or	c,030h
	ld	(bc),a
	ret

;========== < MIDI Receive > Check ========
@_dsp-01_rx_chk:
	ld	hl,(dsp_tx_end)
	cp	hl,(tx_top)		; FIFO Event ?
	ret	z
	ld	de,tx_fifo
	add	hl,de
	ld	b,(hl)
	incw	(dsp_tx_end)
	bit	4,(dsp_tx_end+1)
	jr	z,_tx_dsp_nx
	ldw	(dsp_tx_end),0
_tx_dsp_nx:
	bit	7,b
	jr	z,dsp_running
	cp	b,0f8h
	ret	nc			; Realtime = Pass !
	cp	b,0f0h
	jr	c,dsp_status
	ld	(dsp_rsb),0		; [F0]-[F7] = Pass !
	ret
dsp_status:
	ld	a,b
	and	b,11110000b
	ld	(dsp_rsb),b
	and	a,00001111b
	ld	(dsp_channel),a
	ld	(dsp_dcb),0
	ret
dsp_running:
	ld	a,(dsp_rsb)
	cp	a,0
	ret	z
	cp	(dsp_dcb),0
	jr	nz,_dsp_run_2nd
	ld	(dsp_data_1st),b
	cp	a,0c0h
	jp	z,dsp_prog_change
	cp	a,0d0h
	ret	z 
	ld	(dsp_dcb),1
	ret
_dsp_run_2nd:
	ld	(dsp_data_2nd),b
	ld	(dsp_dcb),0
	ld	a,(dsp_rsb)
	cp	a,080h
	jp	z,dsp_note_off
	cp	a,090h
	jp	z,dsp_note_on
	cp	a,0b0h
	jp	z,dsp_cont_change
	cp	a,0e0h
	jp	z,dsp_pitch_bend
	ret
dsp_note_off:
	ld	e,0			; Assign Channel
_assign_off_1:
	ld	hl,assign_ctr
	ld	a,e
	cp	(hl+a),10h
	jr	z,_assign_off_2
	ld	hl,assign_ch
	ld	a,(hl+a)
	cp	a,(dsp_channel)
	jr	nz,_assign_off_2
	ld	hl,assign_note
	ld	a,e
	ld	a,(hl+a)
	cp	a,(dsp_data_1st)
	jr	z,_assign_off_3
_assign_off_2:
	inc	e
	cp	e,10h
	jr	nz,_assign_off_1
	ret
_assign_off_3:
	ld	hl,assign_ctr
	ld	a,e
	ld	b,(hl+a)
	ld	d,0
_assign_off_4:
	ld	a,d
	cp	(hl+a),10h
	jr	z,_assign_off_6
	ld	a,(hl+a)
	cp	a,b
	jr	z,_assign_off_5
	jr	c,_assign_off_6
	ld	a,d
	dec	(hl+a)
	jr	_assign_off_6
_assign_off_5:
	ld	a,e
	ld	(hl+a),10h
_assign_off_6:
	inc	d
	cp	d,10h
	jr	nz,_assign_off_4
	ld	hl,pedals
	ld	a,(dsp_channel)
	bit	pedal_damper,(hl+a)
	ret	nz
	jp	@_dsp_off_send	  ; DSP-01 Trigger
dsp_note_on:
	cp	(dsp_data_2nd),0
	jp	z,dsp_note_off
	ld	hl,pedals
	ld	a,(dsp_channel)
	bit	pedal_soft,(hl+a)
	jr	z,_not_soft_on
	srl	(dsp_data_2nd)
_not_soft_on:
	ld	e,0			; Assign Channel
_assign_on_1:
	ld	hl,assign_ctr
	ld	a,e
	cp	(hl+a),10h
	jr	z,_assign_on_2
	ld	hl,assign_ch
	ld	a,(hl+a)
	cp	a,(dsp_channel)
	jr	nz,_assign_on_2
	ld	hl,assign_note
	ld	a,e
	ld	a,(hl+a)
	cp	a,(dsp_data_1st)
	jr	z,_assign_on_3
_assign_on_2:
	inc	e
	cp	e,10h
	jr	z,_assign_on_next
	jr	_assign_on_1
_assign_on_3:
	call	@_dsp_off_rhythm	; DSP-01 Trigger
	ld	hl,assign_ctr
	ld	a,e
	ld	b,(hl+a)
	ld	d,0
_assign_on_4:
	ld	a,d
	ld	a,(hl+a)
	cp	a,b
	jr	z,_assign_on_5
	jr	nc,_assign_on_6
	ld	a,d
	inc	(hl+a)
	jr	_assign_on_6
_assign_on_5:
	ld	a,e
	ld	(hl+a),0
_assign_on_6:
	inc	d
	cp	d,10h
	jr	nz,_assign_on_4
	jp	@_dsp_on_send	   ; DSP-01 Trigger
_assign_on_next:
	ld	e,(on_pointer)	  ; Assign Channel
	ld	b,e
	inc	(on_pointer)
	and	(on_pointer),00001111b
	ld	hl,assign_ctr
_assign_on_7:
	ld	a,e
	cp	(hl+a),10h
	jr	z,_assign_on_8
	inc	e
	and	e,00001111b
	ld	a,e
	cp	a,b
	jr	nz,_assign_on_7
	jr	_assign_on_kill
_assign_on_8:
	ld	a,0
_assign_on_9:
	cp	(hl+a),10h
	jr	z,_assign_on_10
	inc	(hl+a)
_assign_on_10:
	inc	a
	cp	a,10h
	jr	nz,_assign_on_9
_assign_set_ok:
	ld	a,e
	ld	(hl+a),0
	ld	hl,assign_ch
	ld	b,(dsp_channel)
	ld	(hl+a),b
	ld	hl,assign_note
	ld	b,(dsp_data_1st)
	ld	(hl+a),b
	jp	@_dsp_on_send	   ; DSP-01 Trigger
_assign_on_kill:
	ld	e,0			; Assign Channel
	ld	hl,assign_ctr
_assign_on_11:
	ld	a,e
	cp	(hl+a),0fh
	jr	z,_assign_on_12
	inc	e
	cp	e,10h
	jr	nz,_assign_on_11
	ret
_assign_on_12:
	call	@_dsp_off_rhythm	; DSP-01 Trigger
	ld	hl,assign_ctr
	ld	a,0
_assign_on_13:
	inc	(hl+a)
	inc	a
	cp	a,10h
	jr	nz,_assign_on_13
	jr	_assign_set_ok
dsp_cont_change:
	cp	(dsp_data_1st),7
	jr	z,_dsp_cont_vol
	cp	(dsp_data_1st),64
	jr	z,_dsp_cont_damp
	cp	(dsp_data_1st),67
	jr	z,_dsp_cont_soft
	ret
_dsp_cont_soft:
	ld	hl,pedals
	ld	a,(dsp_channel)
	bit	6,(dsp_data_2nd)
	jr	z,_soft_off
	set	pedal_soft,(hl+a)
	ret
_soft_off:
	res	pedal_soft,(hl+a)
	ret
_dsp_cont_vol:
	ld	hl,volume
	ld	a,(dsp_channel)
	ld	b,(dsp_data_2nd)
	scf
	rl	b
	ld	(hl+a),b
	ret
_dsp_cont_damp:
	ld	hl,pedals
	ld	a,(dsp_channel)
	bit	6,(dsp_data_2nd)
	jr	z,_damper_off
	set	pedal_damper,(hl+a)
	ret
_damper_off:
	res	pedal_damper,(hl+a)
	ld	e,0			; Assign Channel
_damper_off_1:
	ld	hl,assign_ctr
	ld	a,e
	cp	(hl+a),10h
	jr	nz,_damper_off_2
	ld	hl,assign_ch
	ld	a,(hl+a)
	cp	a,(dsp_channel)
	jr	nz,_damper_off_2
	ld	hl,assign_note
	ld	a,e
	ld	a,(hl+a)
	ld	(dsp_data_1st),a
	ld	(dsp_data_2nd),0
	call	@_dsp_off_send	  ; DSP-01 Trigger
_damper_off_2:
	inc	e
	cp	e,10h
	jr	nz,_damper_off_1
	ret
dsp_prog_change:
	ld	hl,assign_pc
	ld	a,(dsp_channel)
	ld	(hl+a),b
	ld	l,b
	mul	hl,10		  ; Parameter Counts
	add	hl,tone_data_bank
	ld	a,(hl)
	ld	b,a
	and	a,00001100b
	srla
	srla
	add	a,2
	ld	c,a
	ld	a,b
	and	a,00110000b
	slla
	slla
	or	a,c
	ld	c,a
	ld	hl,dsp_vib_spd
	ld	a,(dsp_channel)
	ld	(hl+a),c
	ret
dsp_pitch_bend:
	ld	hl,bender
	ld	a,(dsp_channel)
	ld	(hl+a),b
	ld	e,0
_bend_chk:
	ld	hl,assign_ch
	ld	a,e
	ld	a,(hl+a)
	cp	a,(dsp_channel)
	jr	nz,_bend_next
	ld	(ch_buff),a
	ld	hl,assign_note
	ld	a,e
	ld	b,(hl+a)
	call	@_dsp_on_pitch	  ; ### Common Subroutine ###
_bend_next:
	inc	e
	cp	e,10h
	jr	nz,_bend_chk
	ret

;========== All Note Off ========
@_dsp-01_all_off:
	%dsp-01_16ch(0c0h,0f0h)	 ; Decay Level
	%dsp-01_16ch(0d0h,007h)	 ; Decay Speed
	%dsp-01_16ch(090h,0)		; Control Word
	ld	a,0
	ld	hl,assign_ctr
_all_off_loop:
	ld	(hl+a),10h
	inc	a
	cp	a,10h
	jr	nz,_all_off_loop
	ret

;========== Vibrate Routine ========
@_dsp-01_vibrate:
	inc	(vib_timer)
	cp	(vib_timer),3	   ; Speed Parameter
	ret	nz
	ld	(vib_timer),0
	ld	(loop_counter),0
_dsp_vib_loop:
	ld	e,(loop_counter)	; [E] = MIDI Channel
	ld	a,e
	ld	hl,dsp_vib_flg
	ld	(hl+a),0
	ld	hl,dsp_vib_spd
	ld	c,(hl+a)
	ld	b,c
	and	b,00111111b
	ld	hl,dsp_vib_ctr
	inc	(hl+a)
	ld	a,(hl+a)
	cp	a,b
	jr	c,_dsp_vib_next
	ld	a,e
	ld	(hl+a),0
	ld	hl,dsp_vib_add
	inc	(hl+a)
	ld	a,(hl+a)
	and	a,00011111b
	ld	hl,_dsp_vib_table
	and	c,11000000b
	srl	c
	ld	b,0
	add	hl,bc
	ld	c,(hl+a)
	ld	a,e
	ld	hl,dsp_vib_dat
	ld	(hl+a),c
	ld	hl,dsp_vib_flg
	inc	(hl+a)
_dsp_vib_next:
	inc	(loop_counter)
	cp	(loop_counter),10h
	jr	nz,_dsp_vib_loop
	ld	(loop_counter),0
_vib_chk:
	ld	e,(loop_counter)	; [E] = DSP-01 Channel
	ld	hl,assign_ch
	ld	a,e
	ld	a,(hl+a)
	ld	hl,dsp_vib_flg
	cp	(hl+a),0
	jr	z,_vib_next
	ld	(ch_buff),a
	ld	hl,assign_note
	ld	a,e
	ld	b,(hl+a)
	call	@_dsp_on_pitch	  ; ### Common Subroutine ###
_vib_next:
	inc	(loop_counter)
	cp	(loop_counter),10h
	jr	nz,_vib_chk
	ret
_dsp_vib_table:
	db	8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
	db	8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8	 ; Depth = [00]
	db	8,8,8,9,9,9,9,9,10,9,9,9,9,9,8,8
	db	8,8,8,7,7,7,7,7,6,7,7,7,7,7,8,8	 ; Depth = [01]
	db	8,9,10,10,11,11,11,12,12,12,11,11,11,10,10,9
	db	8,7,6,6,5,5,5,4,4,4,5,5,5,6,6,7	 ; Depth = [10]
	db	8,9,10,11,12,13,13,14,14,14,13,13,12,11,10,9
	db	8,7,6,5,4,3,3,2,2,2,3,3,4,5,6,7	 ; Depth = [11]

;========== Pitch Setting Subroutines ======================
shift_set:
	ldw	(work+6),0b000h
	%shift(0be80h,0ae80h,09e80h)
	%shift(0bd00h,0ad00h,09d00h)
	%shift(0bb80h,0ab80h,09b80h)
	%shift(0ba00h,0aa00h,09a00h)
	%shift(0b880h,0a880h,09880h)
	%shift(0b700h,0a700h,09700h)
	%shift(0b580h,0a580h,09580h)
	%shift(0b400h,0a400h,09400h)
	%shift(0b280h,0a280h,09280h)
	%shift(0b100h,0a100h,09100h)
	ret
_shift_sub:
	ld	hl,(work+0)
	ld	de,(work+2)
	ld	bc,(work+4)
_shift_loop:
	dec	hl
	dec	de
	dec	bc
	srl	(hl)
	rr	(de)
	rr	(bc)
	cp	hl,(work+6)
	jr	nz,_shift_loop
	ret
all_range:
	%range(0bfffh,0be7fh)
	%range(0afffh,0ae7fh)
	%range(09fffh,09e7fh)
	ret
bend_compute:
	ld	(work+0),0
_bend_set_loop:
	ld	(work+1),090h
	ld	(work+2),0a0h
	ld	(work+3),0b0h
	call	_bend_sub		; Each Bend Set
	inc	(work+0)
	cp	(work+0),12
	jr	nz,_bend_set_loop
	ret
_bend_sub:
	ld	a,(work+0)
	ld	hl,_low_offset_2
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+1)
	or	a,b
	ld	b,a
	ld	a,(bc)
	ld	(work+5),a
	ld	a,(work+0)
	ld	hl,_low_offset_2
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+2)
	or	a,b
	ld	b,a
	ld	a,(bc)
	ld	(work+6),a
	ld	a,(work+0)
	ld	hl,_low_offset_2
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+3)
	or	a,b
	ld	b,a
	ld	a,(bc)
	ld	(work+7),a		; Normal Pitch Data Get !
	ld	(work+4),0		; Bend Depth : 0-31
_bend_sub_loop:
	call	_bend_sub_sub
	inc	(work+4)
	cp	(work+4),32
	jr	nz,_bend_sub_loop
	ret
_bend_sub_sub:
	%move_3byte(calc+0,work+5)
	ld	l,(work+4)		; Bend Depth
	mul	hl,3
	ld	bc,_bend_table
	add	hl,bc
	ld	a,(hl)
	ld	(calc+5),a
	inc	hl
	ld	a,(hl)
	ld	(calc+4),a
	inc	hl
	ld	a,(hl)
	ld	(calc+3),a		; Bend Multi. Data Set !
	call	@_24_24_to_48	   ; BIOS
	call	@_48_shift_24	   ; BIOS
	ld	a,(work+0)
	ld	hl,_low_offset_3
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+1)
	or	a,b
	ld	b,a
	ld	l,(work+4)		; Bend Depth
	ld	h,0
	add	hl,bc
	ld	a,(calc+0)
	ld	(hl),a
	ld	a,(work+0)
	ld	hl,_low_offset_3
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+2)
	or	a,b
	ld	b,a
	ld	l,(work+4)		; Bend Depth
	ld	h,0
	add	hl,bc
	ld	a,(calc+1)
	ld	(hl),a
	ld	a,(work+0)
	ld	hl,_low_offset_3
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+3)
	or	a,b
	ld	b,a
	ld	l,(work+4)		; Bend Depth
	ld	h,0
	add	hl,bc
	ld	a,(calc+2)
	ld	(hl),a		  ; Pitch Data Set !
	ret
temp_compute:
	ld	(work+0),0		; Check Note [C-B]
	ld	(work+4),0		; Temp. Offset Bias
	ld	(work+5),0		; Temp Select NO.
	call	_temp_select
_temp_set_loop:
	ld	(work+1),090h
	ld	(work+2),0a0h
	ld	(work+3),0b0h
	call	_temp_sub		; Each Temp. Set
	inc	(work+0)
	cp	(work+0),12
	jr	nz,_temp_set_loop
	ret
_temp_select:
	ld	hl,_temp_sel_tb
	ld	a,(work+5)
	ld	c,(hl+a)
	bit	7,c
	jr	nz,_temp_sel_cont
	ld	(work+5),c
	ret
_temp_sel_cont:
	ld	b,c
	and	b,11110000b		; 80=Pytha/C0=Mean/E0=Q*/F0=T*
	and	c,00001111b		; Shift Data
	cp	b,80h
	jr	z,_temp_sel_80
	cp	b,0c0h
	jr	z,_temp_sel_c0
	cp	b,0e0h
	jr	z,_temp_sel_e0
	ld	(work+5),22		; Pure [T-*] Temp.
	jr	_temp_sel_next
_temp_sel_80:
	ld	(work+5),1		; Pythagorean Temp.
	jr	_temp_sel_next
_temp_sel_c0:
	ld	(work+5),2		; Meantone Temp.
	jr	_temp_sel_next
_temp_sel_e0:
	ld	(work+5),21		; Pure [Q-*] Temp.
_temp_sel_next:
	ld	a,(work+4)		; Temp Bias
	add	a,c
	cp	a,12
	jr	c,_temp_sel_exit
	sub	a,12
_temp_sel_exit:
	ld	(work+4),a
	ret
_temp_sub:
	ld	a,(work+0)
	ld	hl,_low_offset_2
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+1)
	or	a,b
	ld	b,a
	ld	a,(bc)
	ld	(calc+0),a
	ld	a,(work+0)
	ld	hl,_low_offset_2
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+2)
	or	a,b
	ld	b,a
	ld	a,(bc)
	ld	(calc+1),a
	ld	a,(work+0)
	ld	hl,_low_offset_2
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+3)
	or	a,b
	ld	b,a
	ld	a,(bc)
	ld	(calc+2),a		; Current Pitch Data Get !
	ld	hl,_temp_offset
	ld	a,(work+0)		; My Note
	ld	c,(hl+a)
	ld	b,0
	add	hl,bc
	ld	a,(work+4)		; Temp Offset Bias
	ld	c,(hl+a)
	ld	l,3
	mul	hl,c
	ld	bc,hl		   ; Temp Data Note Offset
	ld	l,(work+5)		; Temp Select NO.
	mul	hl,36
	ld	de,hl		   ; Temp Select Offset
	ld	hl,_temp_table
	add	hl,bc
	add	hl,de
	ld	a,(hl)
	ld	(calc+5),a
	inc	hl
	ld	a,(hl)
	ld	(calc+4),a
	inc	hl
	ld	a,(hl)
	ld	(calc+3),a		; Temp Multi. Data Set !
	call	@_24_24_to_48	   ; BIOS
	call	@_48_cut_to_24	  ; BIOS
	ld	a,(work+0)
	ld	hl,_low_offset_2
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+1)
	or	a,b
	ld	b,a
	ld	a,(calc+0)
	ld	(bc),a
	ld	a,(work+0)
	ld	hl,_low_offset_2
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+2)
	or	a,b
	ld	b,a
	ld	a,(calc+1)
	ld	(bc),a
	ld	a,(work+0)
	ld	hl,_low_offset_2
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+3)
	or	a,b
	ld	b,a
	ld	a,(calc+2)
	ld	(bc),a		  ; Pitch Data Set !
	ret
note_devide:
	ld	(work+0),1
_note_dev_loop:
	ld	(work+1),090h
	ld	(work+2),0a0h
	ld	(work+3),0b0h
	call	_note_sub		; Each Notes Set
	inc	(work+0)
	cp	(work+0),12
	jr	nz,_note_dev_loop
	ret
_note_sub:
	ld	hl,_low_offset_1
	ld	c,(hl)
	ld	hl,_high_offset_1
	ld	b,(hl)
	ld	a,(work+1)
	or	a,b
	ld	b,a
	ld	a,(bc)
	ld	(calc+0),a
	ld	hl,_low_offset_1
	ld	c,(hl)
	ld	hl,_high_offset_1
	ld	b,(hl)
	ld	a,(work+2)
	or	a,b
	ld	b,a
	ld	a,(bc)
	ld	(calc+1),a
	ld	hl,_low_offset_1
	ld	c,(hl)
	ld	hl,_high_offset_1
	ld	b,(hl)
	ld	a,(work+3)
	or	a,b
	ld	b,a
	ld	a,(bc)
	ld	(calc+2),a		; Standard Pitch Data Get !
	%move_3byte(calc+3,_dev_data)
	ld	(work+4),0
_note_sub_loop:
	call	@_24_24_to_48	   ; BIOS
	call	@_48_cut_to_24	  ; BIOS
	inc	(work+4)
	ld	a,(work+4)
	cp	a,(work+0)
	jr	nz,_note_sub_loop
	ld	a,(work+0)
	ld	hl,_low_offset_1
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+1)
	or	a,b
	ld	b,a
	ld	a,(calc+0)
	ld	(bc),a
	ld	a,(work+0)
	ld	hl,_low_offset_1
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+2)
	or	a,b
	ld	b,a
	ld	a,(calc+1)
	ld	(bc),a
	ld	a,(work+0)
	ld	hl,_low_offset_1
	ld	c,(hl+a)
	ld	hl,_high_offset_1
	ld	b,(hl+a)
	ld	a,(work+3)
	or	a,b
	ld	b,a
	ld	a,(calc+2)
	ld	(bc),a		  ; Pitch Data Set !
	ret
pitch_shift:
	ld	(work+0),020h
	ld	(work+1),090h
	ld	(work+2),0a0h
	ld	(work+3),0b0h
	call	_pitch_sub		; [G 9] Set
	ret
_pitch_sub:
	ld	a,(work+0)
	and	a,00111111b
	ld	l,3
	mul	hl,a
	ld	bc,_hz_set_table
	add	hl,bc
	ld	a,(hl)
	ld	(calc+2),a
	inc	hl
	ld	a,(hl)
	ld	(calc+1),a
	inc	hl
	ld	a,(hl)
	ld	(calc+0),a
	%move_3byte(calc+3,_g9_data)
	call	@_24_24_to_48	   ; BIOS
	call	@_48_cut_to_24	  ; BIOS
	ld	hl,_low_offset_1
	ld	c,(hl)
	ld	hl,_high_offset_1
	ld	b,(hl)
	ld	a,(work+1)
	or	a,b
	ld	b,a
	ld	a,(calc+0)
	ld	(bc),a
	ld	hl,_low_offset_1
	ld	c,(hl)
	ld	hl,_high_offset_1
	ld	b,(hl)
	ld	a,(work+2)
	or	a,b
	ld	b,a
	ld	a,(calc+1)
	ld	(bc),a
	ld	hl,_low_offset_1
	ld	c,(hl)
	ld	hl,_high_offset_1
	ld	b,(hl)
	ld	a,(work+3)
	or	a,b
	ld	b,a
	ld	a,(calc+2)
	ld	(bc),a		  ; [G9] Pitch Data Set !
	ret
_low_offset_3:
	db	00h,20h,40h,60h,80h,0a0h,0c0h,0e0h,80h,0a0h,0c0h,0e0h
_low_offset_2:
	db	10h,30h,50h,70h,90h,0b0h,0d0h,0f0h,90h,0b0h,0d0h,0f0h
_high_offset_1:
	db	0fh,0fh,0fh,0fh,0fh,0fh,0fh,0fh,0eh,0eh,0eh,0eh
_low_offset_1:
	db	0f0h,0d0h,0b0h,90h,70h,50h,30h,10h,0f0h,0d0h,0b0h,90h
_temp_sel_tb:
	db	0,0
_temp_offset:
	db	11,10,9,8,7,6,5,4,3,2,1,0,11,10,9,8,7,6,5,4,3,2,1
_g9_data:
	db	0bch,084h,0cdh
_dev_data:
	db	0bfh,0a1h,0f1h
_hz_set_table:
  db  076h,0B0h,0DFh,076h,0FBh,058h,077h,045h,0D1h,077h,090h,04Ah ; 408-411(Hz)
  db  077h,0DAh,0C3h,078h,025h,03Ch,078h,06Fh,0B5h,078h,0BAh,02Eh ; 412-415
  db  079h,004h,0A7h,079h,04Fh,020h,079h,099h,099h,079h,0E4h,012h ; 416-419
  db  07Ah,02Eh,08Bh,07Ah,079h,004h,07Ah,0C3h,07Dh,07Bh,00Dh,0F6h ; 420-423
  db  07Bh,058h,06Fh,07Bh,0A2h,0E8h,07Bh,0EDh,061h,07Ch,037h,0DAh ; 424-427
  db  07Ch,082h,053h,07Ch,0CCh,0CCh,07Dh,017h,045h,07Dh,061h,0BEh ; 428-431
  db  07Dh,0ACh,037h,07Dh,0F6h,0B0h,07Eh,041h,029h,07Eh,08Bh,0A2h ; 432-435
  db  07Eh,0D6h,01Bh,07Fh,020h,094h,07Fh,06Bh,00Dh,07Fh,0B5h,086h ; 436-439
  db  080h,000h,000h,080h,04Ah,079h,080h,094h,0F2h,080h,0DFh,06Bh ; 440-443
  db  081h,029h,0E4h,081h,074h,05Dh,081h,0BEh,0D6h,082h,009h,04Fh ; 444-447
  db  082h,053h,0C8h,082h,09Eh,041h,082h,0E8h,0BAh,083h,033h,033h ; 448-451
  db  083h,07Dh,0ACh,083h,0C8h,025h,084h,012h,09Eh,084h,05Dh,017h ; 452-455
  db  084h,0A7h,090h,084h,0F2h,009h,085h,03Ch,082h,085h,086h,0FBh ; 456-459
  db  085h,0D1h,074h,086h,01Bh,0EDh,086h,066h,066h,086h,0B0h,0DFh ; 460-463
  db  086h,0FBh,058h,087h,045h,0D1h,087h,090h,04Ah,087h,0DAh,0C3h ; 464-467
  db  088h,025h,03Ch,088h,06Fh,0B5h,088h,0BAh,02Eh,089h,004h,0A7h ; 468-471
_bend_table:
  db  07Ch,05Bh,028h,07Ch,094h,0ACh,07Ch,0CEh,04Bh,07Dh,008h,005h ; -16/-13
  db  07Dh,041h,0D9h,07Dh,07Bh,0C8h,07Dh,0B5h,0D2h,07Dh,0EFh,0F6h ; -12/-9
  db  07Eh,02Ah,036h,07Eh,064h,090h,07Eh,09Fh,006h,07Eh,0D9h,096h ;  -8/-5
  db  07Fh,014h,042h,07Fh,04Fh,008h,07Fh,089h,0EAh,07Fh,0C4h,0E7h ;  -4/-1
  db  080h,000h,000h,080h,03Bh,033h,080h,076h,082h,080h,0B1h,0EDh ;   0/+3
  db  080h,0EDh,073h,081h,029h,014h,081h,064h,0D1h,081h,0A0h,0AAh ;  +4/+7
  db  081h,0DCh,09Fh,082h,018h,0AFh,082h,054h,0DBh,082h,091h,022h ;  +8/+11
  db  082h,0CDh,086h,083h,00Ah,006h,083h,046h,0A1h,083h,083h,059h ; +12/+15
_temp_table:
;*** Temp [0] 12-Equal
	db	080h,000h,000h,080h,000h,000h,080h,000h,000h,080h,000h,000h
	db	080h,000h,000h,080h,000h,000h,080h,000h,000h,080h,000h,000h
	db	080h,000h,000h,080h,000h,000h,080h,000h,000h,080h,000h,000h

;========== Initial Settings =============================
;<< Initial Tone Data Setting >>
init_tone_set:
	call	pitch_shift		; [G9] 408Hz-471Hz Standard Pitch
	call	note_devide		; Other 11-Notes Devide
	call	temp_compute		; Temperaments Computing
	call	bend_compute		; Bend [-16/+15] Computing
	call	all_range		; All Range Data Only Copy
	call	shift_set		; Each Range Data Shift
	ld	ix,0
	ld	iy,0
	ld	bc,128*10
	ld	hl,default_tone
	ld	de,tone_data_bank
	ldir
	ld	(work),0
_tone_loop_1:
	ld	hl,init_tone_table
	ld	a,(work)
	ld	b,(hl+a)
	ld	hl,assign_pc
	ld	(hl+a),b
	ld	(dsp_channel),a
	call	dsp_prog_change
	inc	(work)
	cp	(work),16
	jr	nz,_tone_loop_1
	ret
init_tone_table:
	db	0,68,48,95,78,41,3,110,122,0	; 0-8
	db	70,34,7,11,62,89		; 10-15

;========== Defaule Tone Data Area ========================

default_tone:
	db	001h,000h,047h,047h,0C0h,06Fh,00Fh,000h,0C8h,099h
	db	001h,048h,08Fh,08Fh,0C0h,07Fh,00Fh,000h,0BBh,099h
	db	001h,0B0h,0EFh,0EFh,0C2h,07Fh,00Fh,000h,0A8h,098h
	db	001h,090h,0AFh,0AFh,0C0h,064h,02Fh,000h,0C9h,098h
	db	001h,0AFh,0AFh,0AFh,0C0h,07Fh,018h,000h,0C8h,098h
	db	001h,0B0h,0CFh,0CFh,0C0h,064h,00Fh,000h,0CAh,098h
	db	009h,0CFh,0CFh,0CFh,0C0h,07Fh,018h,000h,0CAh,098h
	db	001h,0E8h,0FEh,0FEh,0C0h,073h,00Fh,000h,0CFh,088h
	db	001h,000h,000h,000h,0C5h,052h,03Fh,000h,0FFh,04Fh
	db	001h,001h,001h,001h,0C5h,052h,03Fh,000h,0FFh,04Fh
	db	001h,002h,002h,002h,0C5h,064h,03Fh,000h,0FFh,04Fh
	db	001h,003h,003h,003h,0C5h,064h,03Fh,000h,0FFh,04Fh
	db	001h,004h,004h,004h,0C5h,038h,068h,038h,08Ah,098h
	db	001h,005h,005h,005h,0C5h,04Fh,07Ah,04Fh,08Ah,098h
	db	001h,006h,006h,006h,0C5h,04Fh,07Ah,048h,08Ah,098h
	db	015h,028h,03Fh,03Fh,0C1h,054h,06Fh,05Ah,0AAh,065h
	db	001h,000h,01Fh,01Fh,0C2h,07Fh,00Fh,000h,0CFh,088h
	db	00Dh,01Fh,01Fh,01Fh,0C2h,07Fh,00Fh,000h,0B8h,088h
	db	000h,01Dh,01Dh,01Dh,0C5h,07Fh,00Fh,000h,0B8h,088h
	db	000h,01Fh,01Fh,01Fh,0C5h,07Fh,00Fh,000h,0BFh,068h
	db	000h,018h,018h,018h,0C5h,07Fh,00Fh,000h,0BFh,06Bh
	db	004h,01Bh,01Bh,01Bh,0C5h,07Fh,00Fh,000h,0A8h,066h
	db	000h,02Dh,02Dh,02Dh,0C5h,055h,00Fh,000h,0A8h,098h
	db	01Ch,02Eh,02Eh,02Eh,0C5h,055h,00Fh,000h,0A8h,098h
	db	01Ch,030h,03Fh,03Fh,0C3h,05Fh,04Fh,03Fh,0AAh,088h
	db	01Dh,0A8h,0BFh,0BFh,0C4h,048h,00Fh,05Ah,088h,088h
	db	01Dh,0C0h,0C0h,0D7h,0C3h,06Fh,03Fh,000h,0F8h,088h
	db	01Dh,088h,088h,0A7h,0C3h,055h,04Fh,000h,0F8h,088h
	db	002h,090h,09Fh,09Fh,0C5h,07Fh,018h,038h,0CFh,088h
	db	001h,03Fh,03Fh,03Fh,0C3h,06Fh,008h,000h,0DAh,078h
	db	001h,09Fh,09Fh,09Fh,0C5h,07Fh,00Fh,000h,0C8h,078h
	db	000h,01Ah,01Ah,01Ah,0C5h,064h,01Fh,000h,0C8h,08Fh
	db	01Ah,040h,057h,057h,0C1h,07Fh,068h,048h,0A9h,098h
	db	01Dh,058h,06Fh,06Fh,0C1h,064h,088h,03Fh,0EEh,098h
	db	01Eh,070h,070h,09Fh,0C4h,078h,06Fh,048h,0ADh,098h
	db	07Dh,060h,067h,067h,0C3h,058h,06Fh,000h,0DDh,0A8h
	db	0B9h,070h,070h,0AFh,0C2h,04Fh,06Fh,048h,0DFh,0AFh
	db	01Ch,025h,025h,025h,0C5h,05Fh,03Fh,000h,0B8h,0A8h
	db	018h,015h,015h,015h,0C5h,06Fh,018h,000h,0B8h,0A8h
	db	018h,007h,007h,007h,0C5h,064h,058h,03Eh,0ADh,09Fh
	db	0DDh,008h,008h,008h,0C5h,06Fh,02Fh,000h,0BFh,0BFh
	db	07Ch,009h,009h,009h,0C5h,064h,018h,000h,0C8h,0A8h
	db	05Ch,00Ah,00Ah,00Ah,0C5h,048h,048h,038h,0DDh,08Ch
	db	054h,00Bh,00Bh,00Bh,0C5h,04Fh,048h,048h,0A8h,0A8h
	db	01Dh,0A8h,0BFh,0BFh,0C4h,04Fh,05Fh,058h,0A8h,09Fh
	db	019h,000h,000h,000h,0C5h,028h,018h,04Fh,088h,08Dh
	db	085h,014h,014h,014h,0C5h,07Fh,02Fh,000h,0A9h,0A9h
	db	01Ch,00Ch,00Ch,00Ch,0C5h,044h,018h,038h,0C8h,078h
	db	001h,070h,070h,0AFh,0C2h,048h,018h,04Fh,0BFh,0AFh
	db	01Dh,0B0h,0CFh,0CFh,0C1h,06Fh,048h,000h,0CFh,0AFh
	db	01Dh,0CFh,0CFh,0CFh,0C1h,064h,078h,038h,0CFh,0AFh
	db	000h,040h,04Fh,04Fh,0C3h,05Fh,02Fh,000h,088h,088h
	db	019h,010h,01Fh,01Fh,0C4h,05Fh,058h,048h,0B8h,088h
	db	01Ch,021h,021h,021h,0C5h,058h,078h,048h,0B8h,088h
	db	01Dh,0A0h,0A7h,0A7h,0C4h,07Fh,078h,038h,099h,088h
	db	018h,022h,022h,022h,0C5h,064h,09Fh,048h,099h,088h
	db	001h,040h,04Fh,04Fh,0C3h,054h,048h,000h,0DFh,088h
	db	018h,025h,025h,025h,0C5h,058h,018h,000h,0B8h,0AAh
	db	000h,025h,025h,025h,0C5h,06Fh,02Eh,000h,0ACh,0ACh
	db	008h,00Dh,00Dh,00Dh,0C5h,064h,01Fh,000h,0B8h,088h
	db	008h,00Eh,00Eh,00Eh,0C5h,05Fh,00Fh,000h,0B8h,088h
	db	001h,0D0h,0E7h,0E7h,0C0h,06Fh,018h,000h,0DFh,088h
	db	008h,019h,019h,019h,0C5h,06Fh,01Fh,000h,0D8h,088h
	db	055h,078h,08Fh,08Fh,0C5h,06Fh,04Fh,000h,0C8h,098h
	db	001h,000h,00Fh,00Fh,0C3h,06Fh,00Fh,000h,0C8h,088h
	db	001h,00Fh,00Fh,00Fh,0C3h,074h,01Fh,000h,0C8h,088h
	db	001h,010h,01Fh,01Fh,0C3h,06Fh,00Fh,000h,0D8h,088h
	db	059h,010h,01Fh,01Fh,0C3h,07Fh,03Eh,000h,098h,088h
	db	001h,020h,02Fh,02Fh,0C3h,074h,01Fh,000h,0C8h,088h
	db	01Dh,024h,02Fh,02Fh,0C3h,07Fh,00Fh,000h,0BCh,078h
	db	01Ch,028h,028h,028h,0C5h,074h,04Fh,000h,0DCh,068h
	db	01Ch,029h,029h,029h,0C5h,074h,04Fh,000h,0DCh,068h
	db	019h,0D8h,0E7h,0E7h,0C3h,04Fh,044h,058h,0A8h,088h
	db	01Ch,02Ch,02Ch,02Ch,0C5h,064h,07Fh,048h,088h,088h
	db	019h,0D8h,0E7h,0E7h,0C3h,064h,048h,038h,02Fh,08Fh
	db	01Dh,0E7h,0E7h,0E7h,0C3h,064h,038h,028h,098h,078h
	db	008h,00Fh,00Fh,00Fh,0C5h,054h,07Fh,03Fh,099h,078h
	db	01Dh,068h,087h,087h,0C3h,072h,07Fh,04Fh,0E8h,088h
	db	01Dh,080h,097h,097h,0C1h,07Fh,038h,058h,0C8h,088h
	db	00Dh,097h,097h,097h,0C1h,07Fh,048h,048h,0A8h,078h
	db	00Dh,098h,0AFh,0AFh,0C1h,07Fh,018h,048h,0BFh,088h
	db	019h,0ACh,0ACh,0ACh,0C1h,07Fh,03Fh,048h,0A8h,08Ch
	db	018h,010h,010h,010h,0C5h,064h,08Fh,02Eh,0AAh,088h
	db	018h,00Ch,00Ch,00Ch,0C5h,048h,088h,02Eh,0A8h,09Fh
	db	01Ch,00Ah,00Ah,00Ah,0C5h,07Fh,068h,04Fh,098h,08Fh
	db	01Dh,0A8h,0BFh,0BFh,0C3h,064h,00Fh,028h,0CCh,088h
	db	019h,00Ah,00Ah,00Ah,0C5h,07Fh,07Fh,048h,058h,078h
	db	019h,01Ah,01Ah,01Ah,0C5h,064h,06Fh,03Eh,0ADh,078h
	db	019h,0E8h,0FEh,0FEh,0C3h,064h,00Fh,048h,0AFh,088h
	db	01Ch,023h,023h,023h,0C5h,04Fh,038h,048h,0AFh,088h
	db	005h,020h,02Fh,02Fh,0C4h,064h,038h,04Fh,09Fh,08Dh
	db	01Ch,024h,024h,024h,0C5h,06Fh,068h,048h,078h,08Fh
	db	001h,030h,047h,047h,0C5h,064h,00Fh,04Fh,098h,088h
	db	01Dh,047h,047h,047h,0C5h,064h,078h,038h,0BCh,09Fh
	db	009h,030h,03Fh,03Fh,0C4h,06Fh,028h,000h,0D8h,088h
	db	00Dh,040h,057h,057h,0C4h,064h,00Fh,000h,0E8h,088h
	db	01Dh,058h,06Fh,06Fh,0C4h,064h,06Fh,000h,0D8h,088h
	db	000h,012h,012h,012h,0C5h,06Fh,01Eh,000h,0A8h,0A8h
	db	014h,013h,013h,013h,0C5h,05Fh,00Fh,000h,0A5h,099h
	db	001h,048h,05Fh,05Fh,0C5h,07Fh,00Fh,000h,09Fh,09Fh
	db	000h,014h,014h,014h,0C5h,07Fh,01Fh,000h,0AAh,0AAh
	db	048h,026h,026h,026h,0C5h,06Fh,01Ah,000h,0AFh,0AFh
	db	040h,015h,015h,015h,0C5h,064h,038h,000h,0BEh,0BEh
	db	011h,018h,027h,027h,0C1h,064h,01Fh,000h,0ADh,098h
	db	001h,000h,017h,017h,0C1h,05Fh,00Fh,000h,0BDh,098h
	db	005h,0E0h,0EFh,0EFh,0C4h,07Fh,00Fh,000h,098h,098h
	db	05Ah,0F0h,0FEh,0FEh,0C4h,07Fh,00Fh,000h,0AFh,0AFh
	db	009h,0C4h,0DFh,0DFh,0C4h,064h,038h,05Ah,09Ah,088h
	db	018h,02Ah,02Ah,02Ah,0C5h,04Fh,07Fh,038h,08Fh,09Ch
	db	058h,010h,010h,010h,0C5h,02Fh,08Fh,038h,098h,09Ch
	db	019h,050h,067h,067h,0C3h,064h,00Fh,048h,078h,088h
	db	016h,060h,077h,077h,0C5h,064h,07Fh,000h,0A8h,088h
	db	001h,0C0h,0E7h,0E7h,0C5h,07Fh,00Fh,000h,0B8h,098h
	db	001h,0E8h,0F7h,0F7h,0C5h,07Fh,00Fh,000h,088h,088h
	db	001h,0A0h,0BFh,0BFh,0C5h,07Fh,00Fh,000h,0BBh,0BBh
	db	002h,050h,05Fh,05Fh,0C2h,07Fh,00Fh,000h,0CFh,098h
	db	001h,060h,06Fh,06Fh,0C2h,07Fh,00Fh,000h,0C8h,098h
	db	042h,070h,07Fh,07Fh,0C1h,07Fh,00Fh,000h,0C8h,0A8h
	db	002h,000h,00Fh,00Fh,0C4h,07Fh,00Fh,000h,0DDh,0DDh
	db	002h,090h,0AAh,0BAh,0C6h,07Fh,00Fh,000h,0DDh,0A8h
	db	002h,048h,04Fh,04Fh,0C2h,07Fh,00Fh,000h,0B8h,0A9h
	db	041h,020h,047h,047h,0C2h,07Fh,03Fh,05Fh,0A8h,099h
	db	002h,0D0h,0FEh,0FEh,0C1h,07Fh,00Fh,000h,0CFh,0A8h
	db	029h,052h,058h,058h,080h,064h,00Fh,000h,0D8h,088h
	db	029h,042h,04Ch,04Ch,080h,064h,00Fh,000h,0D8h,088h
	db	019h,000h,000h,000h,080h,058h,038h,000h,0D8h,088h
	db	01Dh,052h,05Fh,05Fh,080h,064h,00Fh,000h,0B8h,088h
	db	0D9h,050h,057h,057h,080h,064h,018h,000h,0D8h,088h

;<< NO.[ 6 ] 1msec Timer Interrupt Sequence >>
@_int_1msec_seq:
	bit	int_bit_tim_1,(int_flg)
	ret	z
	res	int_bit_tim_1,(int_flg)
	bit	0,(led_timer+0)	 	; Odd/Even ?
	jr	z,_int_1msec_even
	ld	(port_treg3),187	; 1msec Interval Constant(1)
	jr	_int_1msec_cont
_int_1msec_even:
	ld	(port_treg3),188	; 1msec Interval Constant(2)
_int_1msec_cont:
	inc	(led_timer+0)
	cp	(led_timer+0),5		; 5msec Interval ?
	ret	c
	ld	(led_timer+0),0
	ld	c,(port_adreg)
	ld	b,0
	ld	hl,(ad_sum)
	add	hl,bc
	ld	(ad_sum),hl
        ld      (port_admod),11111000b	; A/D Start !
	inc	(led_timer+1)
	cp	(led_timer+1),20	; 20 Times ?
	ret	c
	ld	(led_timer+1),0
	ld	hl,(ad_sum)
	ldw	(ad_sum),0
	div	hl,20
zzz:
	cp	l,0a0h			; Trigger Level !
	jr	c,_ad_eve
	ld	(ad_flg),0
	ret
_ad_eve:
	cp	(ad_flg),0
	ret	nz
	ld	(ad_flg),1
	bit	0,(active_status)
	jr	z,_on_0
	bit	1,(active_status)
	jr	z,_on_1
	bit	2,(active_status)
	jr	z,_on_2
	bit	3,(active_status)
	jr	z,_on_3
	ret
_on_0:
	set	real_play,(run_mode)
	set	0,(active_status)
	ld	(play_data_1+0),0
	ld	(play_counter+0),0
	ldw	(pointer_1),sequence_data
	ret
_on_1:
	set	real_play,(run_mode)
	set	1,(active_status)
	ld	(play_data_1+1),0
	ld	(play_counter+1),0
	ldw	(pointer_2),sequence_data
	ret
_on_2:
	set	real_play,(run_mode)
	set	2,(active_status)
	ld	(play_data_1+2),0
	ld	(play_counter+2),0
	ldw	(pointer_3),sequence_data
	ret
_on_3:
	set	real_play,(run_mode)
	set	3,(active_status)
	ld	(play_data_1+3),0
	ld	(play_counter+3),0
	ldw	(pointer_4),sequence_data
	ret

	%include(pre-1.hex)		; <--- Song Data !!!
	end

8086アセンブラ コピープロテクト解除

;*-------------------------------------------------------------*
;*      Master --> Child Copy Program                          *
;*-------------------------------------------------------------*
        cgroup  group   code

gdata segment
        dta     db 10000 dup (0aah)
gdata ends

code segment public 'code'
        assume  cs:code,ds:gdata,es:extra,ss:stack

main:;========== Main Routine ==========
        mov     ax,stack
        mov     ss,ax
        mov     ax,offset ss:ss_top
        mov     sp,ax
        mov     ax,extra
        mov     es,ax
        call    gds_set
        mov     ah,0
        mov     dl,01000000b
        int     13h                     ; Disk  System Reset
        mov     ah,0
        mov     dl,01000000b
        int     13h                     ; Disk  System Reset
        call    gds_set
        mov     ax,ds
        mov     es,ax
        mov     bx,offset dta
        mov     cx,0001h
        mov     dx,0040h
        mov     ax,0201h
        int     13h                     ; Boot Load from 
        call    changer
        mov     dx,0041h
        mov     ax,0301h
        int     13h                     ; Boot Save to 
        mov     si,offset bank1
        call    disc_para_set
        mov     cx,0201h
        mov     dx,0040h
        mov     ax,0208h
        int     13h                     ; 1st Load from 
        mov     dx,0041h
        mov     ax,0308h
        int     13h                     ; 1st Save to 
        mov     cx,0401h
        mov     dx,0040h
        mov     ax,0208h
        int     13h                     ; 2nd Load from 
        mov     dx,0041h
        mov     ax,0308h
        int     13h                     ; 2nd Save to 
        mov     ch,15h
loop_001:
        mov     si,offset bank2
        call    disc_para_set
        mov     cl,06h
        mov     dx,0140h
        mov     ax,0203h
        int     13h                     ; 3rd Load from 
        mov     si,offset bank1
        call    disc_para_set
        mov     cl,03h
        mov     dx,0141h
        mov     ax,0306h
        int     13h                     ; 3rd Save to 
        sub     ch,2
        jns     loop_001
        mov     cx,1301h
loop_002:
        mov     si,offset bank3
        call    disc_para_set
        mov     dx,0140h
        mov     ax,0205h
        int     13h                     ; 4th Load from 
        mov     si,offset bank1
        call    disc_para_set
        mov     dx,0041h
        mov     ax,0305h
        int     13h                     ; 4th Save to 
        sub     ch,2
        jns     loop_002
        mov     ah,4ch
        int     21h                     ; DOS Return
bank1:
        db      0efh,003h,025h,002h,009h,02ah,0ffh
bank2:
        db      0efh,003h,025h,003h,009h,02ah,0ffh
bank3:
        db      0efh,003h,025h,001h,009h,02ah,0ffh

changer:;========== Data Chane Routine ==========
        call    gds_set
        push    bx
        mov     bx,97h
        mov     dta[bx],0c3h            ; Return
        mov     bx,54h
        mov     dta[bx],03h             ; Sector No. = 3
        mov     bx,5ah
        mov     dta[bx],06h             ; Sector Count = 6
        mov     bx,7ah
        mov     dta[bx],0               ; Head = 0
        pop     bx
        ret

disc_para_set:;========== Disc Parameter ==========
        push    es
        mov     ax,0
        mov     es,ax
        mov     di,0078h
        mov     es:[di],si
        mov     di,007ah
        mov     es:[di],cs
        pop     es
        ret

gds_set:;========== Data Segment Set ==========
        push    ax
        mov     ax,gdata
        mov     ds,ax
        pop     ax
        ret

code    ends

extra segment
        es_area db 1024 dup (?)
extra ends

stack segment stack
        ss_area db 2048 dup (?)
        ss_top  label word
stack ends

        end     main

;*-------------------------------------------------------------*
;*      Child --> Child Copy Program                           *
;*-------------------------------------------------------------*
        cgroup  group   code

gdata segment
        dta     db 10000 dup (0aah)
gdata ends

code segment public 'code'
        assume  cs:code,ds:gdata,es:extra,ss:stack

child:;========== Main Routine ==========
        mov     ax,stack
        mov     ss,ax
        mov     ax,offset ss:ss_top
        mov     sp,ax
        mov     ax,extra
        mov     es,ax
        call    gds_set
        mov     ah,0
        mov     dl,01000000b
        int     13h                     ; Disk  System Reset
        mov     ah,0
        mov     dl,01000000b
        int     13h                     ; Disk  System Reset
        call    gds_set
        mov     ax,ds
        mov     es,ax
        mov     bx,offset dta
        mov     cx,0001h
        mov     dx,0040h
        mov     ax,0201h
        int     13h                     ; Boot Load from 
        mov     dx,0041h
        mov     ax,0301h
        int     13h                     ; Boot Save to 
        mov     cx,0201h
        mov     dx,0040h
        mov     ax,0208h
        int     13h                     ; 1st Load from 
        mov     dx,0041h
        mov     ax,0308h
        int     13h                     ; 1st Save to 
        mov     cx,0401h
        mov     dx,0040h
        mov     ax,0208h
        int     13h                     ; 2nd Load from 
        mov     dx,0041h
        mov     ax,0308h
        int     13h                     ; 2nd Save to 
        mov     cx,1503h
loop_001:
        mov     dx,0140h
        mov     ax,0206h
        int     13h                     ; 3rd Load from 
        mov     dx,0141h
        mov     ax,0306h
        int     13h                     ; 3rd Save to 
        sub     ch,2
        jns     loop_001
        mov     cx,1301h
loop_002:
        mov     dx,0040h
        mov     ax,0205h
        int     13h                     ; 4th Load from 
        mov     dx,0041h
        mov     ax,0305h
        int     13h                     ; 4th Save to 
        sub     ch,2
        jns     loop_002
        mov     ah,4ch
        int     21h                     ; DOS Return

gds_set:;========== Data Segment Set ==========
        push    ax
        mov     ax,gdata
        mov     ds,ax
        pop     ax
        ret

code    ends

extra segment
        es_area db 1024 dup (?)
extra ends

stack segment stack
        ss_area db 2048 dup (?)
        ss_top  label word
stack ends

        end     child

パソコンのC その1 カオスシンセサイザ

#include 
#include 
#include 
#include 
#include 
#include 

#define	ERROR			-1
#define	STATPT  		0xe0d2
#define DATAPT  		0xe0d0
#define DRR     		0x0040
#define DSR     		0x0080
#define PUT			8888
#define GET			9999
#define entry_table		0x0080
#define vector_offset_user_com	10

FILE	*fds,*fdo;
static	union REGS inregs,outregs;
static 	struct SREGS segregs;
int	msb_mode,para[16][8],moux,mouy,right,left,sts[16],debug,xpt,ypt,stp;
int	xx,xxx,yy,yyy,high,old_l,old_r,old_x,old_y,disp,old_disp,solo,win;
int	mbuff_x,mbuff_y,w_x,w_y,back,front,b_ctr;
char	buff[16];
double	myu,chaos,_1st,_2nd,_3rd,beep,b_myu;

unsigned get_entry(x) int x; { unsigned ent,z; z=(unsigned)x;
	ent=msb_data_get(entry_table)+msb_data_get(entry_table+1)*256;
	return(msb_data_get(ent+2*z)+msb_data_get(ent+2*z+1)*256); }
msb_data_put(add,data) unsigned add; int data; {
	set_command(0xe3); put_data(add/256); set_command(0xe5);
	put_data(add%256); set_command(0xeb); put_data(data); }
msb_data_get(add) unsigned add; {
	set_command(0xe3); put_data(add/256); set_command(0xe5);
	put_data(add%256); set_command(0xae); return(get_data()); }
put_command(x) int x; { while((inp(STATPT)&DRR)!=0){} outp(STATPT,x); }
put_data(x) int x; { while((inp(STATPT)&DRR)!=0){} outp(DATAPT,x); }
get_data(){ while((inp(STATPT)&DSR)!=0){} return(inp(DATAPT)); }
set_command(x) int x; { put_command(x); while(get_data()!=0xfe){} }
mouseini(){ inregs.x.ax=0; int86(51,&inregs,&outregs);
	if(outregs.x.ax==0){ printf("mouse.sys missing\n"); exit(0); } }
mouse(flag) int flag; {
	if(flag==0){inregs.x.ax=2;} else if(flag==1){inregs.x.ax=1;}
	int86(51,&inregs,&outregs); }
getmouse(right,left,x,y) int *right,*left,*x,*y; {
	inregs.x.ax=3; int86(51,&inregs,&outregs); *right=outregs.x.bx;
	*left=outregs.x.ax; *x=outregs.x.cx; *y=outregs.x.dx; }
setmouse(x,y) int x,y; { inregs.x.ax=4; inregs.x.cx=x; inregs.x.dx=y;
	int86(51,&inregs,&outregs); }
m_style(x,y,buf) int x,y; char *buf; { inregs.x.ax=9; inregs.x.bx=x;
	inregs.x.cx=y; segread(&segregs); segregs.es=segregs.ds;
	inregs.x.dx=(int)buf; int86x(51,&inregs,&outregs,&segregs); }
m_view(xmin,ymin,xmax,ymax) int xmin,ymin,xmax,ymax; {
	inregs.x.ax=16; inregs.x.cx=xmin; inregs.x.dx=xmax;
	int86(51,&inregs,&outregs); inregs.x.ax=17; inregs.x.cx=ymin;
	inregs.x.dx=ymax; int86(51,&inregs,&outregs); }
m_color(color) int color; {
	inregs.x.ax=18; inregs.x.bx=color; int86(51,&inregs,&outregs); }
symbol(x,y,color,str,skip) int x,y,color,skip; unsigned char str[80]; {
	int i=0; _setcolor(color);
	while(str[i]!=0){ _gputchar(x,y,str[i++],_GXOR); x += skip; } }
unsigned char patch[1009] = {
	0x1C,0x66,0x29,0xEB,0xE1,0xFE,0x37,0x00,0xEB,0xE3,0xFE,0x37,
	0x00,0xEB,0xE2,0xFE,0x37,0x00,0xEB,0xE4,0xFE,0x37,0x00,0xEB,
	0xEF,0xFE,0x37,0x00,0xEB,0xE0,0xFE,0x37,0x55,0x33,0x00,0x23,
	0x3A,0x60,0x6F,0xF7,0x37,0x00,0x3A,0x00,0x6F,0xF7,0x37,0x00,
	0x1C,0x37,0x44,0x83,0xFB,0x6F,0x10,0xCE,0xEA,0x33,0x00,0x30,
	0xFA,0xFB,0x31,0xF9,0x6E,0x90,0xE8,0x37,0x00,0x83,0xFB,0x6F,
	0x10,0xCE,0xF2,0x1C,0xDB,0x41,0x1C,0x57,0x41,0x1C,0x3D,0x43,
	0xC8,0xF5,0x47,0x7F,0x77,0x7D,0xFE,0xD6,0x39,0x00,0x70,0xF9,
	0x70,0xE2,0x28,0xEF,0x76,0x20,0x97,0x7F,0xAC,0x80,0xC6,0x04,
	0x3F,0x7F,0x00,0x00,0xF8,0xAF,0xC6,0x1F,0xF8,0x6F,0xF0,0xC7,
	0x0B,0xF8,0x6F,0xF8,0xFE,0xD6,0xFE,0xDF,0x37,0x78,0x00,0x1E,
	0x20,0xF8,0x6C,0xF0,0xEF,0x78,0x20,0x6C,0x0F,0x2F,0x77,0x37,
	0x7A,0x00,0x1E,0x27,0x78,0x6F,0x00,0xFE,0xD6,0xEF,0x7A,0x6F,
	0x00,0xCE,0x0C,0x6F,0xC0,0xC6,0x0F,0x6F,0xD0,0xC6,0x1F,0x37,
	0x7A,0x01,0x1E,0x37,0x7A,0x00,0x37,0x78,0x00,0x1E,0xEF,0x77,
	0x6F,0x0F,0xFE,0xDE,0x3A,0x00,0x68,0xE3,0xE1,0xFE,0x2E,0xF7,
	0x20,0xE3,0xE1,0xFE,0x87,0x1E,0xEF,0x77,0x6F,0x0F,0xFE,0xDE,
	0x3A,0x00,0x6A,0xE3,0xE3,0xFE,0x2E,0xF7,0x20,0xE3,0xE3,0xFE,
	0x87,0x1E,0xA8,0x62,0xCE,0x08,0xA9,0x62,0xFE,0xD6,0xB1,0x62,
	0xC8,0x08,0xB0,0x62,0x1C,0x46,0x04,0x1C,0xF2,0x41,0x27,0xF1,
	0x1E,0x27,0x5F,0x6C,0xF0,0xA7,0xA7,0xA7,0x3A,0x00,0x42,0xF3,
	0x4A,0xEA,0xC8,0x58,0x11,0x58,0x11,0x58,0x11,0x41,0x12,0xE9,
	0x12,0xE9,0x12,0xE9,0x12,0xE9,0x12,0x7F,0x13,0xDC,0x13,0x20,
	0x42,0x8C,0x14,0xF2,0x14,0x3F,0x15,0x1C,0x16,0xC3,0x16,0x27,
	0x5F,0xFE,0xAB,0xEB,0x19,0x14,0xC6,0x6C,0x07,0xA6,0x3A,0x32,
	0x42,0xF3,0x4A,0xEA,0xC8,0x56,0x11,0x88,0x42,0x42,0x42,0x2A,
	0x14,0x33,0x14,0x41,0x14,0x4F,0x14,0x84,0x14,0xE3,0xE2,0xFE,
	0x2E,0xE3,0xE1,0xFE,0x67,0xCE,0x10,0xE3,0xE4,0xFE,0x2E,0xE3,
	0xE3,0xFE,0x67,0xCE,0x1C,0x30,0xFF,0x1C,0x48,0x04,0x1E,0x30,
	0xCF,0x1C,0x48,0x04,0x3A,0x00,0x68,0xE3,0xE2,0xFE,0x2E,0xF3,
	0x28,0xE3,0xE2,0xFE,0x87,0x1C,0x48,0x04,0x1E,0x30,0xDF,0x1C,
	0x48,0x04,0x3A,0x00,0x6A,0xE3,0xE4,0xFE,0x2E,0xF3,0x28,0xE3,
	0xE4,0xFE,0x87,0x1C,0x48,0x04,0x1E,0xEB,0xE0,0xFE,0x6F,0x55,
	0xFE,0xDE,0xEB,0x50,0xFE,0x37,0x00,0x3A,0xE5,0xFE,0x27,0xF1,
	0xA9,0x62,0xC6,0xFC,0xB1,0x62,0xE7,0x60,0x28,0xE3,0x50,0xFE,
	0x2E,0xF7,0x20,0xE3,0x50,0xFE,0x87,0xEB,0x50,0xFE,0x6F,0x08,
	0xCE,0xE4,0xE3,0xE5,0xFE,0x2E,0x6C,0x0F,0xEB,0xE6,0xFE,0x6F,
	0x00,0xC6,0x1A,0xEB,0xE6,0xFE,0x6F,0x01,0xC6,0x0D,0x3A,0x60,
	0x6F,0xF7,0x37,0x00,0x3A,0x00,0x6F,0xF7,0x37,0x00,0x1E,0x3A,
	0x60,0x6F,0xF7,0x37,0x80,0x3A,0x00,0x6F,0xE3,0xE9,0xFE,0x28,
	0xF7,0x20,0x3A,0x10,0x6F,0xE3,0xEA,0xFE,0x28,0xF7,0x20,0x3A,
	0x20,0x6F,0xE3,0xEB,0xFE,0x28,0xF7,0x20,0x3A,0x40,0x6F,0xE3,
	0xE7,0xFE,0x28,0xF8,0x6D,0xFF,0xF7,0x20,0x3A,0x50,0x6F,0xE3,
	0xE8,0xFE,0x28,0xF7,0x20,0x29,0xE3,0xEC,0xFE,0x2E,0x6C,0x1F,
	0x3A,0x1D,0x43,0xF3,0x28,0x3A,0x90,0x6F,0x21,0xF7,0x20,0x1E,
	0x5C,0x5F,0x4C,0x4F,0x3A,0x3C,0x3D,0x3F,0x28,0x29,0x2A,0x2B,
	0x2C,0x2D,0x2E,0x2F,0x18,0x19,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,
	0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0xE3,0xEF,0xFE,0x87,
	0xEB,0xEF,0xFE,0x6F,0x10,0xCE,0x05,0xEB,0xEF,0xFE,0x37,0x00,
	0xE3,0xEF,0xFE,0x2E,0x3A,0x30,0x6F,0xF3,0x8F,0xFE,0xDE,0x3A,
	0x40,0x6F,0xF3,0x28,0x3A,0x30,0x6F,0xF7,0x20,0x3A,0x60,0x6F,
	0xF3,0x28,0xEB,0x5C,0xFE,0x20,0x3A,0x70,0x6F,0xF3,0x28,0xEB,
	0x5B,0xFE,0x20,0x3A,0x80,0x6F,0xF3,0x28,0xEB,0x5A,0xFE,0x20,
	0x36,0xFF,0xE3,0x5A,0xFE,0x62,0xEB,0x5D,0xFE,0x26,0x36,0xFF,
	0xE3,0x5B,0xFE,0x62,0xEB,0x5E,0xFE,0x26,0x36,0xFF,0xE3,0x5C,
	0xFE,0x62,0xEB,0x5F,0xFE,0x26,0x1C,0x35,0x05,0xE3,0xEF,0xFE,
	0x2E,0x3A,0x00,0x6F,0xF3,0x28,0xEB,0x5C,0xFE,0x20,0x3A,0x10,
	0x6F,0xF3,0x28,0xEB,0x5B,0xFE,0x20,0x3A,0x20,0x6F,0xF3,0x28,
	0xEB,0x5A,0xFE,0x20,0xE3,0x62,0xFE,0x28,0xE3,0x63,0xFE,0x29,
	0xE3,0x64,0xFE,0x2A,0xE3,0x65,0xFE,0x2B,0xF8,0xA2,0xF9,0xA2,
	0xFA,0xA2,0xFB,0xA2,0xF8,0xA2,0xF9,0xA2,0xFA,0xA2,0xFB,0xA2,
	0xEB,0x5D,0xFE,0x21,0xEB,0x5E,0xFE,0x22,0xEB,0x5F,0xFE,0x23,
	0x1C,0x35,0x05,0xE3,0x63,0xFE,0x29,0xE3,0x64,0xFE,0x2A,0xE3,
	0x65,0xFE,0x2B,0xE3,0xEF,0xFE,0x2E,0x3A,0x60,0x6F,0xF7,0x23,
	0x3A,0x70,0x6F,0xF7,0x22,0x3A,0x80,0x6F,0xF7,0x21,0x3A,0x50,
	0x6F,0xF3,0x2E,0x6F,0x0C,0xC7,0x02,0x36,0x0B,0x6F,0x00,0xC6,
	0x09,0xFB,0xA7,0xFA,0xA3,0xF9,0xA3,0x8E,0xC8,0xF3,0xEB,0x63,
	0xFE,0x21,0xEB,0x64,0xFE,0x22,0xEB,0x65,0xFE,0x23,0xE3,0xEF,
	0xFE,0x2B,0x1C,0xB4,0x44,0x1E,0x30,0xFA,0xFB,0x31,0xE8,0x37,
	0x00,0xFB,0x31,0xF9,0x6E,0x10,0xE8,0x37,0x00,0xFB,0x31,0xF9,
	0x6E,0x20,0xE8,0x37,0x00,0xFB,0x31,0xF9,0x6E,0x30,0xE8,0x37,
	0x00,0xFB,0x31,0xF9,0x6E,0x40,0xE8,0x37,0x00,0xFB,0x31,0xF9,
	0x6E,0x50,0xE8,0x37,0x00,0xFB,0x31,0xF9,0x6E,0x60,0xE8,0x37,
	0x80,0x3A,0xA4,0x44,0x23,0xF3,0x2E,0xFB,0x31,0xF9,0x6E,0x70,
	0xE8,0x26,0xFB,0x31,0xF9,0x6E,0xA0,0xE8,0x37,0x1F,0xFB,0x31,
	0xF9,0x6E,0xB0,0xE8,0x37,0x1F,0xFB,0x31,0xF9,0x6E,0xC0,0xE8,
	0x37,0x1F,0xFB,0x31,0xF9,0x6E,0xD0,0xE8,0x37,0xF1,0xFB,0x31,
	0xF9,0x6E,0x90,0xE8,0x37,0x03,0x1E,0x00,0x7F,0xFF,0x3F,0xAF,
	0x5F,0xCF,0x00,0xEF,0x2F,0xBF,0x4F,0xDF,0x1F,0x9F,0x6F,0x30,
	0xFA,0xE3,0x63,0xFE,0x2E,0xFB,0x31,0xE8,0x26,0xE3,0x64,0xFE,
	0x2E,0xFB,0x31,0xF9,0x6E,0x10,0xE8,0x26,0xE3,0x65,0xFE,0x2E,
	0xFB,0x31,0xF9,0x6E,0x20,0xE8,0x26,0xFB,0x31,0xF9,0x6E,0x30,
	0xE8,0x37,0x00,0x3A,0x90,0x6F,0x23,0xF3,0x2A,0xFB,0x31,0xF9,
	0x6E,0xC0,0xE8,0x22,0xFB,0x31,0xF9,0x6E,0xD0,0xE8,0x37,0x1F,
	0x1E };
static int style[]={
	0,0,0,0,0,0,0,0,0x0070,0x0e88,0x1184,0x2142,0x2221,0x9210,
	0xe107,0x1108,0x1108,0xe107,0x1108,0x1108,0xe107,0x4108,
	0x4208,0xfc07,0,0,0,0,0,0,0,0 };
msb98_check(){ int i,j; for(i=0;i<100;i++) inp(DATAPT);
	j=0; for(i=0;i<10000;i++){ if((inp(STATPT)&DRR)==0) j=1; }
	if(j==0) return(ERROR);
	outp(STATPT,0xff);
	j=0; for(i=0;i<32000;i++){ if((inp(STATPT)&DSR)==0) j=1; }
	if(j==0) return(ERROR);
	if(inp(DATAPT)!=0xfe) return(ERROR);
	set_command(0xff); set_command(0xac); i=get_data();
	if(i!=0x15) return(ERROR);
	set_command(0xf0); set_command(0xac); i=get_data();
	if(i!=0x28) return(ERROR);
	set_command(0xff); return(0); }
diaplay_setting(x) int x; {
	if(x==0){ _setvideomode(_98RESS16COLOR); _displaycursor(_GCURSOROFF);
		_clearscreen(_GCLEARTEXT); _clearscreen(_GCLEARSCREEN); }
	else if(x==1){
		_clearscreen(_GCLEARSCREEN); _displaycursor(_GCURSORON);
		mouse(0); if(msb_mode==1) set_command(0xfe);
		outp(0x37,7); _setvideomode (_DEFAULTMODE); } }
parameter_file_write(){ int i,j; if(debug!=0) return(0);
	fds=fopen("chaos_dt.msb","wb");
	for(i=0;i<16;i++){ for(j=0;j<8;j++) fputc(para[i][j],fds); }
	fputc(moux/256,fds); fputc(moux%256,fds);
	fputc(mouy/256,fds); fputc(mouy%256,fds);
	fputc(stp,fds); fclose(fds); }
parameter_init(){ int i; mbuff_x=320; mbuff_y=200; stp=3;
	for(i=0;i<16;i++){
		para[i][0]=i;		/* Channel : 0 - 15 */
		para[i][1]=0;		/* [0]Off [1]Mute [2]On [3]Solo */
		para[i][2]=230-10*i;	/* Step : 0 - 250 */
		para[i][3]=6;		/* Range : 0 - 11 */
		para[i][4]=254-i;	/* High */
		para[i][5]=255;		/* Middle */
		para[i][6]=255;		/* Low */
		para[i][7]=20;		/* Volume : 0 - 31 */ } }
parameter_file_read(){ int i,j;
	if((fds=fopen("chaos_dt.msb","rb"))!=NULL){
		for(i=0;i<16;i++){ for(j=0;j<8;j++) para[i][j]=fgetc(fds); }
		mbuff_x=256*fgetc(fds); mbuff_x=mbuff_x+fgetc(fds);
		mbuff_y=256*fgetc(fds); mbuff_y=mbuff_y+fgetc(fds);
		stp=fgetc(fds); fclose(fds); }
	else parameter_init();
	if(debug!=0){ mbuff_x=20; mbuff_y=110; }
	for(i=0;i<16;i++) { sts[i]=0; if(para[i][1]==3) solo=1; } }
volume_hit(i) int i; { old_l=left; old_x=moux; old_y=mouy;
	while(1){ getmouse(&right,&left,&moux,&mouy);
		if(left==0) break;
		else if(old_y!=mouy){
			old_y=mouy; _setcolor(back);
			if(mouy<309) para[i][7]=31;
			else if(mouy>367) para[i][7]=1;
			else para[i][7]=(370-mouy)/2;
			_rectangle(_GFILLINTERIOR,xx+8,yy+4,xx+28,yy-64);
			_setcolor(front); yyy=yy-2*para[i][7]+2;
			_rectangle(_GBORDER,xx+9,yyy+3,xx+27,yyy-3);
			_rectangle(_GBORDER,xx+10,yyy+2,xx+26,yyy-2);
			_moveto(xx+12,yyy); _lineto(xx+24,yyy);
			parameter_display(i,7); send_para(i); } } }
send_para(x) int x; { int i;
	if(msb_mode==0){
		if(para[x][1]>1){ outp(0x37,6); return(0); }
		else{ outp(0x37,7); return(0); } }
	set_command(0xa9); put_data(x);
	if((para[x][1]==2)&&(sts[x]==0)){ put_data(1); sts[x]=1; }
	else if((para[x][1]==2)&&(sts[x]==1)){ put_data(0); sts[x]=1; }
	else if((para[x][1]==3)&&(sts[x]==0)){ put_data(1); sts[x]=1; }
	else if((para[x][1]==3)&&(sts[x]==1)){ put_data(0); sts[x]=1; }
	else{ put_data(2); sts[x]=0; }
	for(i=2;i<8;i++) put_data(para[x][i]); }
parameter_display(i,j) int i,j; { int d,e,sk=4,yt,y_pt;
	if(j==50){
		_setcolor(back);
		for(d=0;d<8;d++){
			_rectangle(_GFILLINTERIOR,419,23+8*d,425,27+8*d);
		}
		_setcolor(front);
		_rectangle(_GBORDER,419,23+8*(stp-2),425,27+8*(stp-2));
		_rectangle(_GBORDER,420,24+8*(stp-2),424,26+8*(stp-2));
	}
	else if(j==3){
		for(e=0;e<12;e++){
			yt=245+e*3; _setcolor(back);
			if(para[i][j]==e) _setcolor(front);
			_moveto(xx+2*e+4,yt+1); _lineto(xx+2*e+10,yt+1);
			_moveto(xx+2*e+4,yt+2); _lineto(xx+2*e+10,yt+2);
		}
		_setcolor(front); return(0);
	}
	else if(j==999){
		if(msb_mode==0) beep_chaos(1);
		myu=(double)para[disp][4]*_1st;
		myu=myu+(double)para[disp][5]*_2nd;
		myu=myu+(double)para[disp][6]*_3rd;
		chaos=myu; ypt=94-(int)(80.9*chaos); return(0);
	}
	else if(j==100){
		_setcolor(front);
		if(xpt>339) d=(xpt-340)/10;
		else d=(xpt-10)/10+8;
		if(d==0) _rectangle(_GFILLINTERIOR,13,13,20,94);
		else if(d==40) _rectangle(_GFILLINTERIOR,410,13,415,94);
		else _rectangle(_GFILLINTERIOR,10+10*d,13,20+10*d,94);
		if(xpt>win-3-stp){
			xpt=14; _rectangle(_GFILLINTERIOR,90,13,100,94);
		}
		chaos=4.0*myu*chaos*(1.0-chaos); y_pt=94-(int)(80.9*chaos);
		_setcolor(back); _moveto(xpt,ypt); _lineto(xpt+stp,y_pt);
		ypt=y_pt; xpt=xpt+stp; return(0);
	}
	sprintf(buff,"%d",para[i][j]);
	if(para[i][j]>99) d=0;
	else if(para[i][j]>9) d=1;
	else d=2;
	if((j==4)||(j==5)||(j==6)){
		yt=(j-4)*32+150;
		_setcolor(front);
		_rectangle(_GFILLINTERIOR,xx+4,yt-1,xx+32,yt+17);
		if(disp!=i){
			_setcolor(back);
			_rectangle(_GFILLINTERIOR,xx+5,yt,xx+31,yt+16);
		}
		symbol(xx+6+d*sk,yt,7,buff,sk*2);
		_setcolor(front);
	}
	else if(j==7){
		_setcolor(back);
		_rectangle(_GFILLINTERIOR,xx+6,289,xx+30,303);
		sk=5; symbol(xx+4+d*sk,288,7,buff,sk*2);
		_setcolor(front);
	}
	else if(j==2){
		if(para[i][1]==3) _setcolor(front);
		else _setcolor(back);
		_rectangle(_GFILLINTERIOR,xx+3,high+5,xx+33,high+35);
		_setcolor(front);
		if(para[i][1]==2){
			_rectangle(_GBORDER,xx+3,high+5,xx+33,high+35);
			_rectangle(_GBORDER,xx+4,high+6,xx+32,high+34);
		}
		else if(para[i][1]==1){
			_setlinestyle(0x5555);
			_rectangle(_GBORDER,xx+4,high+6,xx+32,high+34);
			_setlinestyle(0xffff);
		}
		symbol(xx+6+d*sk,high+12,7,buff,sk*2);
		_setcolor(front);
	}
}

screen_display(m)
	int m;
{
	int i,j,yt;
	mouse(0); _setcolor(back); _setlinestyle(0xffff);
	_rectangle(_GFILLINTERIOR,0,0,639,399); _setcolor(front);
	_moveto(629,0); _lineto(639,10); _lineto(639,399); _lineto(10,399);
	_lineto(0,389); _lineto(629,389); _lineto(629,0); _lineto(0,0);
	_lineto(0,389); _moveto(629,389); _lineto(639,399);
	if(front==0){
		_moveto(630,0); _lineto(639,0); _moveto(631,1); _lineto(639,1);
		_moveto(632,2); _lineto(639,2); _moveto(633,3); _lineto(639,3);
		_moveto(634,4); _lineto(639,4); _moveto(635,5); _lineto(639,5);
		_moveto(636,6); _lineto(639,6); _moveto(637,7); _lineto(639,7);
		_moveto(638,8); _lineto(639,8); _moveto(639,9); _lineto(639,9);
		_moveto(0,390); _lineto(0,390); _moveto(0,391); _lineto(1,391);
		_moveto(0,392); _lineto(2,392); _moveto(0,393); _lineto(3,393);
		_moveto(0,394); _lineto(4,394); _moveto(0,395); _lineto(5,395);
		_moveto(0,396); _lineto(6,396); _moveto(0,397); _lineto(7,397);
		_moveto(0,398); _lineto(8,398); _moveto(0,399); _lineto(9,399);
	}
	for(i=1;i<389;i=i+3){ _moveto(629,i); _lineto(639,10+i); }
	for(i=1;i<629;i=i+3){ _moveto(i,389); _lineto(10+i,399); }
	_rectangle(_GBORDER,1,1,628,388); _rectangle(_GBORDER,2,2,627,387);
	_rectangle(_GBORDER,5,5,623,383); _rectangle(_GBORDER,6,6,622,382);
	_rectangle(_GBORDER,11,11,win,96);
	_rectangle(_GFILLINTERIOR,13,13,win-2,94);
	_rectangle(_GBORDER,win,11,win+10,21);
	_moveto(win+1,21); _lineto(win+5,12); _lineto(win+9,21);
	for(i=0;i<8;i++){
		_rectangle(_GBORDER,win,21+8*i,win+10,29+8*i);
	}
	_rectangle(_GBORDER,win,85,win+10,96);
	_moveto(win+1,85); _lineto(win+5,95); _lineto(win+9,85);
	parameter_display(stp,50); _rectangle(_GBORDER,430,11,617,96);
	_rectangle(_GBORDER,432,13,615,51); _rectangle(_GBORDER,433,14,614,50);
	if(back!=0) printf("\x1b[30m");
	else printf("\x1b[37m");
	_settextposition(1,56); printf("カオス・シンセサイザ");
	if(debug==0){ _settextposition(2,57); printf("([MSB-98] Version)"); }
	else{ _settextposition(2,60); printf("(解説デモ版)"); }
	_setcolor(front); _moveto(430,53); _lineto(617,53);
	symbol(434,59,7,"KAWA",8); symbol(465,59,7,"I",8);
	symbol(474,59,7,"Computer",7); symbol(532,59,7,"Music",7);
	symbol(570,59,7,"System",7);
	_setcolor(front); _moveto(430,80); _lineto(617,80);
	_rectangle(_GFILLINTERIOR,558,80,616,95); symbol(569,80,7,"init",10);
	for(i=0;i Graphic / Beep Only...\n\n");
		while(1){ if(kbhit()!=0) break; }
		getch(); msb_mode=0; debug=1; _clearscreen(_GCLEARSCREEN);
	}
	else{
		for(i=0;i-1;i--){
			set_command(0xa9); put_data(i); put_data(1);
			for(j=2;j<8;j++) put_data(para[i][j]);
			set_command(0xa9); put_data(i); put_data(0);
			for(j=2;j<8;j++) put_data(para[i][j]);
			send_para(i);
		}
	}
	if(debug!=0){ demo_limit=49; m=1; }
	parameter_file_read(); diaplay_setting(0);
	setmouse(mbuff_x,mbuff_y);
	screen_display(m);
	if(msb_mode==0) beep_chaos(1);
	while(1){
		if((kbhit()!=0)&&(getch()==0x1b)) break;
		if(msb_mode==0) beep_chaos(0);
		getmouse(&right,&left,&moux,&mouy); disp=255;
		mouse(0); parameter_display(0,100); mouse(1);
		if(back!=0) printf("\x1b[30m");
		else printf("\x1b[37m");
		_settextposition(5,55);
		printf("St=%d (%03d,%03d)",stp-1,moux,mouy);
		if(((moux+26)%38<35)&&(mouy>high)&&(mouy<377)){
			if((moux>10)&&(moux10)&&(moux18){
				j=(moux-1)/38; yyy=370-2*para[j][7]+2;
				if((mouy>yyy-4)&&(mouy242)&&(mouy<256)){
				j=(moux+3)/38-1; xx=11+38*j;
				if(para[j][3]!=0) para[j][3]--;
				mouse(0); parameter_display(j,3);
				send_para(j); mouse(1);
			}
			if(((moux+23)%38<9)&&(mouy>270)&&(mouy<284)){
				j=(moux+23)/38-1; xx=11+38*j;
				if(para[j][3]!=11){
					para[j][3]++;
					mouse(0); parameter_display(j,3);
					send_para(j); mouse(1);
				}
			}
			if(((moux+23)%38<12)&&((mouy+24)%32<8)&&(mouy<240)){
				j=(moux+23)/38-1; xx=11+38*j;
				k=(mouy+24)/32-2;
				if(((para[j][k]!=65)||(k!=4))&&(mouy>140)){
					para[j][k]=0xff&(para[j][k]-1);
					mouse(0); parameter_display(j,k);
					parameter_display(disp,999);
					send_para(j); mouse(1);
				}
			}
			if(((moux+6)%38<12)&&((mouy+19)%32<8)&&(mouy<240)){
				j=(moux+6)/38-1; xx=11+38*j;
				k=(mouy+19)/32-1;
				if(((para[j][k]!=255)||(k!=4))&&(mouy>140)){
					para[j][k]=0xff&(para[j][k]+1);
					mouse(0); parameter_display(j,k);
					parameter_display(disp,999);
					send_para(j); mouse(1);
				}
			}
			if((mouyhigh+4)){
				if((moux-14)%38<15){
					j=(moux-14)/38; xx=11+38*j; k=0;
					mouse(0);
					if(para[j][1]==0){
						if(solo==0) para[j][1]=2;
						else para[j][1]=1;
					}
					else if(para[j][1]==1){
						para[j][1]=0;
					}
					else if(para[j][1]==2){
						para[j][1]=0;
					}
					else if(para[j][1]==3){
						para[j][1]=0; solo=0; k=1;
					}
					parameter_display(j,2); send_para(j);
					if(k==1){
					    for(l=0;l<16;l++){
						if(l==j){}
						else if(para[l][1]==1){
							para[l][1]=2;
							xx=11+38*l;
							parameter_display(l,2);
							send_para(l);
						}
					    }
					}
					mouse(1);
				}
				if((moux+8)%38<15){
					j=(moux+8)/38-1; xx=11+38*j;
					if(para[j][2]<250){
						para[j][2]++; mouse(0);
						parameter_display(j,2);
						send_para(j); mouse(1);
					}
				}
			}
			if((mouy>high+20)&&(mouy1){
						para[j][2]--; mouse(0);
						parameter_display(j,2);
						send_para(j); mouse(1);
					}
				}
			}
		}
		if((old_l==0)&&(left!=0)&&(mouy<99)){
			if((moux>417)&&(moux<427)&&(mouy>11)&&(mouy<21)){
				if(stp>2){
					stp--; mouse(0);
					parameter_display(0,50); mouse(1);
				}
			}
			if((moux>417)&&(moux<427)&&(mouy>85)&&(mouy<96)){
				if(stp<9){
					stp++; mouse(0);
					parameter_display(0,50); mouse(1);
				}
			}
			if((moux>558)&&(moux<617)&&(mouy>80)&&(mouy<96)){
				parameter_init(); break;
			}
			if((moux>431)&&(moux<557)&&(mouy>81)&&(mouy<95)){
				if(back==0){ back=7; front=0; }
				else{ back=0; front=7; }
				screen_display(m);
			}
		}
		old_l=left; old_x=moux; old_y=mouy;
	}
	if(debug==0) parameter_file_write();
	diaplay_setting(1);
}

beep_chaos(x)
	int x;
{
	unsigned int bbx;
	int sh,itb;
	if(x==1){
		b_myu=(double)para[0][4]*_1st;
		b_myu=b_myu+(double)para[0][5]*_2nd;
		b_myu=b_myu+(double)para[0][6]*_3rd;
		beep=b_myu;
	}
	else{
		itb=1+(250-para[0][2])/8;
		if((b_ctr++)

パソコンのC その2 グラニュラシンセシスの実験

#include 
#include 
#include 
#include 
#include 
#include 

#define	ERROR		-1

#define	STATPT  	0xe0d2
#define DATAPT  	0xe0d0
#define DRR     	0x0040
#define DSR     	0x0080
#define PUT		8888
#define GET		9999

#define entry_table		0x0080
#define vector_offset_wave 	1
#define vector_offset_w_block	2
#define vector_offset_user_com	10
#define vector_offset_w_bank   	16

FILE	*fds,*fdo;

double	wc[512];
char	dum[50],name[16];
int	num,ww,wk,kn[16],le[16],vol[16],pt;
unsigned char wb[512],wd[512];

unsigned char patch[594] = {
	0x1C,0x66,0x29,0xEB,0xE1,0xFE,0x37,0x00,0xEB,0xE3,0xFE,0x37,
	0x00,0xEB,0xE2,0xFE,0x37,0x00,0xEB,0xE4,0xFE,0x37,0x00,0x1C,
	0xA4,0x41,0x1C,0x20,0x41,0xC8,0xF8,0x47,0x7F,0x77,0x7D,0xFE,
	0xD6,0x39,0x00,0x70,0xF9,0x70,0xE2,0x28,0xEF,0x76,0x20,0x97,
	0x7F,0xAC,0x80,0xC6,0x04,0x3F,0x7F,0x00,0x00,0xF8,0xAF,0xC6,
	0x1F,0xF8,0x6F,0xF0,0xC7,0x0B,0xF8,0x6F,0xF8,0xFE,0xD6,0xFE,
	0xDF,0x37,0x78,0x00,0x1E,0x20,0xF8,0x6C,0xF0,0xEF,0x78,0x20,
	0x6C,0x0F,0x2F,0x77,0x37,0x7A,0x00,0x1E,0x27,0x78,0x6F,0x00,
	0xFE,0xD6,0xEF,0x7A,0x6F,0x00,0xCE,0x0C,0x6F,0xC0,0xC6,0x0F,
	0x6F,0xD0,0xC6,0x1F,0x37,0x7A,0x01,0x1E,0x37,0x7A,0x00,0x37,
	0x78,0x00,0x1E,0xEF,0x77,0x6F,0x0F,0xFE,0xDE,0x3A,0x00,0x68,
	0xE3,0xE1,0xFE,0x2E,0xF7,0x20,0xE3,0xE1,0xFE,0x87,0x1E,0xEF,
	0x77,0x6F,0x0F,0xFE,0xDE,0x3A,0x00,0x6A,0xE3,0xE3,0xFE,0x2E,
	0xF7,0x20,0xE3,0xE3,0xFE,0x87,0x1E,0xA8,0x62,0xCE,0x08,0xA9,
	0x62,0xFE,0xD6,0xB1,0x62,0xC8,0x08,0xB0,0x62,0x1C,0x46,0x04,
	0x1C,0xBB,0x41,0x27,0xF1,0x1E,0x27,0x5F,0x6C,0xF0,0xA7,0xA7,
	0xA7,0x3A,0xC9,0x41,0xF3,0x4A,0xEA,0xC8,0x58,0x11,0x58,0x11,
	0x58,0x11,0x41,0x12,0xE9,0x12,0xE9,0x12,0xE9,0x12,0xE9,0x12,
	0x7F,0x13,0xDC,0x13,0xE9,0x41,0x8C,0x14,0xF2,0x14,0x3F,0x15,
	0x1C,0x16,0xC3,0x16,0x27,0x5F,0xFE,0xAB,0xEB,0x19,0x14,0xC6,
	0x6C,0x07,0xA6,0x3A,0xFB,0x41,0xF3,0x4A,0xEA,0xC8,0x2D,0x43,
	0x51,0x42,0x0B,0x42,0x2A,0x14,0x33,0x14,0x41,0x14,0x4F,0x14,
	0x84,0x14,0xE3,0xE2,0xFE,0x2E,0xE3,0xE1,0xFE,0x67,0xCE,0x10,
	0xE3,0xE4,0xFE,0x2E,0xE3,0xE3,0xFE,0x67,0xCE,0x1C,0x30,0xFF,
	0x1C,0x48,0x04,0x1E,0x30,0xCF,0x1C,0x48,0x04,0x3A,0x00,0x68,
	0xE3,0xE2,0xFE,0x2E,0xF3,0x28,0xE3,0xE2,0xFE,0x87,0x1C,0x48,
	0x04,0x1E,0x30,0xDF,0x1C,0x48,0x04,0x3A,0x00,0x6A,0xE3,0xE4,
	0xFE,0x2E,0xF3,0x28,0xE3,0xE4,0xFE,0x87,0x1C,0x48,0x04,0x1E,
	0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,0x27,0x60,0xEB,0xE5,
	0xFE,0x26,0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,0x27,0x60,
	0xEB,0xE6,0xFE,0x26,0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,
	0x27,0x60,0xEB,0xE7,0xFE,0x26,0x33,0x00,0x1C,0x87,0x42,0x83,
	0xFB,0x6F,0x10,0xCE,0xF7,0x1E,0x30,0xFA,0xFB,0x31,0xF9,0x6E,
	0xC0,0xE8,0x37,0xF0,0xFB,0x31,0xF9,0x6E,0xD0,0xE8,0x37,0x0F,
	0xE3,0xE6,0xFE,0x28,0x31,0x48,0xF8,0x6A,0x02,0x35,0x20,0xF8,
	0x12,0x30,0x00,0xF8,0x70,0x31,0x08,0xF8,0x70,0xFC,0x6E,0x90,
	0x30,0xFA,0xE2,0x2E,0xFB,0x31,0xE8,0x26,0xFC,0x6C,0x0F,0xFC,
	0x6E,0xA0,0xE2,0x2E,0xFB,0x31,0xF9,0x6E,0x10,0xE8,0x26,0xFC,
	0x6E,0x10,0xE2,0x2E,0xFB,0x31,0xF9,0x6E,0x20,0xE8,0x26,0xFB,
	0x31,0xF9,0x6E,0x30,0xE8,0x37,0x00,0xFB,0x31,0xF9,0x6E,0x40,
	0xE8,0x37,0x00,0xE3,0xE5,0xFE,0x2E,0xFB,0x60,0xFB,0x31,0xF9,
	0x6E,0x50,0xE8,0x26,0xFB,0x31,0xF9,0x6E,0x60,0xE8,0x37,0x80,
	0xFB,0x31,0xF9,0x6E,0x70,0xE8,0x37,0x00,0xFB,0x31,0xF9,0x6E,
	0xA0,0xE8,0x37,0x0F,0xFB,0x31,0xF9,0x6E,0xB0,0xE8,0x37,0x0F,
	0xFB,0x31,0xF9,0x6E,0xC0,0xE3,0xE7,0xFE,0x2E,0xE8,0x26,0xFB,
	0x31,0xF9,0x6E,0xD0,0xE8,0x37,0x0F,0xFB,0x31,0xF9,0x6E,0x90,
	0xE8,0x37,0x03,0x1E,0x33,0x00,0x1C,0x38,0x43,0x83,0xFB,0x6F,
	0x10,0xCE,0xF7,0x30,0xFA,0xFB,0x31,0xF9,0x6E,0xC0,0xE8,0x37,
	0xF0,0xFB,0x31,0xF9,0x6E,0xD0,0xE8,0x37,0x0F,0xFB,0x31,0xF9,
	0x6E,0x90,0xE8,0x37,0x00,0x1E };
unsigned char speed_get_7[66] = {
	0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,0xE7,0x60,0x28,0x27,0xF1,0xA9,
	0x62,0xC6,0xFC,0xB1,0x62,0xE7,0x60,0x29,0x27,0xF1,0xA9,0x62,0xC6,0xFC,
	0xB1,0x62,0xE7,0x60,0x2C,0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,0xE7,
	0x60,0x2D,0x7F,0x00,0x00,0xFE,0xD6,0xAC,0xC6,0xC6,0xFC,0xE0,0x2E,0x2F,
	0xFE,0x27,0xF0,0x90,0x7A,0x01,0x00,0xCE,0xF0,0x1E };
unsigned char speed_put_8[68] = {
	0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,0xE7,0x60,0x28,0x27,0xF1,0xA9,
	0x62,0xC6,0xFC,0xB1,0x62,0xE7,0x60,0x29,0x27,0xF1,0xA9,0x62,0xC6,0xFC,
	0xB1,0x62,0xE7,0x60,0x2C,0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,0xE7,
	0x60,0x2D,0x7F,0x00,0x00,0xFE,0xD6,0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,
	0x62,0x27,0x60,0xE8,0x26,0x90,0x7A,0x01,0x00,0xCE,0xEE,0x1E };
unsigned char volume[32] = {
	0x7a,0x6a,0x5a,0x5e,0x48,0x4a,0x4c,0x4e,
	0x38,0x3a,0x2c,0x3e,0x28,0x2a,0x2c,0x2e,
	0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
	0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f };

unsigned get_entry(x) int x; { unsigned ent,z; z=(unsigned)x;
	ent=msb_data_get(entry_table)+msb_data_get(entry_table+1)*256;
	return(msb_data_get(ent+2*z)+msb_data_get(ent+2*z+1)*256); }
msb_data_put(add,data) unsigned add; int data; {
	set_command(0xe3); put_data(add/256); set_command(0xe5);
	put_data(add%256); set_command(0xeb); put_data(data); }
msb_data_get(add) unsigned add; {
	set_command(0xe3); put_data(add/256); set_command(0xe5);
	put_data(add%256); set_command(0xae); return(get_data()); }
msb_hot_reset(){ inp(DATAPT); inp(DATAPT); inp(DATAPT); set_command(0xff); }
put_command(x) int x; { while((inp(STATPT)&DRR)!=0){} outp(STATPT,x); }
put_data(x) int x; { while((inp(STATPT)&DRR)!=0){} outp(DATAPT,x); }
get_data(){ while((inp(STATPT)&DSR)!=0){} return(inp(DATAPT)); }
set_command(x) int x; { put_command(x); while(get_data()!=0xfe){} }
turbo_command_set(x,add,count) int x; unsigned add,count; {
	if(x==PUT) set_command(0xcd);
	else if(x==GET) set_command(0xcc);
	put_data(add/256); put_data(add%256);
	put_data(count/256); put_data(count%256); }

wave_draw(z,mode)
	int z,mode;
{
	unsigned add;
	int i,j,xx,yy,xxx,yyy,tx,ty,dt,yyyy,d;
	xx=24+336*(z/8); xxx=xx+285; yy=9+48*(z%8); yyy=yy+43;
	_setcolor(0); _rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy); _setcolor(7);
	if(mode==0) _setlinestyle(0x9999);
	else if(mode==1) _setlinestyle(0xffff);
	xx=25+336*(z/8); xxx=xx+128; yy=10+48*(z%8); yyy=yy+42;
	_rectangle(_GBORDER,xx,yy,xxx,yyy);
	xx=157+336*(z/8); xxx=xx+145; yy=34+48*(z%8); yyy=yy+18;
	_rectangle(_GBORDER,xx,yy,xxx,yyy);
	ty=2+3*(z%8); tx=42*(z/8); _settextposition(ty,tx); printf("[%X]",z);
	_settextposition(ty-1,tx+20); printf(" KN=%02x  LE=%02x",kn[z],le[z]);
	xx=26+336*(z/8); xxx=xx+kn[z]; yy=14+48*(z%8); yyy=yy+4;
	_setlinestyle(0x5555); _rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy);

	xx=26+336*(z/8); xxx=2+xx+4*vol[z]; yy=44+48*(z%8); yyy=yy+4;
	_setlinestyle(0x5555); _rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy);

	xx=158+336*(z/8); xxx=xx+143*le[z]/127; yy=35+48*(z%8); yyy=yy+16;
	_setlinestyle(0x5555); _rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy);
	xx=25+336*(z/8); xxx=xx+128; yy=10+48*(z%8); yyy=yy+42;
	_setlinestyle(0xffff);
	for(i=0;i<512;i=i+4){
		if(i==0) _moveto(xx,yyy-14);
		else{
			if(wb[i]<128) dt=wb[i]+128;
			else dt=128-(wb[i]&0x7f);
			yyyy=yyy-56*dt/256+14;
			if(yyyy>yyy) yyyy=yyy;
			_lineto(xx+i/4,yyyy);
		}
		if (i==508) _lineto(xx+127,yyy-14);
	}
	if(mode==1){
		d=le[z];
		if(d>127-16) d=127-16;
		set_command(0xa9);
		put_data(d);
		put_data(kn[z]);
		put_data(volume[vol[z]]);
	}
}

wave_calc(ww,wk)
	int ww,wk;
{
	int i,wl,a,b;
	double p=3.1415926535,q,r,u,v=0;
	if(ww<16) wl=512-24*(16-ww);
	else wl=512-24*(ww-16);
	for(i=0;i<512;i++){
		if(i=0) wd[i]=(char)u;
		else wd[i]=(char)(128-u);
	}
	if(ww<16){
		a=(512-wl)/2; b=a+wl; for(i=0;i16){
		a=wl/2; b=a+(512-wl); for(i=0;i'){ /* Wave Width +1 */
			if(ww!=31){ flg=1; ww++; break; }
		}
		else if(c=='<'){ /* Wave Width -1 */
			if(ww!=0){ flg=1; ww--; break; }
		}
		else if(c=='+'){ /* Wave Type +1 */
			if(wk!=31){ flg=1; wk++; break; }
		}
		else if(c=='-'){ /* Wave Type -1 */
			if(wk!=0){ flg=1; wk--; break; }
		}
		else if((c=='d')||(c=='D')){ /* Volume Down */
			if(vol[pt]!=0){ vol[pt]--; wave_draw(pt,1); }
		}
		else if((c=='u')||(c=='U')){ /* Volume Up */
			if(vol[pt]!=31){ vol[pt]++; wave_draw(pt,1); }
		}
	}
	if(flg!=0){ flg=0; goto _loop_loop; }
	parameter_file_write();
	_clearscreen(_GCLEARSCREEN); _displaycursor(_GCURSORON);
	set_command(0xa8);
}

パソコンのC その3 グラフィカル・カオスシンセサイザ

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma check_stack(off)
#pragma intrinsic(_disable,_enable)
#pragma function(inp,outp)
#define _GB 2
#define _GF 3

void interrupt far new_0a(void);
void (interrupt far *old_0a)(void);
union REGS inregs,outregs;
struct SREGS segregs;
int moux,mouy,right,left,old_l,old_r,old_x,old_y;
FILE *fds,*fdo;
char far wind[14000];
long vsync_counter;
double myu[8],chaos[8];
int volume[8],filter[8][12],midich[8],shift[8],range[2][8],tim[2][8];
int down[8],program[8],panpot[8],para1[8],para2[8],ch_trig;
int section,edit_ch,note[8],velocity[8],phase[8],counter[8],sum[8];
int data_buffer[30][8][9],fifo_pointer,read_pointer;
unsigned char ref_table[8][3200],midi_fifo[4][10000];

mouse(flag) int flag; {
	if(flag==0){inregs.x.ax=2;} else if(flag==1){inregs.x.ax=1;}
	int86(51,&inregs,&outregs); }
getmouse(right,left,x,y) int *right,*left,*x,*y; { inregs.x.ax=3;
	int86(51,&inregs,&outregs); *right=outregs.x.bx;
	*left=outregs.x.ax; *x=outregs.x.cx; *y=outregs.x.dx; }
setmouse(x,y) int x,y; { inregs.x.ax=4; inregs.x.cx=x; inregs.x.dx=y;
	int86(51,&inregs,&outregs); }
m_style(x,y,buf) int x,y; char *buf; { inregs.x.ax=9; inregs.x.bx=x;
	inregs.x.cx=y; segread(&segregs); segregs.es=segregs.ds;
	inregs.x.dx=(int)buf; int86x(51,&inregs,&outregs,&segregs); }
m_view(xmin,ymin,xmax,ymax) int xmin,ymin,xmax,ymax; { inregs.x.ax=16;
	inregs.x.cx=xmin; inregs.x.dx=xmax; int86(51,&inregs,&outregs);
	inregs.x.ax=17; inregs.x.cx=ymin; inregs.x.dx=ymax;
	int86(51,&inregs,&outregs); }
m_color(color) int color; {
	inregs.x.ax=18; inregs.x.bx=color; int86(51,&inregs,&outregs); }
symbol(x,y,color,str,skip) int x,y,color,skip; unsigned char str[80]; {
	int i=0; _setcolor(color);
	while(str[i]!=0){ _gputchar(x,y,str[i++],_GXOR); x += skip; } }
int style[]={ 0,0,0,0,0,0,0,0,0x0070,0x0e88,0x1184,0x2142,0x2221,0x9210,
	0xe107,0x1108,0x1108,0xe107,0x1108,0x1108,0xe107,0x4108,0x4208,
	0xfc07,0,0,0,0,0,0,0,0 };
put_command(x) int x; { while((inp(0xe0d2)&0x40)!=0){} outp(0xe0d2,x); }
put_data(x) int x; { while((inp(0xe0d2)&0x40)!=0){} outp(0xe0d0,x); }
get_data(){ while((inp(0xe0d2)&0x80)!=0){} return(inp(0xe0d0)); }
set_command(x) int x; { put_command(x); while(get_data()!=0xfe){} }
msb_hot_reset(){ inp(0xe0d0); inp(0xe0d0); inp(0xe0d0); set_command(0xff); }
vsync_int_start(){ if(old_0a!=NULL) return;
	old_0a=_dos_getvect(0x0a); _dos_setvect(0x0a,new_0a);
	outp(0x64,0); _disable(); outp(0x02,inp(0x02)&0xfb); _enable();
	signal(SIGINT,SIG_IGN); }
vsync_int_stop(){ if(old_0a==NULL) return;
	_disable(); outp(0x02,inp(0x02)|0x04); _enable();
	_dos_setvect(0x0a,old_0a); old_0a=NULL;	signal(SIGINT,SIG_DFL); }
void interrupt far new_0a(){ outp(0x64,0);
	vsync_counter++; _disable(); _chain_intr(old_0a); }

main(){
	int i,j,d,ch,m_ctr,rsb,dcb,keyno,m_m,m_stat=0;
	int job_fg[8],job_ct=0,run_flag=0,pause_pass=0,bar_disp=0;
	inregs.x.ax=0; int86(51,&inregs,&outregs);
	if(outregs.x.ax==0){printf("\nPlease install mouse.sys !\n");exit(0);}
	m_style(0,8,style); m_color(1); m_view(0,0,639,399); setmouse(320,200);
	msb_hot_reset();
	set_command(0xf0); set_command(0xfd); set_command(0x3f);
	for(i=0;i<4096;i++){ for(j=0;j<200;j++){} inp(0xe0d0); }
	for(d=0;d<8;d++){
		midich[d]=d; velocity[d]=0;
		phase[d]=0; counter[d]=0; chaos[d]=0.5; job_fg[d]=0;
	}
	_setvideomode(_98RESS16COLOR); _displaycursor(_GCURSOROFF);
	_clearscreen(_GCLEARSCREEN);
	parameter_file_read();
	section=1; screen_display(); edit_ch=0;
	fifo_pointer=0; read_pointer=0;
	mouse(1); getmouse(&old_r,&old_l,&old_x,&old_y);
	vsync_counter=0; vsync_int_start();
	while(1){
		if(vsync_counter!=0){
			vsync_counter=0;
			if(run_flag!=2) put_data(0xf8);
			for(ch=0;ch<8;ch++) job_fg[ch]=1;
		}
		if((inp(0xe0d2)&0x80)==0){
			d=inp(0xe0d0);
			if(d>0xef){}
			else if(d>0x7f){ rsb=d; dcb=0; }
			else if(rsb==0xcf){
				if((run_flag==1)&&((d==0x7f)||(d==0))){
					put_data(0xfa);
					run_flag=0;
					window_draw(0);
				}
				else if((run_flag==2)&&((d==0x7f)||(d==0))){
					run_flag=0;
					window_draw(0);
				}
				else if((run_flag==3)&&((d==0x7f)||(d==0))){
					d=ch_trig;
					if(++ch_trig>4) ch_trig=1;
					phase[d]=3;
					if(volume[d]!=0){
						velocity[d]=0x7f;
					}
				}
			}
			else if(rsb==0xdf){
				if((run_flag==1)&&((d==0x7f)||(d==0))){
					put_data(0xfa);
					run_flag=0;
					window_draw(0);
				}
				else if((run_flag==2)&&((d==0x7f)||(d==0))){
					run_flag=0;
					window_draw(0);
				}
				else if((run_flag==3)&&((d==0x7f)||(d==0))){
					d=ch_trig;
					if(++ch_trig>4) ch_trig=1;
					phase[d]=3;
					if(volume[d]!=0){
						velocity[d]=0x7f;
					}
				}
			}
			else if(rsb==0xaf){
				if(dcb==0){ keyno=d; dcb=1; }
				else{
					dcb=0;
					midi_display(1,rsb,keyno,d,0);
					if(keyno==60){
						if((d>0)&&(d<10)){
							if(run_flag==1){
								run_flag=0;
								window_draw(0);
								pause_pass=1;
							}
							for(i=0;i<8;i++){
							    chaos[d]=0.5;
							    velocity[i]=0;
							}
							section=d;
							screen_display();
						}
					}
					else if(keyno==61){
						if((d>0)&&(d<9)){
							phase[d-1]=1;
							if(volume[d-1]!=0){
							    velocity[d-1]=0x7f;
							}
						}
					}
					else if(keyno==62) edit_ch=d;
					else if((keyno>62)&&(keyno<93)){
						edit_midi_rx(keyno,d);
					}
					else if((keyno==93)&&(pause_pass==0)){
						run_flag=2;
						window_draw(1);
					}
					else if(keyno==94){
						if(d==0) run_flag=0;
						else run_flag=3;
						ch_trig=1;
					}
					else if(keyno==95){
						if(d==0) bar_disp=0;
						else{
							bar_disp=d;
							bar_display(d);
						}
					}
				}
			}
			else if((rsb>0xbf)&&(rsb<0xcf)){
				midi_display(3,rsb,d,0,0);
			}
			else if((rsb>0x8f)&&(rsb<0xa0)){
				if(dcb==0){ keyno=d; dcb=1; }
				else{
					dcb=0;
					if(d!=0) midi_display(0,rsb,keyno,d,0);
				}
			}
			else if((rsb>0xaf)&&(rsb<0xc0)){
				if(dcb==0){ keyno=d; dcb=1; }
				else{
					dcb=0;
					midi_display(2,rsb,keyno,d,0);
					if((rsb==0xbf)&&(keyno==0x7f)&&(d==0)){
						if(pause_pass==0){
							run_flag=1;
							window_draw(1);
						}
					}
				}
			}
		}
		else if(job_fg[job_ct]!=0){
			each_channel(job_ct);
			job_fg[job_ct]=0;
			job_ct=(job_ct+1)&0x07;
		}
		else if(kbhit()!=0){
			d=getch();
			if(d==0x1b) break;
			else if((d==0x20)||(d==0x0d)){
				if(run_flag==1){
					put_data(0xfa);
					run_flag=0;
					window_draw(0);
				}
				else if(run_flag==2){
					run_flag=0;
					window_draw(0);
				}
			}
			else if((d>0x30)&&(d<0x3a)){
				for(ch=0;ch<8;ch++) velocity[ch]=0;
				parameter_to_data_buffer();
				section=d-0x30; screen_display();
			}
			else if((d=='c')||(d=='C')) put_data(0xfc);
			else if((d=='b')||(d=='B')) put_data(0xfb);
			else if(d=='A'){ put_data(0xfa); pause_pass=0; }
		}
		else{
			getmouse(&right,&left,&moux,&mouy);
			if((right==0)&&(left==0)) m_stat=0;
			else if((old_l==0)&&(left!=0)){
				m_stat=1; m_ctr=0; m_m=1;
			}
			else if((old_r==0)&&(right!=0)){
				m_stat=2; m_ctr=0; m_m=1;
			}
			else if(left!=0){
				if(++m_ctr>30){ m_stat=1; m_ctr=0; m_m=0; }
			}
			else if(right!=0){
				if(++m_ctr>30){ m_stat=2; m_ctr=0; m_m=0;}
			}
			if(m_stat==1){ m_stat=0; m_stat_1(m_m); }
			else if(m_stat==2){ m_stat=0; m_stat_2(m_m); }
			old_r=right; old_l=left; old_x=moux; old_y=mouy;
			if(bar_disp==0){
				_settextposition(12,66);
				printf("(X=%03d,Y=%03d)",moux,mouy);
			}
			midi_display(0,0,0,0,1);
		}
	}
	vsync_int_stop(); mouse(0);
	for(i=0;i<8;i++){
		put_data(0x80+midich[i]); put_data(note[i]); put_data(0);
	}
	put_data(0xfc);
	for(i=0;i<16;i++){ put_data(0xb0+i); put_data(0x7f); put_data(0); }
	put_command(0xff);
	parameter_to_data_buffer(); parameter_file_write();
	for(i=0;i<4096;i++){ for(j=0;j<200;j++){} inp(0xe0d0); }
	_clearscreen(_GCLEARSCREEN); _displaycursor(_GCURSORON);
	_setvideomode (_DEFAULTMODE);
}

m_stat_1(m_m)
	int m_m;
{
	if((mouy>176)&&(mouy<189)) volume_change(0,m_m);
	else if((mouy>384)&&(mouy<397)) volume_change(4,m_m);
	else if((mouy>114)&&(mouy<128)) range_change(0,0);
	else if((mouy>322)&&(mouy<336)) range_change(4,0);
	else if((mouy>98)&&(mouy<112)) time_change(0,0);
	else if((mouy>306)&&(mouy<320)) time_change(4,0);
	else if((mouy>82)&&(mouy<96)) dwn_prg_change(0,0);
	else if((mouy>290)&&(mouy<304)) dwn_prg_change(4,0);
	else if((mouy>50)&&(mouy<64)) myu_change(0,0,m_m);
	else if((mouy>258)&&(mouy<272)) myu_change(4,0,m_m);
	else if((mouy>158)&&(mouy<174)) map_change(0,0);
	else if((mouy>366)&&(mouy<382)) map_change(4,0);
	else if((mouy>66)&&(mouy<80)) p1_p2_change(0,0);
	else if((mouy>274)&&(mouy<288)) p1_p2_change(4,0);
	else if((mouy>34)&&(mouy<48)) volpan_change(0,0);
	else if((mouy>242)&&(mouy<256)) volpan_change(4,0);
	else if((mouy>0)&&(mouy<16)) channel_trigger(0,m_m);
	else if((mouy>208)&&(mouy<224)) channel_trigger(4,m_m);
}

m_stat_2(m_m)
	int m_m;
{
	if((mouy>176)&&(mouy<189)) volume_change(0,m_m);
	else if((mouy>384)&&(mouy<397)) volume_change(4,m_m);
	else if((mouy>114)&&(mouy<128)) range_change(0,1);
	else if((mouy>322)&&(mouy<336)) range_change(4,1);
	else if((mouy>98)&&(mouy<112)) time_change(0,1);
	else if((mouy>306)&&(mouy<320)) time_change(4,1);
	else if((mouy>82)&&(mouy<96)) dwn_prg_change(0,1);
	else if((mouy>290)&&(mouy<304)) dwn_prg_change(4,1);
	else if((mouy>50)&&(mouy<64)) myu_change(0,1,m_m);
	else if((mouy>258)&&(mouy<272)) myu_change(4,1,m_m);
	else if((mouy>158)&&(mouy<174)) map_change(0,1);
	else if((mouy>366)&&(mouy<382)) map_change(4,1);
	else if((mouy>66)&&(mouy<80)) p1_p2_change(0,1);
	else if((mouy>274)&&(mouy<288)) p1_p2_change(4,1);
	else if((mouy>34)&&(mouy<48)) volpan_change(0,1);
	else if((mouy>242)&&(mouy<256)) volpan_change(4,1);
}

point_screen(mode,y,midi)
	int mode,y,midi;
{
	char ss[5];
	int i,j,k,l,xx,yy,xt,yt,xx_b,yy_b;
	xt=1+(y%4)*20; yt=(y/4)*13; xx=1+(y%4)*160; yy=1+(y/4)*208;
	if(mode==0){
		mouse(0); ss[0]='A'+y; ss[1]=0;
		_setlinestyle(0xffff); _setcolor(0);
		_rectangle(_GF,xx,yy,xx+157,yy+190); _setcolor(7);
		_rectangle(_GB,xx,yy,xx+157,yy+190);
		_rectangle(_GF,xx,yy,xx+157,yy+14);
		_settextposition(yt,xt); printf("\x1b[30m");
		printf("   == %s ==   ch:%d ",ss,midich[y]+1);
		printf("\x1b[37m"); mouse(1);
	}
	else if(mode==1){
		mouse(0); _setlinestyle(0xffff); _setcolor(0);
		_rectangle(_GF,xx+1,yy+173,xx+156,yy+189); _setcolor(7);
		xx_b=xx+6+(volume[y]/2)*143/64; yy_b=yy+176;
		_moveto(xx+7,yy_b+5); _lineto(xx+150,yy_b+5);
		_moveto(xx+7,yy_b+6); _lineto(xx+150,yy_b+6);
		i=0; _moveto(xx+7+11*i,yy_b+2); _lineto(xx+7+11*i,yy_b+9);
		i=13; _moveto(xx+7+11*i,yy_b+2); _lineto(xx+7+11*i,yy_b+9);
		for(i=1;i<13;i++){
			_moveto(xx+7+11*i,yy_b+3); _lineto(xx+7+11*i,yy_b+8);
		}
		_rectangle(_GF,xx_b,yy_b,xx_b+5,yy_b+11); _setcolor(0);
		_rectangle(_GF,xx_b+1,yy_b+1,xx_b+4,yy_b+10); _setcolor(7);
		_rectangle(_GF,xx_b+2,yy_b+2,xx_b+3,yy_b+9);
		_settextposition(yt+2,xt);
		printf(" vol=%03d  pan=%03d",volume[y],panpot[y]);
		if(midi==0){
			put_data(0xb0+midich[y]);
			put_data(0x07); put_data(volume[y]);
			put_data(0xb0+midich[y]); put_data(0x0A);
			put_data(panpot[y]);
		}
		mouse(1);
	}
	else if(mode==2){
		mouse(0); _setlinestyle(0xffff); _setcolor(0);
		_rectangle(_GF,xx+13,yy+131,xx+144,yy+157); _setcolor(7);
		_setlinestyle(0x6666);
		_rectangle(_GB,xx+12,yy+130,xx+145,yy+158);
		_setlinestyle(0xffff);
		for(i=0;i<7;i++){
			_rectangle(_GB,xx+12+19*i,yy+158,xx+31+19*i,yy+172);
		}
		yy_b=yy+159;
		xx_b=xx+31; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		xx_b=xx+50; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		xx_b=xx+88; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		xx_b=xx+107; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		xx_b=xx+126; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		yy_b=yy+156;
		_rectangle(_GB,xx+20,yy_b,xx+23,yy_b-filter[y][0]);
		_rectangle(_GF,xx+29,yy_b,xx+33,yy_b-filter[y][1]);
		_rectangle(_GB,xx+39,yy_b,xx+42,yy_b-filter[y][2]);
		_rectangle(_GF,xx+48,yy_b,xx+52,yy_b-filter[y][3]);
		_rectangle(_GB,xx+58,yy_b,xx+61,yy_b-filter[y][4]);
		_rectangle(_GB,xx+77,yy_b,xx+80,yy_b-filter[y][5]);
		_rectangle(_GF,xx+86,yy_b,xx+90,yy_b-filter[y][6]);
		_rectangle(_GB,xx+96,yy_b,xx+99,yy_b-filter[y][7]);
		_rectangle(_GF,xx+105,yy_b,xx+109,yy_b-filter[y][8]);
		_rectangle(_GB,xx+115,yy_b,xx+118,yy_b-filter[y][9]);
		_rectangle(_GF,xx+124,yy_b,xx+128,yy_b-filter[y][10]);
		_rectangle(_GB,xx+134,yy_b,xx+137,yy_b-filter[y][11]);
		mouse(1);
	}
	else if(mode==3){
		_settextposition(yt+7,xt);
		printf(" low=%03d high=%03d",range[0][y],range[1][y]);
	}
	else if(mode==4){
		_settextposition(yt+6,xt);
		printf(" dly=%03d  stp=%03d",tim[0][y],tim[1][y]);
	}
	else if(mode==5){
		_settextposition(yt+5,xt);
		printf(" dwn=%03d  prg=%03d",down[y],program[y]);
		if(midi==0){
			put_data(0xc0+midich[y]); put_data(program[y]);
		}
	}
	else if(mode==6){
		_settextposition(yt+3,xt);
		printf(" myu=%f <%d>",myu[y],shift[y]+1);
	}
	else if(mode==7){
		_settextposition(yt+1,xt);
		printf(" note=%03d vel=%03d",note[y],velocity[y]);
	}
	else if(mode==8){
		_settextposition(yt+4,xt);
		printf(" pp1=%03d  pp2=%03d",para1[y],para2[y]);
	}
	if((mode==2)||(mode==3)){
		sum[y]=0; j=0;
		for(i=range[0][y];i0)&&(moux<159)) of=0;
	else if((moux>160)&&(moux<319)) of=1;
	else if((moux>320)&&(moux<479)) of=2;
	else if((moux>480)&&(moux<639)) of=3;
	else return(0);
	y=off+of;
	phase[y]=1;
	if(volume[y]!=0) velocity[y]=0x7f;
}

volume_change(off,m_m)
	int off,m_m;
{
	int of,y,x;
	if(m_m==0) return(0);
	if((moux>7)&&(moux<152)) { of=0; x=8; }
	else if((moux>167)&&(moux<312)) { of=1; x=168; }
	else if((moux>327)&&(moux<472)) { of=2; x=328; }
	else if((moux>487)&&(moux<632)) { of=3; x=488; }
	else return(0);
	y=off+of;
	volume[y]=(moux-x)*127/143;
	point_screen(1,y,0);	/* Volume */
}

range_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>80)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>240)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>400)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>560)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(range[0][y]>0) range[0][y]--; }
	else if((z==0)&&(md==1)){ if(range[0][y]<127) range[0][y]++; }
	else if((z==1)&&(md==0)){ if(range[1][y]>0) range[1][y]--; }
	else if((z==1)&&(md==1)){ if(range[1][y]<127) range[1][y]++; }
	point_screen(3,y,0);	/* Range */
}

time_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>87)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>247)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>407)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>567)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(tim[0][y]>0) tim[0][y]--; }
	else if((z==0)&&(md==1)){ if(tim[0][y]<127) tim[0][y]++; }
	else if((z==1)&&(md==0)){ if(tim[1][y]>0) tim[1][y]--; }
	else if((z==1)&&(md==1)){ if(tim[1][y]<127) tim[1][y]++; }
	point_screen(4,y,0);	/* Delay, Step */
}

dwn_prg_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>87)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>247)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>407)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>567)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(down[y]>0) down[y]--; }
	else if((z==0)&&(md==1)){ if(down[y]<127) down[y]++; }
	else if((z==1)&&(md==0)){ if(program[y]>0) program[y]--; }
	else if((z==1)&&(md==1)){ if(program[y]<127) program[y]++; }
	point_screen(5,y,0);	/* Down, Program */
}

p1_p2_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>87)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>247)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>407)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>567)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(para1[y]>0) para1[y]--; }
	else if((z==0)&&(md==1)){ if(para1[y]<127) para1[y]++; }
	else if((z==1)&&(md==0)){ if(para2[y]>0) para2[y]--; }
	else if((z==1)&&(md==1)){ if(para2[y]<127) para2[y]++; }
	point_screen(8,y,0);	/* Parameters */
}

volpan_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>87)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>247)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>407)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>567)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(volume[y]>0) volume[y]--; }
	else if((z==0)&&(md==1)){ if(volume[y]<127) volume[y]++; }
	else if((z==1)&&(md==0)){ if(panpot[y]>0) panpot[y]--; }
	else if((z==1)&&(md==1)){ if(panpot[y]<127) panpot[y]++; }
	point_screen(1,y,0);	/* Volume, Panpot */
}

myu_change(off,md,m_m)
	int off,md,m_m;
{
	int of,y,z;
	if((moux>15)&&(moux<111)) { of=0; z=0; }
	else if((moux>120)&&(moux<142)) { of=0; z=1; }
	else if((moux>175)&&(moux<271)) { of=1; z=0; }
	else if((moux>280)&&(moux<302)) { of=1; z=1; }
	else if((moux>335)&&(moux<431)) { of=2; z=0; }
	else if((moux>440)&&(moux<462)) { of=2; z=1; }
	else if((moux>495)&&(moux<591)) { of=3; z=0; }
	else if((moux>600)&&(moux<622)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){
		if(shift[y]==0) myu[y]=myu[y]-0.1;
		else if(shift[y]==1) myu[y]=myu[y]-0.01;
		else if(shift[y]==2) myu[y]=myu[y]-0.001;
		else if(shift[y]==3) myu[y]=myu[y]-0.0001;
		else if(shift[y]==4) myu[y]=myu[y]-0.00001;
		else if(shift[y]==5) myu[y]=myu[y]-0.000001;
		if(myu[y]<1.0) myu[y]=1.0;
	}
	else if((z==0)&&(md==1)){
		if(shift[y]==0) myu[y]=myu[y]+0.1;
		else if(shift[y]==1) myu[y]=myu[y]+0.01;
		else if(shift[y]==2) myu[y]=myu[y]+0.001;
		else if(shift[y]==3) myu[y]=myu[y]+0.0001;
		else if(shift[y]==4) myu[y]=myu[y]+0.00001;
		else if(shift[y]==5) myu[y]=myu[y]+0.000001;
		if(myu[y]>4.0) myu[y]=4.0;
	}
	else if((m_m!=0)&&(z==1)&&(md==0)){ if(shift[y]>0) shift[y]--; }
	else if((m_m!=0)&&(z==1)&&(md==1)){ if(shift[y]<5) shift[y]++; }
	point_screen(6,y,0);	/* Myu, Shift */
}

map_change(off,md)
	int off,md;
{
	int f,of,y,z,xx,yy;
	if(off==0) yy=mouy;
	else yy=mouy-208;
	if((moux>13)&&(moux<147)) { of=0; xx=moux; }
	else if((moux>172)&&(moux<307)) { of=1; xx=moux-160; }
	else if((moux>332)&&(moux<467)) { of=2; xx=moux-320; }
	else if((moux>492)&&(moux<627)) { of=3; xx=moux-480; }
	else return(0);
	if((yy<168)&&(xx>25)&&(xx<38)) z=1;
	else if((yy<168)&&(xx>44)&&(xx<58)) z=3;
	else if((yy<168)&&(xx>82)&&(xx<96)) z=6;
	else if((yy<168)&&(xx>101)&&(xx<115)) z=8;
	else if((yy<168)&&(xx>120)&&(xx<134)) z=10;
	else if(xx<32) z=0;
	else if(xx<51) z=2;
	else if(xx<70) z=4;
	else if(xx<89) z=5;
	else if(xx<108) z=7;
	else if(xx<127) z=9;
	else z=11;
	y=off+of;
	if(md==0){ if(filter[y][z]>0) filter[y][z]--; }
	else { if(filter[y][z]<24) filter[y][z]++; }
	point_screen(2,y,0);	/* Note Map */
}

parameter_file_write(){
	int i,j,k;
	fds=fopen("chaosgen.dat","wb");
	for(i=0;i<9;i++){
		for(j=0;j<8;j++){
			for(k=0;k<30;k++) fputc(data_buffer[k][j][i],fds);
			fputc(0xff,fds);
			fputc(0xff,fds);
		}
	}
	fclose(fds);
}

parameter_file_read(){
	int i,j,k;
	if((fds=fopen("chaosgen.dat","rb"))==NULL){
		for(i=0;i<9;i++){
			for(j=0;j<8;j++){
				data_buffer[0][j][i]=3; /* Myu over point */
				data_buffer[1][j][i]=j; /* Myu (1) */
				data_buffer[2][j][i]=j; /* Myu (2) */
				data_buffer[3][j][i]=j; /* Myu (3) */
				data_buffer[4][j][i]=j; /* Myu (4) */
				data_buffer[5][j][i]=j; /* Myu (5) */
				data_buffer[6][j][i]=j; /* Myu (6) */
				data_buffer[7][j][i]=1; /* shift */
				data_buffer[8][j][i]=1+2*j; /* down */
				data_buffer[9][j][i]=2*j+1; /* program */
				data_buffer[10][j][i]=64+2*j; /* volume */
				data_buffer[11][j][i]=64; /* panpot */
				data_buffer[12][j][i]=99; /* para(1) */
				data_buffer[13][j][i]=99; /* para(2) */
				data_buffer[14][j][i]=36-j; /* range[0] low */
				data_buffer[15][j][i]=84+j; /* range[1] high */
				data_buffer[16][j][i]=j; /* tim[0] Delay */
				data_buffer[17][j][i]=1+j*2; /* tim[1] Step */
				data_buffer[18][j][i]=12; /* filter[0] */
				data_buffer[19][j][i]=12; /* filter[1] */
				data_buffer[20][j][i]=12; /* filter[2] */
				data_buffer[21][j][i]=12; /* filter[3] */
				data_buffer[22][j][i]=12; /* filter[4] */
				data_buffer[23][j][i]=12; /* filter[5] */
				data_buffer[24][j][i]=12; /* filter[6] */
				data_buffer[25][j][i]=12; /* filter[7] */
				data_buffer[26][j][i]=12; /* filter[8] */
				data_buffer[27][j][i]=12; /* filter[9] */
				data_buffer[28][j][i]=12; /* filter[10] */
				data_buffer[29][j][i]=12; /* filter[11] */
			}
		}
	}
	else{
		for(i=0;i<9;i++){
			for(j=0;j<8;j++){
				for(k=0;k<30;k++){
					data_buffer[k][j][i]=fgetc(fds);
				}
				fgetc(fds);
				fgetc(fds);
			}
		}
		fclose(fds);
	}
}

screen_display(){
	int i,j,k,m0,m1,m2,m3,m4,m5,m6;
	i=section-1;
	for(j=0;j<8;j++){
		m0=data_buffer[0][j][i]; /* Myu over point */
		m1=data_buffer[1][j][i]; /* Myu (1) */
		m2=data_buffer[2][j][i]; /* Myu (2) */
		m3=data_buffer[3][j][i]; /* Myu (3) */
		m4=data_buffer[4][j][i]; /* Myu (4) */
		m5=data_buffer[5][j][i]; /* Myu (5) */
		m6=data_buffer[6][j][i]; /* Myu (6) */
		myu[j]=(double)m0;
		myu[j]=myu[j]+0.1*(double)m1;
		myu[j]=myu[j]+0.01*(double)m2;
		myu[j]=myu[j]+0.001*(double)m3;
		myu[j]=myu[j]+0.0001*(double)m4;
		myu[j]=myu[j]+0.00001*(double)m5;
		myu[j]=myu[j]+0.000001*(double)m6;
		shift[j]=data_buffer[7][j][i]; /* shift */
		down[j]=data_buffer[8][j][i]; /* down */
		program[j]=data_buffer[9][j][i]; /* program */
		volume[j]=data_buffer[10][j][i]; /* volume */
		panpot[j]=data_buffer[11][j][i]; /* panpot */
		para1[j]=data_buffer[12][j][i]; /* para(1) */
		para2[j]=data_buffer[13][j][i]; /* para(2) */
		range[0][j]=data_buffer[14][j][i]; /* range[0] low */
		range[1][j]=data_buffer[15][j][i]; /* range[1] high */
		tim[0][j]=data_buffer[16][j][i]; /* tim[0] Delay */
		tim[1][j]=data_buffer[17][j][i]; /* tim[1] Step */
		for(k=0;k<12;k++){
			filter[j][k]=data_buffer[18+k][j][i]; /* filter[k] */
		}
	}
	for(i=0;i<8;i++){
		point_screen(0,i,0);	/* Whole Frame */
		point_screen(7,i,0);	/* Note, Velocity */
		point_screen(6,i,0);	/* Myu, Shift */
		point_screen(8,i,0);	/* Parameters */
		point_screen(1,i,0);	/* Volume, Panpot */
		point_screen(5,i,0);	/* Down, Program */
		point_screen(4,i,0);	/* Delay, Step */
		point_screen(3,i,0);	/* Range */
		point_screen(2,i,0);	/* Note Map */
	}
	_settextposition(12,1);
	if(section==1)      printf("【1】2  3  4  5  6  7  8  9  ");
	else if(section==2) printf("  1【2】3  4  5  6  7  8  9  ");
	else if(section==3) printf("  1  2【3】4  5  6  7  8  9  ");
	else if(section==4) printf("  1  2  3【4】5  6  7  8  9  ");
	else if(section==5) printf("  1  2  3  4【5】6  7  8  9  ");
	else if(section==6) printf("  1  2  3  4  5【6】7  8  9  ");
	else if(section==7) printf("  1  2  3  4  5  6【7】8  9  ");
	else if(section==8) printf("  1  2  3  4  5  6  7【8】9  ");
	else if(section==9) printf("  1  2  3  4  5  6  7  8【9】");
}

parameter_to_data_buffer(){
	double myu_buff;
	int i,j,k,m;
	i=section-1;
	for(j=0;j<8;j++){
		myu_buff=myu[j];
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[0][j][i]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[1][j][i]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[2][j][i]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[3][j][i]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[4][j][i]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[5][j][i]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[6][j][i]=m;
		data_buffer[7][j][i]=shift[j];
		data_buffer[8][j][i]=down[j];
		data_buffer[9][j][i]=program[j];
		data_buffer[10][j][i]=volume[j];
		data_buffer[11][j][i]=panpot[j];
		data_buffer[12][j][i]=para1[j];
		data_buffer[13][j][i]=para2[j];
		data_buffer[14][j][i]=range[0][j];
		data_buffer[15][j][i]=range[1][j];
		data_buffer[16][j][i]=tim[0][j];
		data_buffer[17][j][i]=tim[1][j];
		for(k=0;k<12;k++){
			data_buffer[18+k][j][i]=filter[j][k];
		}
	}
}

each_channel(ch)
	int ch;
{
	if(phase[ch]==0){ counter[ch]=0; }
	else if(phase[ch]==1){ if(++counter[ch]>tim[0][ch]) phase[ch]=2; }
	else if(phase[ch]==2){
		phase[ch]=3; chaos[ch]=myu[ch]*chaos[ch]*(1.0-chaos[ch]);
		note[ch]=(int)ref_table[ch][(int)(sum[ch]*chaos[ch])];
		put_data(0x90+midich[ch]); put_data(note[ch]);
		put_data(0x7f); velocity[ch]=0x7f;
		point_screen(7,ch,0);	/* Note, Velocity */
	}
	else if(phase[ch]==3){
		phase[ch]=4; put_data(0x80+midich[ch]);
		put_data(note[ch]); put_data(0x00); counter[ch]=0;
	}
	else if(phase[ch]==4){ if(++counter[ch]>tim[1][ch]) phase[ch]=5; }
	else if(phase[ch]==5){
		velocity[ch]=velocity[ch]-down[ch];
		if(velocity[ch]<1){ phase[ch]=0; return(0); }
		phase[ch]=3; chaos[ch]=myu[ch]*chaos[ch]*(1.0-chaos[ch]);
		note[ch]=(int)ref_table[ch][(int)(sum[ch]*chaos[ch])];
		put_data(0x90+midich[ch]); put_data(note[ch]);
		put_data(velocity[ch]);
		point_screen(7,ch,0);	/* Note, Velocity */
	}
}

myu_decode(j)
	int j;
{
	double myu_buff;
	int i,m;
	i=section-1;
	myu_buff=myu[j];
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[0][j][i]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[1][j][i]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[2][j][i]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[3][j][i]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[4][j][i]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[5][j][i]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[6][j][i]=m;
}

myu_encode(j)
	int j;
{
	int i,m0,m1,m2,m3,m4,m5,m6;
	i=section-1;
	m0=data_buffer[0][j][i]; /* Myu over point */
	m1=data_buffer[1][j][i]; /* Myu (1) */
	m2=data_buffer[2][j][i]; /* Myu (2) */
	m3=data_buffer[3][j][i]; /* Myu (3) */
	m4=data_buffer[4][j][i]; /* Myu (4) */
	m5=data_buffer[5][j][i]; /* Myu (5) */
	m6=data_buffer[6][j][i]; /* Myu (6) */
	myu[j]=(double)m0;
	myu[j]=myu[j]+0.1*(double)m1;
	myu[j]=myu[j]+0.01*(double)m2;
	myu[j]=myu[j]+0.001*(double)m3;
	myu[j]=myu[j]+0.0001*(double)m4;
	myu[j]=myu[j]+0.00001*(double)m5;
	myu[j]=myu[j]+0.000001*(double)m6;
}

edit_midi_rx(sel,data)
	int sel,data;
{
	int i,j,m0,m1,m2,m3,m4,m5,m6;
	if((edit_ch<1)||(edit_ch>8)) return(0);
	i=section-1;
	j=edit_ch-1;
	switch(sel){
		case (63):
			myu_decode(j);
			data_buffer[0][j][i]=data; /* Myu over point */
			myu_encode(j);
			point_screen(6,j,0);	/* Myu, Shift */
			break;
		case (64):
			myu_decode(j);
			data_buffer[1][j][i]=data; /* Myu (1) */
			myu_encode(j);
			point_screen(6,j,0);	/* Myu, Shift */
			break;
		case (65):
			myu_decode(j);
			data_buffer[2][j][i]=data; /* Myu (2) */
			myu_encode(j);
			point_screen(6,j,0);	/* Myu, Shift */
			break;
		case (66):
			myu_decode(j);
			data_buffer[3][j][i]=data; /* Myu (3) */
			myu_encode(j);
			point_screen(6,j,0);	/* Myu, Shift */
			break;
		case (67):
			myu_decode(j);
			data_buffer[4][j][i]=data; /* Myu (4) */
			myu_encode(j);
			point_screen(6,j,0);	/* Myu, Shift */
			break;
		case (68):
			myu_decode(j);
			data_buffer[5][j][i]=data; /* Myu (5) */
			myu_encode(j);
			point_screen(6,j,0);	/* Myu, Shift */
			break;
		case (69):
			myu_decode(j);
			data_buffer[6][j][i]=data; /* Myu (6) */
			myu_encode(j);
			point_screen(6,j,0);	/* Myu, Shift */
			break;
		case (70):
			shift[j]=data;
			point_screen(6,j,0);	/* Myu, Shift */
			break;
		case (71):
			down[j]=data;
			point_screen(5,j,0);	/* Down, Program */
			break;
		case (72):
			program[j]=data;
			point_screen(5,j,0);	/* Down, Program */
			break;
		case (73):
			volume[j]=data;
			point_screen(1,j,0);	/* Volume, Panpot */
			break;
		case (74):
			panpot[j]=data;
			point_screen(1,j,0);	/* Volume, Panpot */
			break;
		case (75):
			para1[j]=data;
			point_screen(8,j,0);	/* Parameters */
			break;
		case (76):
			para2[j]=data;
			point_screen(8,j,0);	/* Parameters */
			break;
		case (77):
			range[0][j]=data;
			point_screen(3,j,0);	/* Range */
			break;
		case (78):
			range[1][j]=data;
			point_screen(3,j,0);	/* Range */
			break;
		case (79):
			tim[0][j]=data;
			point_screen(4,j,0);	/* Delay, Step */
			break;
		case (80):
			tim[1][j]=data;
			point_screen(4,j,0);	/* Delay, Step */
			break;
		case (81):
			filter[j][0]=data;
			point_screen(2,j,0);	/* Note Map */
			break;
		case (82):
			filter[j][1]=data;
			point_screen(2,j,0);	/* Note Map */
			break;
		case (83):
			filter[j][2]=data;
			point_screen(2,j,0);	/* Note Map */
			break;
		case (84):
			filter[j][3]=data;
			point_screen(2,j,0);	/* Note Map */
			break;
		case (85):
			filter[j][4]=data;
			point_screen(2,j,0);	/* Note Map */
			break;
		case (86):
			filter[j][5]=data;
			point_screen(2,j,0);	/* Note Map */
			break;
		case (87):
			filter[j][6]=data;
			point_screen(2,j,0);	/* Note Map */
			break;
		case (88):
			filter[j][7]=data;
			point_screen(2,j,0);	/* Note Map */
			break;
		case (89):
			filter[j][8]=data;
			point_screen(2,j,0);	/* Note Map */
			break;
		case (90):
			filter[j][9]=data;
			point_screen(2,j,0);	/* Note Map */
			break;
		case (91):
			filter[j][10]=data;
			point_screen(2,j,0);	/* Note Map */
			break;
		case (92):
			filter[j][11]=data;
			point_screen(2,j,0);	/* Note Map */
			break;
	}
}

midi_display(mode,status,keyno,data,u_d)
	int mode,status,keyno,data,u_d;
{
	int j,fifo_buff;
	if(u_d==0){
		fifo_buff=fifo_pointer;
		midi_fifo[0][fifo_pointer]=mode;
		midi_fifo[1][fifo_pointer]=status;
		midi_fifo[2][fifo_pointer]=keyno;
		midi_fifo[3][fifo_pointer]=data;
		fifo_pointer++;
		if(fifo_pointer>9999) fifo_pointer=0;
		if(fifo_pointer==read_pointer) fifo_pointer=fifo_buff;
		return(0);
	}
	if(fifo_pointer==read_pointer) return(0);
	mode=midi_fifo[0][read_pointer];
	status=midi_fifo[1][read_pointer];
	keyno=midi_fifo[2][read_pointer];
	data=midi_fifo[3][read_pointer];
	read_pointer++;
	if(read_pointer>9999) read_pointer=0;
	switch(mode){
		case(0):
			_settextposition(12,32);
			printf("%02X %02X %02X",status,keyno,data);
			break;
		case(1):
			_settextposition(12,41);
			printf("%02X %02X %02X",status,keyno,data);
			break;
		case(2):
			_settextposition(12,50);
			printf("%02X %02X %02X",status,keyno,data);
			if(status>0xb7) break;
			j=status&0x0f;
			if(keyno==7) volume[j]=data;
			else if(keyno==10) panpot[j]=data;
			else break;
			point_screen(1,j,1);	/* Volume, Panpot */
			break;
		case(3):
			_settextposition(12,59);
			printf("%02X %02X",status,keyno);
			if(status>0xc7) break;
			j=status&0x0f;
			program[j]=keyno;
			point_screen(5,j,1);	/* Down, Program */
			break;
	}
}

window_draw(d)
	int d;
{
	int w1x=7,w1y=133,w2x=632,w2y=170;
	if(d==0){
		_settextposition(9,15);
		printf("                                                   ");
		mouse(0);
		_putimage(w1x,w1y,wind,_GPSET);
		mouse(1);
		return(0);
	}
	mouse(0);
	_getimage(w1x,w1y,w2x,w2y,wind);
	_setcolor(0); _rectangle(_GFILLINTERIOR,w1x,w1y,w2x,w2y);
	_setcolor(7); _rectangle(_GFILLINTERIOR,w1x+4,w1y+4,w2x-4,w2y-4);
	_setcolor(0); _rectangle(_GFILLINTERIOR,w1x+8,w1y+8,w2x-8,w2y-8);
	_setcolor(7);
	mouse(1);
	_settextposition(9,15);
	printf("System is waiting ... Plaese Cueing with JOY/KBD !!");
}

bar_display(d)
	int d;
{
	_settextposition(12,66);
	printf(" ★");
	if(d>127) char_disp(10);
	else if(d>99) char_disp(1);
	else char_disp(0);
	d=d-100*(d/100);
	char_disp(d/10);
	char_disp(d%10);
	printf("★  ");
}

char_disp(x)
	int x;
{
	if(x==0) printf("0");
	else if(x==1) printf("1");
	else if(x==2) printf("2");
	else if(x==3) printf("3");
	else if(x==4) printf("4");
	else if(x==5) printf("5");
	else if(x==6) printf("6");
	else if(x==7) printf("7");
	else if(x==8) printf("8");
	else if(x==9) printf("9");
	else printf("*");
}

パソコンのC その4 MIDI制御カオスジェネレータ

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma check_stack(off)
#pragma intrinsic(_disable,_enable)
#pragma function(inp,outp)
#define _GB 2
#define _GF 3

void interrupt far new_0a(void);
void (interrupt far *old_0a)(void);
union REGS inregs,outregs;
struct SREGS segregs;
int moux,mouy,right,left,old_l,old_r,old_x,old_y;
FILE *fds,*fdo;
long vsync_counter;
double myu[8],chaos[8];
int volume[8],filter[8][12],midich[8],shift[8],range[2][8],tim[2][8];
int down[8],program[8],panpot[8],para1[8],para2[8],ch_trig;
int section,edit_ch,note[8],velocity[8],phase[8],counter[8],sum[8];
int data_buffer[30][8][9],fifo_pointer,read_pointer;
unsigned char ref_table[8][3200];

mouse(flag) int flag; {
	if(flag==0){inregs.x.ax=2;} else if(flag==1){inregs.x.ax=1;}
	int86(51,&inregs,&outregs); }
getmouse(right,left,x,y) int *right,*left,*x,*y; { inregs.x.ax=3;
	int86(51,&inregs,&outregs); *right=outregs.x.bx;
	*left=outregs.x.ax; *x=outregs.x.cx; *y=outregs.x.dx; }
setmouse(x,y) int x,y; { inregs.x.ax=4; inregs.x.cx=x; inregs.x.dx=y;
	int86(51,&inregs,&outregs); }
m_style(x,y,buf) int x,y; char *buf; { inregs.x.ax=9; inregs.x.bx=x;
	inregs.x.cx=y; segread(&segregs); segregs.es=segregs.ds;
	inregs.x.dx=(int)buf; int86x(51,&inregs,&outregs,&segregs); }
m_view(xmin,ymin,xmax,ymax) int xmin,ymin,xmax,ymax; { inregs.x.ax=16;
	inregs.x.cx=xmin; inregs.x.dx=xmax; int86(51,&inregs,&outregs);
	inregs.x.ax=17; inregs.x.cx=ymin; inregs.x.dx=ymax;
	int86(51,&inregs,&outregs); }
m_color(color) int color; {
	inregs.x.ax=18; inregs.x.bx=color; int86(51,&inregs,&outregs); }
symbol(x,y,color,str,skip) int x,y,color,skip; unsigned char str[80]; {
	int i=0; _setcolor(color);
	while(str[i]!=0){ _gputchar(x,y,str[i++],_GXOR); x += skip; } }
int style[]={ 0,0,0,0,0,0,0,0,0x0070,0x0e88,0x1184,0x2142,0x2221,0x9210,
	0xe107,0x1108,0x1108,0xe107,0x1108,0x1108,0xe107,0x4108,0x4208,
	0xfc07,0,0,0,0,0,0,0,0 };
put_command(x) int x; { while((inp(0xe0d2)&0x40)!=0){} outp(0xe0d2,x); }
put_data(x) int x; { while((inp(0xe0d2)&0x40)!=0){} outp(0xe0d0,x); }
get_data(){ while((inp(0xe0d2)&0x80)!=0){} return(inp(0xe0d0)); }
set_command(x) int x; { put_command(x); while(get_data()!=0xfe){} }
msb98_check(){ int i,j; for(i=0;i<100;i++) inp(0xe0d0);
	j=0; for(i=0;i<10000;i++){ if((inp(0xe0d2)&0x40)==0) j=1; }
	if(j==0) return(-1);
	outp(0xe0d2,0xff);
	j=0; for(i=0;i<32000;i++){ if((inp(0xe0d2)&0x80)==0) j=1; }
	if(j==0) return(-1);
	if(inp(0xe0d0)!=0xfe) return(-1);
	set_command(0xff); set_command(0xac); i=get_data();
	if(i!=0x15) return(-1);
	set_command(0xf0); set_command(0xac); i=get_data();
	if(i!=0x28) return(-1);
	set_command(0xff); return(0); }
msb_hot_reset(){ inp(0xe0d0); inp(0xe0d0); inp(0xe0d0); set_command(0xff); }
vsync_int_start(){ if(old_0a!=NULL) return;
	old_0a=_dos_getvect(0x0a); _dos_setvect(0x0a,new_0a);
	outp(0x64,0); _disable(); outp(0x02,inp(0x02)&0xfb); _enable();
	signal(SIGINT,SIG_IGN); }
vsync_int_stop(){ if(old_0a==NULL) return;
	_disable(); outp(0x02,inp(0x02)|0x04); _enable();
	_dos_setvect(0x0a,old_0a); old_0a=NULL;	signal(SIGINT,SIG_DFL); }
void interrupt far new_0a(){ outp(0x64,0);
	vsync_counter++; _disable(); _chain_intr(old_0a); }

main(){
	int i,j,d,ch,m_ctr,rsb,dcb,keyno,m_m,m_stat=0;
	int job_fg[8],job_ct=0,run_flag=0,pause_pass=0,bar_disp=0;
	inregs.x.ax=0; int86(51,&inregs,&outregs);
	if(outregs.x.ax==0){printf("\nPlease install mouse.sys !\n");exit(0);}
	m_style(0,8,style); m_color(1); m_view(0,0,639,399); setmouse(320,200);
	if(msb98_check()==-1){
		printf("\n\n\n\n\n\t\t\t[MSB-98] is not found !\n\n");
		while(1){ if(kbhit()!=0) break; }
		exit(0);
	}
	msb_hot_reset();
	set_command(0x3f);
	for(i=0;i<4096;i++){ for(j=0;j<200;j++){} inp(0xe0d0); }
	for(d=0;d<8;d++){
		midich[d]=d; velocity[d]=0;
		phase[d]=0; counter[d]=0; chaos[d]=0.5; job_fg[d]=0;
	}
	_setvideomode(_98RESS16COLOR); _displaycursor(_GCURSOROFF);
	_clearscreen(_GCLEARSCREEN);
	parameter_file_read();
	section=1; screen_display(); edit_ch=0;
	_settextposition(12,31);
	printf("[Chaos Generator] by Y.Nagashima");
	fifo_pointer=0; read_pointer=0;
	mouse(1); getmouse(&old_r,&old_l,&old_x,&old_y);
	vsync_counter=0; vsync_int_start();
	while(1){
		if(vsync_counter!=0){
			vsync_counter=0;
			if(run_flag!=2) put_data(0xf8);
			for(ch=0;ch<8;ch++) job_fg[ch]=1;
		}
		if(job_fg[job_ct]!=0){
			each_channel(job_ct);
			job_fg[job_ct]=0;
			job_ct=(job_ct+1)&0x07;
		}
		else if(kbhit()!=0){
			d=getch();
			if(d==0x1b) break;
			else if((d==0x20)||(d==0x0d)){
				for(d=0;d<8;d++){
					phase[d-1]=1;
					if(volume[d-1]!=0) velocity[d-1]=0x7f;
				}
			}
			else if((d>0x30)&&(d<0x3a)){
				for(ch=0;ch<8;ch++) velocity[ch]=0;
				parameter_to_data_buffer();
				section=d-0x30; screen_display();
			}
			else if((d=='a')||(d=='A')){
				d=1; phase[d-1]=1;
				if(volume[d-1]!=0) velocity[d-1]=0x7f;
			}
			else if((d=='b')||(d=='B')){
				d=2; phase[d-1]=1;
				if(volume[d-1]!=0) velocity[d-1]=0x7f;
			}
			else if((d=='c')||(d=='C')){
				d=3; phase[d-1]=1;
				if(volume[d-1]!=0) velocity[d-1]=0x7f;
			}
			else if((d=='d')||(d=='D')){
				d=4; phase[d-1]=1;
				if(volume[d-1]!=0) velocity[d-1]=0x7f;
			}
			else if((d=='e')||(d=='E')){
				d=5; phase[d-1]=1;
				if(volume[d-1]!=0) velocity[d-1]=0x7f;
			}
			else if((d=='f')||(d=='F')){
				d=6; phase[d-1]=1;
				if(volume[d-1]!=0) velocity[d-1]=0x7f;
			}
			else if((d=='g')||(d=='G')){
				d=7; phase[d-1]=1;
				if(volume[d-1]!=0) velocity[d-1]=0x7f;
			}
			else if((d=='h')||(d=='H')){
				d=8; phase[d-1]=1;
				if(volume[d-1]!=0) velocity[d-1]=0x7f;
			}
		}
		else{
			getmouse(&right,&left,&moux,&mouy);
			if((right==0)&&(left==0)) m_stat=0;
			else if((old_l==0)&&(left!=0)){
				m_stat=1; m_ctr=0; m_m=1;
			}
			else if((old_r==0)&&(right!=0)){
				m_stat=2; m_ctr=0; m_m=1;
			}
			else if(left!=0){
				if(++m_ctr>30){ m_stat=1; m_ctr=0; m_m=0; }
			}
			else if(right!=0){
				if(++m_ctr>30){ m_stat=2; m_ctr=0; m_m=0;}
			}
			if(m_stat==1){ m_stat=0; m_stat_1(m_m); }
			else if(m_stat==2){ m_stat=0; m_stat_2(m_m); }
			old_r=right; old_l=left; old_x=moux; old_y=mouy;
			if(bar_disp==0){
				_settextposition(12,66);
				printf("(X=%03d,Y=%03d)",moux,mouy);
			}
		}
	}
	vsync_int_stop(); mouse(0);
	for(i=0;i<8;i++){
		put_data(0x80+midich[i]); put_data(note[i]); put_data(0);
	}
	put_data(0xfc);
	for(i=0;i<16;i++){ put_data(0xb0+i); put_data(0x7f); put_data(0); }
	put_command(0xff);
	parameter_to_data_buffer(); parameter_file_write();
	for(i=0;i<4096;i++){ for(j=0;j<200;j++){} inp(0xe0d0); }
	_clearscreen(_GCLEARSCREEN); _displaycursor(_GCURSORON);
	_setvideomode (_DEFAULTMODE);
}

m_stat_1(m_m)
	int m_m;
{
	if((mouy>176)&&(mouy<189)) volume_change(0,m_m);
	else if((mouy>384)&&(mouy<397)) volume_change(4,m_m);
	else if((mouy>114)&&(mouy<128)) range_change(0,0);
	else if((mouy>322)&&(mouy<336)) range_change(4,0);
	else if((mouy>98)&&(mouy<112)) time_change(0,0);
	else if((mouy>306)&&(mouy<320)) time_change(4,0);
	else if((mouy>82)&&(mouy<96)) dwn_prg_change(0,0);
	else if((mouy>290)&&(mouy<304)) dwn_prg_change(4,0);
	else if((mouy>50)&&(mouy<64)) myu_change(0,0,m_m);
	else if((mouy>258)&&(mouy<272)) myu_change(4,0,m_m);
	else if((mouy>158)&&(mouy<174)) map_change(0,0);
	else if((mouy>366)&&(mouy<382)) map_change(4,0);
	else if((mouy>66)&&(mouy<80)) p1_p2_change(0,0);
	else if((mouy>274)&&(mouy<288)) p1_p2_change(4,0);
	else if((mouy>34)&&(mouy<48)) volpan_change(0,0);
	else if((mouy>242)&&(mouy<256)) volpan_change(4,0);
	else if((mouy>0)&&(mouy<16)) channel_trigger(0,m_m);
	else if((mouy>208)&&(mouy<224)) channel_trigger(4,m_m);
}

m_stat_2(m_m)
	int m_m;
{
	if((mouy>176)&&(mouy<189)) volume_change(0,m_m);
	else if((mouy>384)&&(mouy<397)) volume_change(4,m_m);
	else if((mouy>114)&&(mouy<128)) range_change(0,1);
	else if((mouy>322)&&(mouy<336)) range_change(4,1);
	else if((mouy>98)&&(mouy<112)) time_change(0,1);
	else if((mouy>306)&&(mouy<320)) time_change(4,1);
	else if((mouy>82)&&(mouy<96)) dwn_prg_change(0,1);
	else if((mouy>290)&&(mouy<304)) dwn_prg_change(4,1);
	else if((mouy>50)&&(mouy<64)) myu_change(0,1,m_m);
	else if((mouy>258)&&(mouy<272)) myu_change(4,1,m_m);
	else if((mouy>158)&&(mouy<174)) map_change(0,1);
	else if((mouy>366)&&(mouy<382)) map_change(4,1);
	else if((mouy>66)&&(mouy<80)) p1_p2_change(0,1);
	else if((mouy>274)&&(mouy<288)) p1_p2_change(4,1);
	else if((mouy>34)&&(mouy<48)) volpan_change(0,1);
	else if((mouy>242)&&(mouy<256)) volpan_change(4,1);
}

point_screen(mode,y,midi)
	int mode,y,midi;
{
	char ss[5];
	int i,j,k,l,xx,yy,xt,yt,xx_b,yy_b;
	xt=1+(y%4)*20; yt=(y/4)*13; xx=1+(y%4)*160; yy=1+(y/4)*208;
	if(mode==0){
		mouse(0); ss[0]='A'+y; ss[1]=0;
		_setlinestyle(0xffff); _setcolor(0);
		_rectangle(_GF,xx,yy,xx+157,yy+190); _setcolor(7);
		_rectangle(_GB,xx,yy,xx+157,yy+190);
		_rectangle(_GF,xx,yy,xx+157,yy+14);
		_settextposition(yt,xt); printf("\x1b[30m");
		printf("   == %s ==   ch:%d ",ss,midich[y]+1);
		printf("\x1b[37m"); mouse(1);
	}
	else if(mode==1){
		mouse(0); _setlinestyle(0xffff); _setcolor(0);
		_rectangle(_GF,xx+1,yy+173,xx+156,yy+189); _setcolor(7);
		xx_b=xx+6+(volume[y]/2)*143/64; yy_b=yy+176;
		_moveto(xx+7,yy_b+5); _lineto(xx+150,yy_b+5);
		_moveto(xx+7,yy_b+6); _lineto(xx+150,yy_b+6);
		i=0; _moveto(xx+7+11*i,yy_b+2); _lineto(xx+7+11*i,yy_b+9);
		i=13; _moveto(xx+7+11*i,yy_b+2); _lineto(xx+7+11*i,yy_b+9);
		for(i=1;i<13;i++){
			_moveto(xx+7+11*i,yy_b+3); _lineto(xx+7+11*i,yy_b+8);
		}
		_rectangle(_GF,xx_b,yy_b,xx_b+5,yy_b+11); _setcolor(0);
		_rectangle(_GF,xx_b+1,yy_b+1,xx_b+4,yy_b+10); _setcolor(7);
		_rectangle(_GF,xx_b+2,yy_b+2,xx_b+3,yy_b+9);
		_settextposition(yt+2,xt);
		printf(" vol=%03d  pan=%03d",volume[y],panpot[y]);
		if(midi==0){
			put_data(0xb0+midich[y]);
			put_data(0x07); put_data(volume[y]);
			put_data(0xb0+midich[y]); put_data(0x0A);
			put_data(panpot[y]);
		}
		mouse(1);
	}
	else if(mode==2){
		mouse(0); _setlinestyle(0xffff); _setcolor(0);
		_rectangle(_GF,xx+13,yy+131,xx+144,yy+157); _setcolor(7);
		_setlinestyle(0x6666);
		_rectangle(_GB,xx+12,yy+130,xx+145,yy+158);
		_setlinestyle(0xffff);
		for(i=0;i<7;i++){
			_rectangle(_GB,xx+12+19*i,yy+158,xx+31+19*i,yy+172);
		}
		yy_b=yy+159;
		xx_b=xx+31; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		xx_b=xx+50; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		xx_b=xx+88; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		xx_b=xx+107; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		xx_b=xx+126; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		yy_b=yy+156;
		_rectangle(_GB,xx+20,yy_b,xx+23,yy_b-filter[y][0]);
		_rectangle(_GF,xx+29,yy_b,xx+33,yy_b-filter[y][1]);
		_rectangle(_GB,xx+39,yy_b,xx+42,yy_b-filter[y][2]);
		_rectangle(_GF,xx+48,yy_b,xx+52,yy_b-filter[y][3]);
		_rectangle(_GB,xx+58,yy_b,xx+61,yy_b-filter[y][4]);
		_rectangle(_GB,xx+77,yy_b,xx+80,yy_b-filter[y][5]);
		_rectangle(_GF,xx+86,yy_b,xx+90,yy_b-filter[y][6]);
		_rectangle(_GB,xx+96,yy_b,xx+99,yy_b-filter[y][7]);
		_rectangle(_GF,xx+105,yy_b,xx+109,yy_b-filter[y][8]);
		_rectangle(_GB,xx+115,yy_b,xx+118,yy_b-filter[y][9]);
		_rectangle(_GF,xx+124,yy_b,xx+128,yy_b-filter[y][10]);
		_rectangle(_GB,xx+134,yy_b,xx+137,yy_b-filter[y][11]);
		mouse(1);
	}
	else if(mode==3){
		_settextposition(yt+7,xt);
		printf(" low=%03d high=%03d",range[0][y],range[1][y]);
	}
	else if(mode==4){
		_settextposition(yt+6,xt);
		printf(" dly=%03d  stp=%03d",tim[0][y],tim[1][y]);
	}
	else if(mode==5){
		_settextposition(yt+5,xt);
		printf(" dwn=%03d  prg=%03d",down[y],program[y]);
		if(midi==0){
			put_data(0xc0+midich[y]); put_data(program[y]);
		}
	}
	else if(mode==6){
		_settextposition(yt+3,xt);
		printf(" myu=%f <%d>",myu[y],shift[y]+1);
	}
	else if(mode==7){
		_settextposition(yt+1,xt);
		printf(" note=%03d vel=%03d",note[y],velocity[y]);
	}
	else if(mode==8){
		_settextposition(yt+4,xt);
		printf(" pp1=%03d  pp2=%03d",para1[y],para2[y]);
	}
	if((mode==2)||(mode==3)){
		sum[y]=0; j=0;
		for(i=range[0][y];i0)&&(moux<159)) of=0;
	else if((moux>160)&&(moux<319)) of=1;
	else if((moux>320)&&(moux<479)) of=2;
	else if((moux>480)&&(moux<639)) of=3;
	else return(0);
	y=off+of;
	phase[y]=1;
	if(volume[y]!=0) velocity[y]=0x7f;
}

volume_change(off,m_m)
	int off,m_m;
{
	int of,y,x;
	if(m_m==0) return(0);
	if((moux>7)&&(moux<152)) { of=0; x=8; }
	else if((moux>167)&&(moux<312)) { of=1; x=168; }
	else if((moux>327)&&(moux<472)) { of=2; x=328; }
	else if((moux>487)&&(moux<632)) { of=3; x=488; }
	else return(0);
	y=off+of;
	volume[y]=(moux-x)*127/143;
	point_screen(1,y,0);	/* Volume */
}

range_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>80)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>240)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>400)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>560)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(range[0][y]>0) range[0][y]--; }
	else if((z==0)&&(md==1)){ if(range[0][y]<127) range[0][y]++; }
	else if((z==1)&&(md==0)){ if(range[1][y]>0) range[1][y]--; }
	else if((z==1)&&(md==1)){ if(range[1][y]<127) range[1][y]++; }
	point_screen(3,y,0);	/* Range */
}

time_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>87)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>247)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>407)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>567)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(tim[0][y]>0) tim[0][y]--; }
	else if((z==0)&&(md==1)){ if(tim[0][y]<127) tim[0][y]++; }
	else if((z==1)&&(md==0)){ if(tim[1][y]>0) tim[1][y]--; }
	else if((z==1)&&(md==1)){ if(tim[1][y]<127) tim[1][y]++; }
	point_screen(4,y,0);	/* Delay, Step */
}

dwn_prg_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>87)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>247)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>407)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>567)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(down[y]>0) down[y]--; }
	else if((z==0)&&(md==1)){ if(down[y]<127) down[y]++; }
	else if((z==1)&&(md==0)){ if(program[y]>0) program[y]--; }
	else if((z==1)&&(md==1)){ if(program[y]<127) program[y]++; }
	point_screen(5,y,0);	/* Down, Program */
}

p1_p2_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>87)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>247)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>407)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>567)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(para1[y]>0) para1[y]--; }
	else if((z==0)&&(md==1)){ if(para1[y]<127) para1[y]++; }
	else if((z==1)&&(md==0)){ if(para2[y]>0) para2[y]--; }
	else if((z==1)&&(md==1)){ if(para2[y]<127) para2[y]++; }
	point_screen(8,y,0);	/* Parameters */
}

volpan_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>87)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>247)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>407)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>567)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(volume[y]>0) volume[y]--; }
	else if((z==0)&&(md==1)){ if(volume[y]<127) volume[y]++; }
	else if((z==1)&&(md==0)){ if(panpot[y]>0) panpot[y]--; }
	else if((z==1)&&(md==1)){ if(panpot[y]<127) panpot[y]++; }
	point_screen(1,y,0);	/* Volume, Panpot */
}

myu_change(off,md,m_m)
	int off,md,m_m;
{
	int of,y,z;
	if((moux>15)&&(moux<111)) { of=0; z=0; }
	else if((moux>120)&&(moux<142)) { of=0; z=1; }
	else if((moux>175)&&(moux<271)) { of=1; z=0; }
	else if((moux>280)&&(moux<302)) { of=1; z=1; }
	else if((moux>335)&&(moux<431)) { of=2; z=0; }
	else if((moux>440)&&(moux<462)) { of=2; z=1; }
	else if((moux>495)&&(moux<591)) { of=3; z=0; }
	else if((moux>600)&&(moux<622)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){
		if(shift[y]==0) myu[y]=myu[y]-0.1;
		else if(shift[y]==1) myu[y]=myu[y]-0.01;
		else if(shift[y]==2) myu[y]=myu[y]-0.001;
		else if(shift[y]==3) myu[y]=myu[y]-0.0001;
		else if(shift[y]==4) myu[y]=myu[y]-0.00001;
		else if(shift[y]==5) myu[y]=myu[y]-0.000001;
		if(myu[y]<1.0) myu[y]=1.0;
	}
	else if((z==0)&&(md==1)){
		if(shift[y]==0) myu[y]=myu[y]+0.1;
		else if(shift[y]==1) myu[y]=myu[y]+0.01;
		else if(shift[y]==2) myu[y]=myu[y]+0.001;
		else if(shift[y]==3) myu[y]=myu[y]+0.0001;
		else if(shift[y]==4) myu[y]=myu[y]+0.00001;
		else if(shift[y]==5) myu[y]=myu[y]+0.000001;
		if(myu[y]>4.0) myu[y]=4.0;
	}
	else if((m_m!=0)&&(z==1)&&(md==0)){ if(shift[y]>0) shift[y]--; }
	else if((m_m!=0)&&(z==1)&&(md==1)){ if(shift[y]<5) shift[y]++; }
	point_screen(6,y,0);	/* Myu, Shift */
}

map_change(off,md)
	int off,md;
{
	int f,of,y,z,xx,yy;
	if(off==0) yy=mouy;
	else yy=mouy-208;
	if((moux>13)&&(moux<147)) { of=0; xx=moux; }
	else if((moux>172)&&(moux<307)) { of=1; xx=moux-160; }
	else if((moux>332)&&(moux<467)) { of=2; xx=moux-320; }
	else if((moux>492)&&(moux<627)) { of=3; xx=moux-480; }
	else return(0);
	if((yy<168)&&(xx>25)&&(xx<38)) z=1;
	else if((yy<168)&&(xx>44)&&(xx<58)) z=3;
	else if((yy<168)&&(xx>82)&&(xx<96)) z=6;
	else if((yy<168)&&(xx>101)&&(xx<115)) z=8;
	else if((yy<168)&&(xx>120)&&(xx<134)) z=10;
	else if(xx<32) z=0;
	else if(xx<51) z=2;
	else if(xx<70) z=4;
	else if(xx<89) z=5;
	else if(xx<108) z=7;
	else if(xx<127) z=9;
	else z=11;
	y=off+of;
	if(md==0){ if(filter[y][z]>0) filter[y][z]--; }
	else { if(filter[y][z]<24) filter[y][z]++; }
	point_screen(2,y,0);	/* Note Map */
}

parameter_file_write(){
	int i,j,k;
	fds=fopen("chaosgen.dat","wb");
	for(i=0;i<9;i++){
		for(j=0;j<8;j++){
			for(k=0;k<30;k++) fputc(data_buffer[k][j][i],fds);
			fputc(0xff,fds);
			fputc(0xff,fds);
		}
	}
	fclose(fds);
}

parameter_file_read(){
	int i,j,k;
	if((fds=fopen("chaosgen.dat","rb"))==NULL){
		for(i=0;i<9;i++){
			for(j=0;j<8;j++){
				data_buffer[0][j][i]=3; /* Myu over point */
				data_buffer[1][j][i]=j; /* Myu (1) */
				data_buffer[2][j][i]=j; /* Myu (2) */
				data_buffer[3][j][i]=j; /* Myu (3) */
				data_buffer[4][j][i]=j; /* Myu (4) */
				data_buffer[5][j][i]=j; /* Myu (5) */
				data_buffer[6][j][i]=j; /* Myu (6) */
				data_buffer[7][j][i]=1; /* shift */
				data_buffer[8][j][i]=1+2*j; /* down */
				data_buffer[9][j][i]=2*j+1; /* program */
				data_buffer[10][j][i]=64+2*j; /* volume */
				data_buffer[11][j][i]=64; /* panpot */
				data_buffer[12][j][i]=99; /* para(1) */
				data_buffer[13][j][i]=99; /* para(2) */
				data_buffer[14][j][i]=36-j; /* range[0] low */
				data_buffer[15][j][i]=84+j; /* range[1] high */
				data_buffer[16][j][i]=j; /* tim[0] Delay */
				data_buffer[17][j][i]=1+j*2; /* tim[1] Step */
				data_buffer[18][j][i]=12; /* filter[0] */
				data_buffer[19][j][i]=12; /* filter[1] */
				data_buffer[20][j][i]=12; /* filter[2] */
				data_buffer[21][j][i]=12; /* filter[3] */
				data_buffer[22][j][i]=12; /* filter[4] */
				data_buffer[23][j][i]=12; /* filter[5] */
				data_buffer[24][j][i]=12; /* filter[6] */
				data_buffer[25][j][i]=12; /* filter[7] */
				data_buffer[26][j][i]=12; /* filter[8] */
				data_buffer[27][j][i]=12; /* filter[9] */
				data_buffer[28][j][i]=12; /* filter[10] */
				data_buffer[29][j][i]=12; /* filter[11] */
			}
		}
	}
	else{
		for(i=0;i<9;i++){
			for(j=0;j<8;j++){
				for(k=0;k<30;k++){
					data_buffer[k][j][i]=fgetc(fds);
				}
				fgetc(fds);
				fgetc(fds);
			}
		}
		fclose(fds);
	}
}

screen_display(){
	int i,j,k,m0,m1,m2,m3,m4,m5,m6;
	i=section-1;
	for(j=0;j<8;j++){
		m0=data_buffer[0][j][i]; /* Myu over point */
		m1=data_buffer[1][j][i]; /* Myu (1) */
		m2=data_buffer[2][j][i]; /* Myu (2) */
		m3=data_buffer[3][j][i]; /* Myu (3) */
		m4=data_buffer[4][j][i]; /* Myu (4) */
		m5=data_buffer[5][j][i]; /* Myu (5) */
		m6=data_buffer[6][j][i]; /* Myu (6) */
		myu[j]=(double)m0;
		myu[j]=myu[j]+0.1*(double)m1;
		myu[j]=myu[j]+0.01*(double)m2;
		myu[j]=myu[j]+0.001*(double)m3;
		myu[j]=myu[j]+0.0001*(double)m4;
		myu[j]=myu[j]+0.00001*(double)m5;
		myu[j]=myu[j]+0.000001*(double)m6;
		shift[j]=data_buffer[7][j][i]; /* shift */
		down[j]=data_buffer[8][j][i]; /* down */
		program[j]=data_buffer[9][j][i]; /* program */
		volume[j]=data_buffer[10][j][i]; /* volume */
		panpot[j]=data_buffer[11][j][i]; /* panpot */
		para1[j]=data_buffer[12][j][i]; /* para(1) */
		para2[j]=data_buffer[13][j][i]; /* para(2) */
		range[0][j]=data_buffer[14][j][i]; /* range[0] low */
		range[1][j]=data_buffer[15][j][i]; /* range[1] high */
		tim[0][j]=data_buffer[16][j][i]; /* tim[0] Delay */
		tim[1][j]=data_buffer[17][j][i]; /* tim[1] Step */
		for(k=0;k<12;k++){
			filter[j][k]=data_buffer[18+k][j][i]; /* filter[k] */
		}
	}
	for(i=0;i<8;i++){
		point_screen(0,i,0);	/* Whole Frame */
		point_screen(7,i,0);	/* Note, Velocity */
		point_screen(6,i,0);	/* Myu, Shift */
		point_screen(8,i,0);	/* Parameters */
		point_screen(1,i,0);	/* Volume, Panpot */
		point_screen(5,i,0);	/* Down, Program */
		point_screen(4,i,0);	/* Delay, Step */
		point_screen(3,i,0);	/* Range */
		point_screen(2,i,0);	/* Note Map */
	}
	_settextposition(12,1);
	if(section==1)      printf("【1】2  3  4  5  6  7  8  9  ");
	else if(section==2) printf("  1【2】3  4  5  6  7  8  9  ");
	else if(section==3) printf("  1  2【3】4  5  6  7  8  9  ");
	else if(section==4) printf("  1  2  3【4】5  6  7  8  9  ");
	else if(section==5) printf("  1  2  3  4【5】6  7  8  9  ");
	else if(section==6) printf("  1  2  3  4  5【6】7  8  9  ");
	else if(section==7) printf("  1  2  3  4  5  6【7】8  9  ");
	else if(section==8) printf("  1  2  3  4  5  6  7【8】9  ");
	else if(section==9) printf("  1  2  3  4  5  6  7  8【9】");
}

parameter_to_data_buffer(){
	double myu_buff;
	int i,j,k,m;
	i=section-1;
	for(j=0;j<8;j++){
		myu_buff=myu[j];
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[0][j][i]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[1][j][i]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[2][j][i]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[3][j][i]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[4][j][i]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[5][j][i]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[6][j][i]=m;
		data_buffer[7][j][i]=shift[j];
		data_buffer[8][j][i]=down[j];
		data_buffer[9][j][i]=program[j];
		data_buffer[10][j][i]=volume[j];
		data_buffer[11][j][i]=panpot[j];
		data_buffer[12][j][i]=para1[j];
		data_buffer[13][j][i]=para2[j];
		data_buffer[14][j][i]=range[0][j];
		data_buffer[15][j][i]=range[1][j];
		data_buffer[16][j][i]=tim[0][j];
		data_buffer[17][j][i]=tim[1][j];
		for(k=0;k<12;k++){
			data_buffer[18+k][j][i]=filter[j][k];
		}
	}
}

each_channel(ch)
	int ch;
{
	if(phase[ch]==0){ counter[ch]=0; }
	else if(phase[ch]==1){ if(++counter[ch]>tim[0][ch]) phase[ch]=2; }
	else if(phase[ch]==2){
		phase[ch]=3; chaos[ch]=myu[ch]*chaos[ch]*(1.0-chaos[ch]);
		note[ch]=(int)ref_table[ch][(int)(sum[ch]*chaos[ch])];
		put_data(0x90+midich[ch]); put_data(note[ch]);
		put_data(0x7f); velocity[ch]=0x7f;
		point_screen(7,ch,0);	/* Note, Velocity */
	}
	else if(phase[ch]==3){
		phase[ch]=4; put_data(0x80+midich[ch]);
		put_data(note[ch]); put_data(0x00); counter[ch]=0;
	}
	else if(phase[ch]==4){ if(++counter[ch]>tim[1][ch]) phase[ch]=5; }
	else if(phase[ch]==5){
		velocity[ch]=velocity[ch]-down[ch];
		if(velocity[ch]<1){ phase[ch]=0; return(0); }
		phase[ch]=3; chaos[ch]=myu[ch]*chaos[ch]*(1.0-chaos[ch]);
		note[ch]=(int)ref_table[ch][(int)(sum[ch]*chaos[ch])];
		put_data(0x90+midich[ch]); put_data(note[ch]);
		put_data(velocity[ch]);
		point_screen(7,ch,0);	/* Note, Velocity */
	}
}

myu_decode(j)
	int j;
{
	double myu_buff;
	int i,m;
	i=section-1;
	myu_buff=myu[j];
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[0][j][i]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[1][j][i]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[2][j][i]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[3][j][i]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[4][j][i]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[5][j][i]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[6][j][i]=m;
}

myu_encode(j)
	int j;
{
	int i,m0,m1,m2,m3,m4,m5,m6;
	i=section-1;
	m0=data_buffer[0][j][i]; /* Myu over point */
	m1=data_buffer[1][j][i]; /* Myu (1) */
	m2=data_buffer[2][j][i]; /* Myu (2) */
	m3=data_buffer[3][j][i]; /* Myu (3) */
	m4=data_buffer[4][j][i]; /* Myu (4) */
	m5=data_buffer[5][j][i]; /* Myu (5) */
	m6=data_buffer[6][j][i]; /* Myu (6) */
	myu[j]=(double)m0;
	myu[j]=myu[j]+0.1*(double)m1;
	myu[j]=myu[j]+0.01*(double)m2;
	myu[j]=myu[j]+0.001*(double)m3;
	myu[j]=myu[j]+0.0001*(double)m4;
	myu[j]=myu[j]+0.00001*(double)m5;
	myu[j]=myu[j]+0.000001*(double)m6;
}

bar_display(d)
	int d;
{
	_settextposition(12,66);
	printf(" ★");
	if(d>127) char_disp(10);
	else if(d>99) char_disp(1);
	else char_disp(0);
	d=d-100*(d/100);
	char_disp(d/10);
	char_disp(d%10);
	printf("★  ");
}

char_disp(x)
	int x;
{
	if(x==0) printf("0");
	else if(x==1) printf("1");
	else if(x==2) printf("2");
	else if(x==3) printf("3");
	else if(x==4) printf("4");
	else if(x==5) printf("5");
	else if(x==6) printf("6");
	else if(x==7) printf("7");
	else if(x==8) printf("8");
	else if(x==9) printf("9");
	else printf("*");
}

パソコンのC その5 MIDI制御グラニュラシンセサイザ

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#pragma check_stack(off)
#pragma intrinsic(_disable,_enable)
#pragma function(inp,outp)
#define _GB 2
#define _GF 3

void interrupt far new_0a(void);
void (interrupt far *old_0a)(void);
union REGS inregs,outregs;
struct SREGS segregs;
int moux,mouy,right,left,old_l,old_r,old_x,old_y;
time_t ltime;
FILE *fds,*fdo;
char far wind[14000];
long vsync_counter;
double myu[8],chaos[8];
unsigned char ref_table[8][3200],midi_fifo[3][10000];
int volume[8],filter[8][12],midich[8],shift[8],range[2][8],tim[2][8],sum[8];
int down[8],program[8],para1[8],note[8],velocity[8],phase[8],counter[8];
int pad_assign[3][8],pad_mark[8],pad_para[8],cond_pad[32];
int on_off[8],data_buffer[30][8],fifo_pointer,read_pointer;
int ch,dh,scene,ch_trig,edit_ch,debug;

int drums[12]={ 43,41,50,45,48,38,46,44,42,52,49,55 };
int pad_note[8]={ 40,43,47,50,38,42,45,49 };
int pad_cg[8]={ 1,3,5,7,0,2,4,6 };

mouse(flag) int flag; {
	if(flag==0){inregs.x.ax=2;} else if(flag==1){inregs.x.ax=1;}
	int86(51,&inregs,&outregs); }
getmouse(right,left,x,y) int *right,*left,*x,*y; { inregs.x.ax=3;
	int86(51,&inregs,&outregs); *right=outregs.x.bx;
	*left=outregs.x.ax; *x=outregs.x.cx; *y=outregs.x.dx; }
setmouse(x,y) int x,y; { inregs.x.ax=4; inregs.x.cx=x; inregs.x.dx=y;
	int86(51,&inregs,&outregs); }
m_style(x,y,buf) int x,y; char *buf; { inregs.x.ax=9; inregs.x.bx=x;
	inregs.x.cx=y; segread(&segregs); segregs.es=segregs.ds;
	inregs.x.dx=(int)buf; int86x(51,&inregs,&outregs,&segregs); }
m_view(xmin,ymin,xmax,ymax) int xmin,ymin,xmax,ymax; { inregs.x.ax=16;
	inregs.x.cx=xmin; inregs.x.dx=xmax; int86(51,&inregs,&outregs);
	inregs.x.ax=17; inregs.x.cx=ymin; inregs.x.dx=ymax;
	int86(51,&inregs,&outregs); }
m_color(color) int color; {
	inregs.x.ax=18; inregs.x.bx=color; int86(51,&inregs,&outregs); }
symbol(x,y,color,str,skip) int x,y,color,skip; unsigned char str[80]; {
	int i=0; _setcolor(color);
	while(str[i]!=0){ _gputchar(x,y,str[i++],_GXOR); x += skip; } }
int style[]={ 0,0,0,0,0,0,0,0,0x0070,0x0e88,0x1184,0x2142,0x2221,0x9210,
	0xe107,0x1108,0x1108,0xe107,0x1108,0x1108,0xe107,0x4108,0x4208,
	0xfc07,0,0,0,0,0,0,0,0 };
put_command(x) int x; { while((inp(0xe0d2)&0x40)!=0){} outp(0xe0d2,x); }
put_data(x) int x; { while((inp(0xe0d2)&0x40)!=0){} outp(0xe0d0,x); }
get_data(){ while((inp(0xe0d2)&0x80)!=0){} return(inp(0xe0d0)); }
set_command(x) int x; { put_command(x); while(get_data()!=0xfe){} }
msb98_check(){ int i,j; for(i=0;i<100;i++) inp(0xe0d0);
	j=0; for(i=0;i<10000;i++){ if((inp(0xe0d2)&0x40)==0) j=1; }
	if(j==0) return(-1);
	outp(0xe0d2,0xff);
	j=0; for(i=0;i<32000;i++){ if((inp(0xe0d2)&0x80)==0) j=1; }
	if(j==0) return(-1);
	if(inp(0xe0d0)!=0xfe) return(-1);
	set_command(0xff); set_command(0xac); i=get_data();
	if(i!=0x15) return(-1);
	set_command(0xf0); set_command(0xac); i=get_data();
	if(i!=0x28) return(-1);
	set_command(0xff);
	if(debug==0){ set_command(0xf0); set_command(0xfd); }
	set_command(0x3f);
	for(i=0;i<4096;i++){ for(j=0;j<200;j++){} inp(0xe0d0); }
	return(0); }
vsync_int_start(){ if(old_0a!=NULL) return;
	old_0a=_dos_getvect(0x0a); _dos_setvect(0x0a,new_0a);
	outp(0x64,0); _disable(); outp(0x02,inp(0x02)&0xfb); _enable();
	signal(SIGINT,SIG_IGN); }
vsync_int_stop(){ if(old_0a==NULL) return;
	_disable(); outp(0x02,inp(0x02)|0x04); _enable();
	_dos_setvect(0x0a,old_0a); old_0a=NULL;	signal(SIGINT,SIG_DFL); }
void interrupt far new_0a(){ outp(0x64,0);
	vsync_counter++; _disable(); _chain_intr(old_0a); }

main(){
	int i,j,d,y,z,m_ctr,rsb,dcb,keyno,m_m,m_stat=0;
	int job_fg[8],job_ct=0,run_flag=0,bar_disp=0;
	debug=0;  /********** [0]=Normal, [1]=Debug ****************/
	inregs.x.ax=0; int86(51,&inregs,&outregs);
	if(outregs.x.ax==0){printf("\nPlease install mouse.sys !\n");exit(0);}
	m_style(0,8,style); m_color(1); m_view(0,0,639,399); setmouse(580,65);
	if(msb98_check()==-1){
		printf("\n\n\n\n\n\t\t\t[MSB-98] is not found !\n\n");
		while(1){ if(kbhit()!=0) break; }
		exit(0);
	}
	for(d=0;d<8;d++){
		midich[d]=d; velocity[d]=0; chaos[d]=0.5; phase[d]=0;
		counter[d]=0; job_fg[d]=0; on_off[d]=0; pad_mark[d]=0;
		pad_para[d]=0; cond_pad[d]=0;
	}
	_setvideomode(_98RESS16COLOR); _displaycursor(_GCURSOROFF);
	_clearscreen(_GCLEARSCREEN);
	time(<ime); srand((int)ltime);
	parameter_file_read(); screen_display(); pad_assign_display(0);
	edit_ch=0; scene=1; scene_display(); bar_display(0);
	put_data(0xaf); put_data(98); put_data(11);
	if(debug!=0){
		_settextposition(4,59); printf("### Debug Mode ###");
		put_data(0xcc); put_data(23); put_data(0xcd); put_data(51);
	}
	fifo_pointer=0; read_pointer=0;
	mouse(1); getmouse(&old_r,&old_l,&old_x,&old_y);
	vsync_counter=0; vsync_int_start();
	while(1){
		if(vsync_counter!=0){
			vsync_counter=0; m_ctr++;
			if(run_flag!=2) put_data(0xf8);
			for(i=0;i<8;i++) job_fg[i]=1;
		}
		if((inp(0xe0d2)&0x80)==0){
			d=inp(0xe0d0);
			if(d>0xef){}
			else if(d>0x7f){ rsb=d; dcb=0; }
			else if(rsb==0x9f){
				if(dcb==0){ keyno=d; dcb=1; }
				else{
					dcb=0;
					if(d!=0){
						midi_display(rsb,keyno,d,0);
						pad_event(keyno,d);
					}
				}
			}
			else if(rsb==0xaf){
				if(dcb==0){ keyno=d; dcb=1; }
				else{
					dcb=0; 
					if(scene!=5){
						midi_display(rsb,keyno,d,0);
					}
					if(keyno==60){
						if((d>0)&&(d<10)){
							scene=d;
							scene_display();
						}
					}
					else if(keyno==61){
					    if((d>0)&&(d<9)){
						if(volume[d-1]!=0){
							dh=d-1; chaos_off();
							phase[dh]=1;
						}
					    }
					}
					else if(keyno==62) edit_ch=d;
					else if((keyno>62)&&(keyno<93)){
						edit_midi_rx(keyno,d);
					}
					else if(keyno==93){
						run_flag=2; window_draw(1);
					}
					else if(keyno==94){
						if(d==0) run_flag=0;
						else run_flag=3;
						ch_trig=1;
					}
					else if(keyno==95){
						if(d==0) bar_disp=0;
						else{
							bar_disp=d;
							bar_display(d);
						}
					}
					else if((keyno>102)&&(keyno<127)){
						edit_midi_rx(keyno,d);
					}
				}
			}
			else if(rsb==0xbf){
				if(dcb==0){ keyno=d; dcb=1; }
				else{
					dcb=0; midi_display(rsb,keyno,d,0);
					if((keyno==0x7f)&&(d==0)){
						run_flag=1; window_draw(1);
					}
				}
			}
			else if((rsb==0xcf)||(rsb==0xdf)){
				if((scene<4)||(scene>5)){
					midi_display(rsb,d,0,0);
				}
				if((run_flag==1)&&((d==0x7f)||(d==0))){
					put_data(0xfa); run_flag=0;
					window_draw(0);
				}
				else if((run_flag==2)&&((d==0x7f)||(d==0))){
					run_flag=0; window_draw(0);
				}
				else if((run_flag==3)&&((d==0x7f)||(d==0))){
					d=ch_trig;
					if(++ch_trig>4) ch_trig=1;
					if(volume[d]!=0) phase[d]=1;
				}
				else joy_to_grain(rsb,d);
			}
			else if((rsb>0xbf)&&(rsb<0xc8)){
				y=rsb-0xc0; program[y]=d; point_screen(5,y,1);
			}
			else if((rsb>0xaf)&&(rsb<0xb8)){
				if(dcb==0){ keyno=d; dcb=1; }
				else{
					dcb=0; midi_display(rsb,keyno,d,0);
					if(keyno==7){
						y=rsb-0xb0; volume[y]=d;
						point_screen(1,y,1);
					}
				}
			}
		}
		else if(job_fg[job_ct]!=0){
			ch=job_ct; each_channel();
			job_fg[job_ct]=0; job_ct=(job_ct+1)&0x07;
		}
		else if(kbhit()!=0){
			d=getch();
			if(d==0x1b) break;
			else if((d==0x20)||(d==0x0d)){
				if(run_flag==1){
					put_data(0xfa); run_flag=0;
					window_draw(0);
				}
				else if(run_flag==2){
					run_flag=0; window_draw(0);
				}
				else{
					for(i=0;i<8;i++){
						dh=i; chaos_off();
						phase[dh]=0; velocity[dh]=0;
					}
				}
			}
			else if((d>0x30)&&(d<0x39)){
				d=d-0x31;
				if(volume[d]!=0){
					dh=d; chaos_off(); phase[dh]=1;
				}
				else{
					dh=d; chaos_off(); phase[dh]=0;
					velocity[dh]=0;
				}
			}
			else if((d=='i')||(d=='I')){
				parameters_initial();
				screen_display(); pad_assign_display(0);
			}
			else if((d=='m')||(d=='M')){
				for(i=0;i<8;i++){
					dh=i; chaos_off();
					phase[dh]=0; velocity[dh]=0;
				}
			}
			else if((d=='b')||(d=='B')){
				put_data(0xaf); put_data(98); put_data(11);
			}
		}
		else{
			getmouse(&right,&left,&moux,&mouy);
			if((right==0)&&(left==0)) m_stat=0;
			else if((old_l==0)&&(left!=0)){
				m_stat=1; m_ctr=0; m_m=1;
			}
			else if((old_r==0)&&(right!=0)){
				m_stat=2; m_ctr=0; m_m=1;
			}
			else if(left!=0){
				if(m_ctr>10){ m_stat=1; m_ctr=0; m_m=0; }
			}
			else if(right!=0){
				if(m_ctr>10){ m_stat=2; m_ctr=0; m_m=0;}
			}
			if(m_stat==1){ m_stat=0; m_stat_1(m_m); }
			else if(m_stat==2){ m_stat=0; m_stat_2(m_m); }
			else{
				_settextposition(6,67);
				printf("(X=%03d,Y=%03d)",moux,mouy);
			}
			old_r=right; old_l=left; old_x=moux; old_y=mouy;
			pad_mark_display();
			midi_display(0,0,0,1);
		}
	}
	vsync_int_stop(); mouse(0);
	for(i=0;i<8;i++){
		put_data(0x80+midich[i]); put_data(note[i]); put_data(0);
	}
	put_data(0xfc);
	for(i=0;i<16;i++){ put_data(0xb0+i); put_data(0x7f); put_data(0); }
	put_command(0xff);
	parameter_file_write();
	for(i=0;i<4096;i++){ for(j=0;j<200;j++){} inp(0xe0d0); }
	_clearscreen(_GCLEARSCREEN); _displaycursor(_GCURSORON);
	_setvideomode (_DEFAULTMODE);
}

m_stat_1(m_m)
	int m_m;
{
	if((mouy>240)&&(mouy<253)) volume_change(0,m_m);
	else if((mouy>384)&&(mouy<397)) volume_change(4,m_m);
	else if((mouy>178)&&(mouy<192)) range_change(0,0);
	else if((mouy>322)&&(mouy<336)) range_change(4,0);
	else if((mouy>162)&&(mouy<176)) time_change(0,0);
	else if((mouy>306)&&(mouy<320)) time_change(4,0);
	else if((mouy>146)&&(mouy<160)) dwn_prg_change(0,0);
	else if((mouy>290)&&(mouy<304)) dwn_prg_change(4,0);
	else if((mouy>114)&&(mouy<128)) myu_change(0,0,m_m);
	else if((mouy>258)&&(mouy<272)) myu_change(4,0,m_m);
	else if((mouy>222)&&(mouy<238)) map_change(0,0);
	else if((mouy>366)&&(mouy<382)) map_change(4,0);
	else if((mouy>130)&&(mouy<144)) p1_p2_change(0,0);
	else if((mouy>274)&&(mouy<288)) p1_p2_change(4,0);
	else if((mouy>29)&&(mouy<51)) pad_change(0,0);
	else if((mouy>61)&&(mouy<83)) pad_change(4,0);
}

m_stat_2(m_m)
	int m_m;
{
	if((mouy>240)&&(mouy<253)) volume_change(0,m_m);
	else if((mouy>384)&&(mouy<397)) volume_change(4,m_m);
	else if((mouy>178)&&(mouy<192)) range_change(0,1);
	else if((mouy>322)&&(mouy<336)) range_change(4,1);
	else if((mouy>162)&&(mouy<176)) time_change(0,1);
	else if((mouy>306)&&(mouy<320)) time_change(4,1);
	else if((mouy>146)&&(mouy<160)) dwn_prg_change(0,1);
	else if((mouy>290)&&(mouy<304)) dwn_prg_change(4,1);
	else if((mouy>114)&&(mouy<128)) myu_change(0,1,m_m);
	else if((mouy>258)&&(mouy<272)) myu_change(4,1,m_m);
	else if((mouy>222)&&(mouy<238)) map_change(0,1);
	else if((mouy>366)&&(mouy<382)) map_change(4,1);
	else if((mouy>130)&&(mouy<144)) p1_p2_change(0,1);
	else if((mouy>274)&&(mouy<288)) p1_p2_change(4,1);
	else if((mouy>29)&&(mouy<51)) pad_change(0,1);
	else if((mouy>61)&&(mouy<83)) pad_change(4,1);
}

point_screen(mode,y,midi)
	int mode,y,midi;
{
	int i,j,k,l,xx,yy,xt,yt,xx_b,yy_b;
	char ss[4];
	xt=1+(y%4)*20; yt=4+(y/4)*9; xx=1+(y%4)*160; yy=65+(y/4)*144;
	if(mode==1){
		mouse(0); _setlinestyle(0xffff); _setcolor(0);
		_rectangle(_GF,xx+1,yy+173,xx+156,yy+189); _setcolor(7);
		xx_b=xx+6+(volume[y]/2)*143/64; yy_b=yy+176;
		_moveto(xx+7,yy_b+5); _lineto(xx+150,yy_b+5);
		_moveto(xx+7,yy_b+6); _lineto(xx+150,yy_b+6);
		i=0; _moveto(xx+7+11*i,yy_b+2); _lineto(xx+7+11*i,yy_b+9);
		i=13; _moveto(xx+7+11*i,yy_b+2); _lineto(xx+7+11*i,yy_b+9);
		for(i=1;i<13;i++){
			_moveto(xx+7+11*i,yy_b+3); _lineto(xx+7+11*i,yy_b+8);
		}
		_rectangle(_GF,xx_b,yy_b,xx_b+5,yy_b+11); _setcolor(0);
		_rectangle(_GF,xx_b+1,yy_b+1,xx_b+4,yy_b+10); _setcolor(7);
		_rectangle(_GF,xx_b+2,yy_b+2,xx_b+3,yy_b+9);
		if(midi==0){
			if((scene>6)&&(y==6)) put_data(0xbb);
			else put_data(0xb0+midich[y]);
			put_data(0x07); put_data(volume[y]);
		}
		mouse(1);
	}
	else if(mode==2){
		mouse(0); _setlinestyle(0xffff); _setcolor(0);
		_rectangle(_GF,xx+13,yy+131,xx+144,yy+157); _setcolor(7);
		_setlinestyle(0x6666);
		_rectangle(_GB,xx+12,yy+130,xx+145,yy+158);
		_setlinestyle(0xffff);
		for(i=0;i<7;i++){
			_rectangle(_GB,xx+12+19*i,yy+158,xx+31+19*i,yy+172);
		}
		yy_b=yy+159;
		xx_b=xx+31; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		xx_b=xx+50; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		xx_b=xx+88; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		xx_b=xx+107; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		xx_b=xx+126; _rectangle(_GF,xx_b-6,yy_b,xx_b+6,yy_b+7);
		yy_b=yy+156;
		_rectangle(_GB,xx+20,yy_b,xx+23,yy_b-filter[y][0]);
		_rectangle(_GF,xx+29,yy_b,xx+33,yy_b-filter[y][1]);
		_rectangle(_GB,xx+39,yy_b,xx+42,yy_b-filter[y][2]);
		_rectangle(_GF,xx+48,yy_b,xx+52,yy_b-filter[y][3]);
		_rectangle(_GB,xx+58,yy_b,xx+61,yy_b-filter[y][4]);
		_rectangle(_GB,xx+77,yy_b,xx+80,yy_b-filter[y][5]);
		_rectangle(_GF,xx+86,yy_b,xx+90,yy_b-filter[y][6]);
		_rectangle(_GB,xx+96,yy_b,xx+99,yy_b-filter[y][7]);
		_rectangle(_GF,xx+105,yy_b,xx+109,yy_b-filter[y][8]);
		_rectangle(_GB,xx+115,yy_b,xx+118,yy_b-filter[y][9]);
		_rectangle(_GF,xx+124,yy_b,xx+128,yy_b-filter[y][10]);
		_rectangle(_GB,xx+134,yy_b,xx+137,yy_b-filter[y][11]);
		mouse(1);
	}
	else if((mode==3)&&(midi==0)){
		_settextposition(yt+7,xt);
		printf(" low=%03d high=%03d",range[0][y],range[1][y]);
	}
	else if(mode==4){
		_settextposition(yt+6,xt);
		printf(" dly=%03d  stp=%03d",tim[0][y],tim[1][y]);
	}
	else if(mode==5){
		_settextposition(yt+5,xt);
		printf(" dwn=%03d  prg=%03d",down[y],program[y]);
		if(midi==0){
			put_data(0xc0+midich[y]); put_data(program[y]);
		}
	}
	else if(mode==6){
		ss[0]='1'+y; ss[1]=0;
		_settextposition(yt+3,xt);
		printf(" [%s]=%f <%d>",ss,myu[y],shift[y]+1);
	}
	else if(mode==8){
		_settextposition(yt+4,xt);
		printf(" int=%03d  vel=%03d",para1[y],velocity[y]);
	}
	else if(mode==0){
		mouse(0); _setlinestyle(0xffff); _setcolor(0);
		_rectangle(_GF,xx-1,yy+46,xx+158,yy+190); _setcolor(7);
		_rectangle(_GB,xx-1,yy+46,xx+158,yy+190);
		mouse(1);
	}
	if((mode==2)||(mode==3)){
		sum[y]=0; j=0;
		for(i=range[0][y];i7)&&(moux<152)) { of=0; x=8; }
	else if((moux>167)&&(moux<312)) { of=1; x=168; }
	else if((moux>327)&&(moux<472)) { of=2; x=328; }
	else if((moux>487)&&(moux<632)) { of=3; x=488; }
	else return(0);
	y=off+of;
	volume[y]=(moux-x)*127/143;
	point_screen(1,y,0);	/* Volume */
}

range_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>80)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>240)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>400)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>560)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(range[0][y]>0) range[0][y]--; }
	else if((z==0)&&(md==1)){ if(range[0][y]<127) range[0][y]++; }
	else if((z==1)&&(md==0)){ if(range[1][y]>0) range[1][y]--; }
	else if((z==1)&&(md==1)){ if(range[1][y]<127) range[1][y]++; }
	point_screen(3,y,0);	/* Range */
}

time_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>87)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>247)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>407)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>567)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(tim[0][y]>0) tim[0][y]--; }
	else if((z==0)&&(md==1)){ if(tim[0][y]<127) tim[0][y]++; }
	else if((z==1)&&(md==0)){ if(tim[1][y]>0) tim[1][y]--; }
	else if((z==1)&&(md==1)){ if(tim[1][y]<127) tim[1][y]++; }
	point_screen(4,y,0);	/* Delay, Step */
}

dwn_prg_change(off,md)
	int off,md;
{
	int of,y,z,zz;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>87)&&(moux<143)) { of=0; z=1; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>247)&&(moux<303)) { of=1; z=1; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>407)&&(moux<463)) { of=2; z=1; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else if((moux>567)&&(moux<623)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ zz=1; if(down[y]>0) down[y]--; }
	else if((z==0)&&(md==1)){ zz=1; if(down[y]<127) down[y]++; }
	else if((z==1)&&(md==0)){ zz=0; if(program[y]>0) program[y]--; }
	else if((z==1)&&(md==1)){ zz=0; if(program[y]<127) program[y]++; }
	point_screen(5,y,zz);	/* Down, Program */
}

p1_p2_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>15)&&(moux<71)) { of=0; z=0; }
	else if((moux>175)&&(moux<231)) { of=1; z=0; }
	else if((moux>335)&&(moux<391)) { of=2; z=0; }
	else if((moux>495)&&(moux<551)) { of=3; z=0; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){ if(para1[y]>0) para1[y]--; }
	else if((z==0)&&(md==1)){ if(para1[y]<127) para1[y]++; }
	point_screen(8,y,0);	/* Parameters */
}

myu_change(off,md,m_m)
	int off,md,m_m;
{
	int of,y,z;
	if((moux>15)&&(moux<111)) { of=0; z=0; }
	else if((moux>120)&&(moux<142)) { of=0; z=1; }
	else if((moux>175)&&(moux<271)) { of=1; z=0; }
	else if((moux>280)&&(moux<302)) { of=1; z=1; }
	else if((moux>335)&&(moux<431)) { of=2; z=0; }
	else if((moux>440)&&(moux<462)) { of=2; z=1; }
	else if((moux>495)&&(moux<591)) { of=3; z=0; }
	else if((moux>600)&&(moux<622)) { of=3; z=1; }
	else return(0);
	y=off+of;
	if((z==0)&&(md==0)){
		if(shift[y]==0) myu[y]=myu[y]-0.1;
		else if(shift[y]==1) myu[y]=myu[y]-0.01;
		else if(shift[y]==2) myu[y]=myu[y]-0.001;
		else if(shift[y]==3) myu[y]=myu[y]-0.0001;
		else if(shift[y]==4) myu[y]=myu[y]-0.00001;
		else if(shift[y]==5) myu[y]=myu[y]-0.000001;
		if(myu[y]<1.0) myu[y]=1.0;
	}
	else if((z==0)&&(md==1)){
		if(shift[y]==0) myu[y]=myu[y]+0.1;
		else if(shift[y]==1) myu[y]=myu[y]+0.01;
		else if(shift[y]==2) myu[y]=myu[y]+0.001;
		else if(shift[y]==3) myu[y]=myu[y]+0.0001;
		else if(shift[y]==4) myu[y]=myu[y]+0.00001;
		else if(shift[y]==5) myu[y]=myu[y]+0.000001;
		if(myu[y]>4.0) myu[y]=4.0;
	}
	else if((m_m!=0)&&(z==1)&&(md==0)){ if(shift[y]>0) shift[y]--; }
	else if((m_m!=0)&&(z==1)&&(md==1)){ if(shift[y]<5) shift[y]++; }
	point_screen(6,y,0);	/* Myu, Shift */
}

map_change(off,md)
	int off,md;
{
	int f,of,y,z,xx,yy;
	if(off==0) yy=mouy;
	else yy=mouy-144;
	if((moux>13)&&(moux<147)) { of=0; xx=moux; }
	else if((moux>172)&&(moux<307)) { of=1; xx=moux-160; }
	else if((moux>332)&&(moux<467)) { of=2; xx=moux-320; }
	else if((moux>492)&&(moux<627)) { of=3; xx=moux-480; }
	else return(0);
	if((yy<232)&&(xx>25)&&(xx<38)) z=1;
	else if((yy<232)&&(xx>44)&&(xx<58)) z=3;
	else if((yy<232)&&(xx>82)&&(xx<96)) z=6;
	else if((yy<232)&&(xx>101)&&(xx<115)) z=8;
	else if((yy<232)&&(xx>120)&&(xx<134)) z=10;
	else if(xx<32) z=0;
	else if(xx<51) z=2;
	else if(xx<70) z=4;
	else if(xx<89) z=5;
	else if(xx<108) z=7;
	else if(xx<127) z=9;
	else z=11;
	y=off+of;
	if(md==0){ if(filter[y][z]>0) filter[y][z]--; }
	else { if(filter[y][z]<24) filter[y][z]++; }
	point_screen(2,y,0);	/* Note Map */
}

screen_display(){
	int i,j,k,m0,m1,m2,m3,m4,m5,m6;
	for(j=0;j<8;j++){
		m0=data_buffer[0][j]; /* Myu over point */
		m1=data_buffer[1][j]; /* Myu (1) */
		m2=data_buffer[2][j]; /* Myu (2) */
		m3=data_buffer[3][j]; /* Myu (3) */
		m4=data_buffer[4][j]; /* Myu (4) */
		m5=data_buffer[5][j]; /* Myu (5) */
		m6=data_buffer[6][j]; /* Myu (6) */
		myu[j]=(double)m0;
		myu[j]=myu[j]+0.1*(double)m1;
		myu[j]=myu[j]+0.01*(double)m2;
		myu[j]=myu[j]+0.001*(double)m3;
		myu[j]=myu[j]+0.0001*(double)m4;
		myu[j]=myu[j]+0.00001*(double)m5;
		myu[j]=myu[j]+0.000001*(double)m6;
		shift[j]=data_buffer[7][j]; /* shift */
		down[j]=data_buffer[8][j]; /* down */
		program[j]=data_buffer[9][j]; /* program */
		volume[j]=data_buffer[10][j]; /* volume */
		para1[j]=data_buffer[12][j]; /* para(1) */
		range[0][j]=data_buffer[14][j]; /* range[0] low */
		range[1][j]=data_buffer[15][j]; /* range[1] high */
		tim[0][j]=data_buffer[16][j]; /* tim[0] Delay */
		tim[1][j]=data_buffer[17][j]; /* tim[1] Step */
		for(k=0;k<12;k++){
			filter[j][k]=data_buffer[18+k][j]; /* filter[k] */
		}
	}
	for(i=0;i<8;i++){
		point_screen(0,i,0);	/* Whole Frame */
		point_screen(6,i,0);	/* Myu, Shift */
		point_screen(8,i,0);	/* Parameters */
		point_screen(1,i,0);	/* Volume */
		point_screen(5,i,0);	/* Down, Program */
		point_screen(4,i,0);	/* Delay, Step */
		point_screen(3,i,0);	/* Range */
		point_screen(2,i,0);	/* Note Map */
	}
}

scene_display(){
	_settextposition(0,0);
	if(scene==1)      printf("【1】2  3  4  5  6  7  8  9  ");
	else if(scene==2) printf("  1【2】3  4  5  6  7  8  9  ");
	else if(scene==3) printf("  1  2【3】4  5  6  7  8  9  ");
	else if(scene==4) printf("  1  2  3【4】5  6  7  8  9  ");
	else if(scene==5) printf("  1  2  3  4【5】6  7  8  9  ");
	else if(scene==6) printf("  1  2  3  4  5【6】7  8  9  ");
	else if(scene==7) printf("  1  2  3  4  5  6【7】8  9  ");
	else if(scene==8) printf("  1  2  3  4  5  6  7【8】9  ");
	else if(scene==9) printf("  1  2  3  4  5  6  7  8【9】");
}

myu_decode(j)
	int j;
{
	double myu_buff;
	int i,m;
	myu_buff=myu[j];
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[0][j]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[1][j]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[2][j]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[3][j]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[4][j]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[5][j]=m;
	m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
	data_buffer[6][j]=m;
}

myu_encode(j)
	int j;
{
	int i,m0,m1,m2,m3,m4,m5,m6;
	m0=data_buffer[0][j]; /* Myu over point */
	m1=data_buffer[1][j]; /* Myu (1) */
	m2=data_buffer[2][j]; /* Myu (2) */
	m3=data_buffer[3][j]; /* Myu (3) */
	m4=data_buffer[4][j]; /* Myu (4) */
	m5=data_buffer[5][j]; /* Myu (5) */
	m6=data_buffer[6][j]; /* Myu (6) */
	myu[j]=(double)m0;
	myu[j]=myu[j]+0.1*(double)m1;
	myu[j]=myu[j]+0.01*(double)m2;
	myu[j]=myu[j]+0.001*(double)m3;
	myu[j]=myu[j]+0.0001*(double)m4;
	myu[j]=myu[j]+0.00001*(double)m5;
	myu[j]=myu[j]+0.000001*(double)m6;
}

edit_midi_rx(sel,data)
	int sel,data;
{
	int i,j,k,m0,m1,m2,m3,m4,m5,m6;
	if((sel>102)&&(sel<111)){
		k=sel-103; pad_assign[0][k]=data;
		pad_assign_display(1); return(0);
	}
	else if((sel>110)&&(sel<119)){
		k=sel-111; pad_assign[1][k]=data;
		pad_assign_display(1); return(0);
	}
	else if((sel>118)&&(sel<127)){
		k=sel-119; pad_assign[2][k]=1+(data-1)%7;
		pad_assign_display(1); return(0);
	}
	if((edit_ch<1)||(edit_ch>8)) return(0);
	j=edit_ch-1;
	if((sel>63)&&(sel<70)){
		k=sel-63; myu_decode(j); data_buffer[k][j]=data;
		myu_encode(j); point_screen(6,j,0); return(0);
	}
	else if((sel>80)&&(sel<93)){
		k=sel-81; filter[j][k]=data;
		point_screen(2,j,0); return(0);
	}
	switch(sel){
		case (63):
			myu_decode(j);
			data_buffer[0][j]=data; /* Myu over point */
			myu_encode(j);
			point_screen(6,j,0);	/* Myu, Shift */
			break;
		case (70):
			shift[j]=data;
			point_screen(6,j,0);	/* Myu, Shift */
			break;
		case (71):
			down[j]=data;
			point_screen(5,j,0);	/* Down, Program */
			break;
		case (72):
			program[j]=data;
			point_screen(5,j,0);	/* Down, Program */
			break;
		case (73):
			volume[j]=data;
			point_screen(1,j,0);	/* Volume */
			break;
		case (75):
			para1[j]=data;
			point_screen(8,j,0);	/* Parameters */
			break;
		case (77):
			range[0][j]=data;
			point_screen(3,j,0);	/* Range */
			break;
		case (78):
			range[1][j]=data;
			point_screen(3,j,0);	/* Range */
			break;
		case (79):
			tim[0][j]=data;
			point_screen(4,j,0);	/* Delay, Step */
			break;
		case (80):
			tim[1][j]=data;
			point_screen(4,j,0);	/* Delay, Step */
			break;
	}
}

midi_display(status,keyno,data,u_d)
	int status,keyno,data,u_d;
{
	int j,fifo_buff;
	if(u_d==0){
		fifo_buff=fifo_pointer;
		midi_fifo[0][fifo_pointer]=status;
		midi_fifo[1][fifo_pointer]=keyno;
		midi_fifo[2][fifo_pointer]=data;
		fifo_pointer++;
		if(fifo_pointer>9999) fifo_pointer=0;
		if(fifo_pointer==read_pointer) fifo_pointer=fifo_buff;
		return(0);
	}
	if(fifo_pointer==read_pointer) return(0);
	status=midi_fifo[0][read_pointer];
	keyno=midi_fifo[1][read_pointer];
	data=midi_fifo[2][read_pointer];
	read_pointer++;
	if(read_pointer>9999) read_pointer=0;
	if((status>0xaf)&&(status<0xb8)){
		_settextposition(0,52);
		printf("[%02X-%02X-%02X]",status,keyno,data); return(0);
	}
	switch(status){
		case(0x9f):
			_settextposition(0,30);
			printf("[%02X-%02X-%02X]",status,keyno,data);
			break;
		case(0xaf):
			_settextposition(0,41);
			printf("[%02X-%02X-%02X]",status,keyno,data);
			break;
		case(0xbf):
			_settextposition(0,52);
			printf("[%02X-%02X-%02X]",status,keyno,data);
			break;
		case(0xcf):
			_settextposition(0,63);
			printf("[%02X-%02X]",status,keyno);
			break;
		case(0xdf):
			_settextposition(0,71);
			printf("[%02X-%02X]",status,keyno);
			break;
	}
}

window_draw(d)
	int d;
{
	int w1x=7,w1y=197,w2x=632,w2y=234;
	if(d==0){
		_settextposition(13,15);
		printf("                                                   ");
		mouse(0);
		_putimage(w1x,w1y,wind,_GPSET);
		mouse(1);
		return(0);
	}
	mouse(0);
	_getimage(w1x,w1y,w2x,w2y,wind);
	_setcolor(0); _rectangle(_GFILLINTERIOR,w1x,w1y,w2x,w2y);
	_setcolor(7); _rectangle(_GFILLINTERIOR,w1x+4,w1y+4,w2x-4,w2y-4);
	_setcolor(0); _rectangle(_GFILLINTERIOR,w1x+8,w1y+8,w2x-8,w2y-8);
	_setcolor(7);
	mouse(1);
	_settextposition(13,15);
	printf("System is waiting ... Plaese Cueing with JOY/KBD !!");
}

bar_display(d)
	int d;
{
	_settextposition(2,61); printf("★★ ");
	if(d>127) char_disp(10);
	else if(d>99) char_disp(1);
	else char_disp(0);
	d=d-100*(d/100); char_disp(d/10); char_disp(d%10); printf(" ★★");
}

char_disp(x)
	int x;
{
	if(x==0) printf("0");
	else if(x==1) printf("1");
	else if(x==2) printf("2");
	else if(x==3) printf("3");
	else if(x==4) printf("4");
	else if(x==5) printf("5");
	else if(x==6) printf("6");
	else if(x==7) printf("7");
	else if(x==8) printf("8");
	else if(x==9) printf("9");
	else printf("*");
}

parameter_file_write(){
	double myu_buff;
	int j,k,m;
	for(j=0;j<8;j++){
		myu_buff=myu[j];
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[0][j]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[1][j]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[2][j]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[3][j]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[4][j]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[5][j]=m;
		m=(int)myu_buff; myu_buff=10.0*(myu_buff-(double)m);
		data_buffer[6][j]=m;
		data_buffer[7][j]=shift[j];
		data_buffer[8][j]=down[j];
		data_buffer[9][j]=program[j];
		data_buffer[10][j]=volume[j];
		data_buffer[12][j]=para1[j];
		data_buffer[14][j]=range[0][j];
		data_buffer[15][j]=range[1][j];
		data_buffer[16][j]=tim[0][j];
		data_buffer[17][j]=tim[1][j];
		for(k=0;k<12;k++){
			data_buffer[18+k][j]=filter[j][k];
		}
	}
	fds=fopen("chaosgen.dat","wb");
	for(j=0;j<8;j++){
		for(k=0;k<30;k++) fputc(data_buffer[k][j],fds);
		fputc(0xff,fds); fputc(0xff,fds);
	}
	for(j=0;j<8;j++){
		for(k=0;k<3;k++) fputc(pad_assign[k][j],fds);
		fputc(0xff,fds);
	}
	fclose(fds);
}

parameters_initial(){
	int j,k;
	for(j=0;j<8;j++){
		data_buffer[0][j]=3;
		for(k=1;k<13;k++) data_buffer[k][j]=0;
		data_buffer[14][j]=40; /* range[0] low */
		data_buffer[15][j]=80; /* range[1] high */
		data_buffer[16][j]=0; /* tim[0] Delay */
		for(k=17;k<30;k++) data_buffer[k][j]=10;
		pad_assign[0][j]=0x20;
		pad_assign[1][j]=0x20;
		pad_assign[2][j]=0;
	}
}

parameter_file_read(){
	int j,k;
	if((fds=fopen("chaosgen.dat","rb"))==NULL) parameters_initial();
	else{
		for(j=0;j<8;j++){
			for(k=0;k<30;k++) data_buffer[k][j]=fgetc(fds);
			fgetc(fds); fgetc(fds);
		}
		for(j=0;j<8;j++){
			for(k=0;k<3;k++) pad_assign[k][j]=fgetc(fds);
			fgetc(fds);
		}
		fclose(fds);
	}
}

chaos_on(){
	double mpy;
	chaos[dh]=myu[dh]*chaos[dh]*(1.0-chaos[dh]);
	mpy=(double)sum[dh]*chaos[dh]; note[dh]=(int)ref_table[dh][(int)mpy];
	if((scene>6)&&(dh==6)){
		note[dh]=drums[note[dh]%12]; put_data(0x9b);
	}
	else put_data(0x90+midich[dh]);
	put_data(note[dh]); put_data(velocity[dh]);
	point_screen(8,dh,0); on_off[dh]=1;
}

chaos_off(){
	if(on_off[dh]==1){
		if((scene>6)&&(dh==6)) put_data(0x8b);
		else put_data(0x80+midich[dh]);
		put_data(note[dh]); put_data(0x00); on_off[dh]=0;
	}
}

each_channel(){
	int z;
	dh=ch;
	if(phase[ch]==0){ counter[ch]=0; }
	else if(phase[ch]==1){
		if(++counter[ch]>tim[0][ch]){
			z=40+(rand()%20); chaos[ch]=(double)z/100.0;
			phase[ch]=2;
		}
	}
	else if(phase[ch]==2){ velocity[ch]=0x7f; chaos_on(); phase[ch]=3; }
	else if(phase[ch]==3){ counter[ch]=0; chaos_off(); phase[ch]=4; }
	else if(phase[ch]==4){ if(++counter[ch]>tim[1][ch]) phase[ch]=5; }
	else if(phase[ch]==5){
		if((rand()%127)>(127-para1[ch])){
			counter[ch]=0; phase[ch]=4; return(0);
		}
		velocity[ch]=velocity[ch]-down[ch];
		if(velocity[ch]<1){ velocity[ch]=0; phase[ch]=0; }
		else { chaos_on(); phase[ch]=3; }
	}
}

pad_change(off,md)
	int off,md;
{
	int of,y,z;
	if((moux>18)&&(moux<44)) { of=0; z=0; }
	else if((moux>50)&&(moux<76)) { of=1; z=0; }
	else if((moux>82)&&(moux<108)) { of=2; z=0; }
	else if((moux>114)&&(moux<140)) { of=3; z=0; }
	else if((moux>170)&&(moux<196)) { of=0; z=1; }
	else if((moux>202)&&(moux<228)) { of=1; z=1; }
	else if((moux>234)&&(moux<260)) { of=2; z=1; }
	else if((moux>266)&&(moux<292)) { of=3; z=1; }
	else if((moux>322)&&(moux<347)) { of=0; z=2; }
	else if((moux>354)&&(moux<379)) { of=1; z=2; }
	else if((moux>386)&&(moux<411)) { of=2; z=2; }
	else if((moux>418)&&(moux<443)) { of=3; z=2; }
	else return(0);
	y=off+of;
	if(z==2){
		if(md==0){ if(pad_assign[z][y]>0) pad_assign[z][y]--; }
		else if(md==1){ if(pad_assign[z][y]<8) pad_assign[z][y]++; }
	}
	else{
		if(md==0){ if(pad_assign[z][y]>0) pad_assign[z][y]--; }
		else if(md==1){ if(pad_assign[z][y]<127) pad_assign[z][y]++; }
	}
	pad_assign_display(1);
	if(debug!=0){
		if((z==0)&&(y==0)) cg_debug(0);
		else if((z==0)&&(y==4)) cg_debug(0);
		else if((z==1)&&(y==0)) cg_debug(1);
		else if((z==1)&&(y==4)) cg_debug(1);
	}
}

pad_assign_display(mode)
	int mode;
{
	int i,j,xx,yy,k=0;
	for(j=0;j<3;j++){
		if(j==2) k=1;
		if(mode==0){
			mouse(0); _setlinestyle(0xffff); _setcolor(7);
			_rectangle(_GB,13+152*j,24,145+152*j-k,88);
			for(i=0;i<8;i++){
				xx=32*(i%4)+18+152*j; yy=32*(i/4)+29;
				_rectangle(_GB,xx,yy,xx+26-k,yy+22);
			}
			mouse(1);
		}
		for(i=0;i<8;i++){
			_settextposition(2*(i/4)+2,4*(i%4)+3+19*j);
			if(j==2) char_disp(pad_assign[j][i]);
			else printf("%02X",pad_assign[j][i]);
		}
	}
}

pad_paint(no,mode)
	int no,mode;
{
	int j,xx,yy,k=0;
	for(j=0;j<3;j++){
		if(j==2) k=1;
		xx=32*(no%4)+18+152*j; yy=32*(no/4)+29;
		mouse(0); _setlinestyle(0xffff);
		if(mode==0){
			_setcolor(0); _rectangle(_GF,xx,yy,xx+26-k,yy+22);
			_setcolor(7); _rectangle(_GB,xx,yy,xx+26-k,yy+22);
		}
		else if(mode==1){
			_setcolor(7); _rectangle(_GF,xx,yy,xx+26-k,yy+22);
		}
		mouse(1);
	}
}

pad_mark_display(){
	int i;
	for(i=0;i<8;i++){
		if(pad_mark[i]!=0){
			pad_mark[i]--;
			if(pad_mark[i]==0) pad_paint(i,0);
		}
	}
}

pad_event(keyno,velo)
	int keyno,velo;
{
	int d,z,v,yura;
	if((keyno>0x64)&&(keyno<0x6d)){
		z=keyno-0x65;
		pad_mark[z]=20; pad_paint(z,1);
		if((scene<4)||(scene>5)){
			put_data(0x99);
			put_data(pad_assign[0][z]); put_data(velo);
			put_data(0x9b);
			put_data(pad_assign[1][z]); put_data(velo);
			put_data(0x89);
			put_data(pad_assign[0][z]); put_data(0);
			put_data(0x8b);
			put_data(pad_assign[1][z]); put_data(0);
		}
		if(pad_assign[2][z]!=0){
			d=pad_assign[2][z]-1;
			if(volume[d]!=0){ dh=d; chaos_off(); phase[dh]=1; }
			else{ dh=d; chaos_off(); phase[dh]=0; velocity[dh]=0; }
		}
		if(scene==2){
			if((keyno==0x65)||(keyno==0x68)){
				put_data(0xaf); put_data(97);
				pad_para[0]=(pad_para[0]+3)%5+1;
				put_data(pad_para[0]);
			}
		}
		else if((scene==4)||(scene==5)){
			put_data(0x98); put_data(pad_note[z]); put_data(velo);
			put_data(0x88); put_data(pad_note[z]); put_data(0);
			put_data(0xaf); put_data(101);
			put_data(0x40+8*pad_cg[z]);
			if(keyno==0x68){
				put_data(0xaf); put_data(97); put_data(26);
			}
			else if(keyno==0x66){
				yura=rand()%100;
				if(yura<33){
					put_data(0xaf); put_data(97);
					put_data(27);
				}
			}
		}
		else if(scene>6){
			if(scene==7){
				pad_para[1]=(pad_para[1]+3)%5+1;
				if(keyno<0x69) d=pad_para[1]+5;
				else d=pad_para[1]+10;
			}
			else if(scene==8){
				pad_para[1]=(pad_para[1]+3)%5+1;
				if(keyno<0x67) d=pad_para[1]+5;
				else if(keyno<0x69) d=pad_para[1]+10;
				else if(keyno<0x6b) d=pad_para[1]+15;
				else d=pad_para[1]+20;
			}
			else{
				pad_para[1]=(pad_para[1]+8)%25+1;
				d=pad_para[1];
			}
			put_data(0xaf); put_data(97); put_data(d);
		}
	}
	else if((keyno>0x22)&&(keyno<0x45)){
		if(scene==3){
			cond_pad[0]=(cond_pad[0]+1)%3;
			if(cond_pad[0]==0){ z=54; v=112; }
			else if(cond_pad[0]==1){ z=70; v=112; }
			else { z=73; v=112; }
			put_data(0x9b);	put_data(z); put_data(v);
			put_data(0x8b);	put_data(z); put_data(0);
			cond_pad[1]=(cond_pad[1]+53)%64;
			put_data(0xaf);	put_data(101); put_data(cond_pad[1]);
			cond_pad[2]=(cond_pad[2]+1)%2; d=6+cond_pad[2];
			if(volume[d]!=0){ dh=d; chaos_off(); phase[dh]=1; }
		}
		else if(scene>6){
			cond_pad[3]=(cond_pad[3]+1)%2;
			myu_decode(6); data_buffer[1][6]=5+3*cond_pad[3];
			myu_encode(6); point_screen(6,6,0);
			cond_pad[4]=(cond_pad[4]+1)%3;
			d=scene-6+3*cond_pad[4];
			put_data(0xaf);	put_data(98); put_data(d);
			d=3+2*cond_pad[3];
			if(volume[d]!=0){ dh=d; chaos_off(); phase[dh]=1; }
		}
	}
}

cg_debug(mode)
	int mode;
{
	int aa,bb,cc;
	aa=0x9c+mode; bb=pad_assign[mode][0]; cc=pad_assign[mode][4];
	if(mode==0){
		_settextposition(6,3);
		printf("CG(1) Performer : %02X %02X %02X",aa,bb,cc);
	}
	else{
		_settextposition(6,33);
		printf("CG(2) Bars & Pipes : %02X %02X %02X",aa,bb,cc);
	}
	put_data(aa); put_data(bb); put_data(0x4c);
	put_data(aa+0x50); put_data(00); put_data(cc);
}

joy_to_grain(rsb,data)
	int rsb,data;
{
	if(scene==4){
		if((data%2)==0) return(0);
		if(rsb==0xcf) range[1][1]=64+data/4;
		else range[0][1]=58-(127-data)/5;
		point_screen(3,1,1);
	}
	else if(scene==5){
		if((data%3)!=0) return(0);
		if(rsb==0xcf){
			put_data(0xaf); put_data(19); put_data(data);
			put_data(29); put_data(127-data);
		}
		else{
			put_data(0xaf); put_data(14); put_data(data);
			put_data(24); put_data(data);
		}
	}
}

パソコンのC その6 アトラクタシンセシスの実験

#include 
#include 
#include 
#include 

int	k[4],wave[4096],r_l,u_d,grow,dir;
double	a[4],b[4],c[4],d[4];

main(){
	int z,i;
	double s,t,u,v,w,th;
	display_setting(0);
	for(i=0;i<4096;i++){
		th=(double)i*3.1415926535/128.0;
		s=sin(th*55.0)*0.1;
		t=sin(th*36.0)*0.15;
		u=sin(th*25.0)*0.2;
		v=sin(th*13.0)*0.25;
		w=sin(th*1.0)*1.0;
		wave[i]=(int)(32767.9*(s+t+u+v+w)/2.0);
	}
	a[0]=0.5; b[0]=0.5; c[0]=0.5; d[0]=0.5; k[0]=3;
	a[1]=0.5; b[1]=0.5; c[1]=0.5; d[1]=-0.5; k[1]=3;
	a[2]=0.5; b[2]=0.5; c[2]=-0.5; d[2]=-0.5; k[2]=3;
	a[3]=0.5; b[3]=-0.5; c[3]=-0.5; d[3]=-0.5; k[3]=3;
	r_l=0; wave_display(0); keyboard(0);
	for(i=0;i<4;i++){ u_d=i; wave_display(1); }
	u_d=0; grow=1; dir=1;
	while(1){
		if(kbhit()!=0){
			z=getch();
			if(z==0x1b) break;
			else keyboard(z);
		}
		else wave_display(1);
	}
	display_setting(1);
}

keyboard(data)
	int data;
{
	int i,j;
	if(data==0x0b){ u_d=u_d-1; if(u_d<0) u_d=3; }
	else if(data==0x0a){ u_d=u_d+1; if(u_d>3) u_d=0; }
	else if(data==0x0c){ r_l=r_l+1; if(r_l>4) r_l=0; }
	else if(data==0x08){ r_l=r_l-1; if(r_l<0) r_l=4; }
	else if(data==0x20){ grow=(grow+1)&1; }
	for(j=0;j<4;j++){
		for(i=0;i<5;i++){
			_settextposition(6+j*5,1+i*15); printf("  ");
		}
	}
	_settextposition(6+u_d*5,1+r_l*15); printf("##");
	if(data==0){
		for(j=0;j<4;j++){
			i=0; _settextposition(6+j*5,4+i*15);
			printf("        ");
			_settextposition(6+j*5,4+i*15);
			printf("K=%d",k[j]);
			i=1; _settextposition(6+j*5,4+i*15);
			printf("        ");
			_settextposition(6+j*5,4+i*15);
			printf("a=%f",a[j]);
			i=2; _settextposition(6+j*5,4+i*15);
			printf("        ");
			_settextposition(6+j*5,4+i*15);
			printf("b=%f",b[j]);
			i=3; _settextposition(6+j*5,4+i*15);
			printf("        ");
			_settextposition(6+j*5,4+i*15);
			printf("c=%f",c[j]);
			i=4; _settextposition(6+j*5,4+i*15);
			printf("        ");
			_settextposition(6+j*5,4+i*15);
			printf("d=%f",d[j]);
		}
	}
	else if((grow==1)&&(data==0x0d)){
		j=u_d; i=r_l;
		_settextposition(6+j*5,4+i*15);
		printf("        ");
		_settextposition(6+j*5,4+i*15);
		if(i==0){
			k[j]=k[j]+1; if(k[j]>128) k[j]=1;
			printf("K=%d",k[j]);
		}
		else if(i==1){
			a[j]=a[j]+0.1; if(a[j]>1.0) a[j]=-1.0;
			printf("a=%f",a[j]);
		}
		else if(i==2){
			b[j]=b[j]+0.1; if(b[j]>1.0) b[j]=-1.0;
			printf("b=%f",b[j]);
		}
		else if(i==3){
			c[j]=c[j]+0.1; if(c[j]>1.0) c[j]=-1.0;
			printf("c=%f",c[j]);
		}
		else{
			d[j]=d[j]+0.1; if(d[j]>1.0) d[j]=-1.0;
			printf("d=%f",d[j]);
		}
	}
}

wave_display(mode)
	int mode;
{
	int v_point;
	int i,j,l,x,y,ox,oy,p,q,r,s;
	double e,f,g,h,u;
	if(mode==0){
		_clearscreen(_GCLEARTEXT); _clearscreen(_GCLEARSCREEN);
		ox=0; oy=0; v_point=60;
		for(i=0;i<512;i++){
			x=i; y=wave[i]/656;
			_moveto(ox+50,v_point-oy); _lineto(x+50,v_point-y);
			ox=x; oy=y;
		}
		return(0);
	}
	if(grow==0){
		j=u_d; i=r_l;
		_settextposition(6+j*5,4+i*15);
		printf("        ");
		_settextposition(6+j*5,4+i*15);
		if(i==0){
			if(dir==1){ k[j]=k[j]+1; if(k[j]>127) dir=0; }
			else{ k[j]=k[j]-1; if(k[j]<2) dir=1; }
			printf("K=%d",k[j]);
		}
		else if(i==1){
			if(dir==1){ a[j]=a[j]+0.1; if(a[j]>0.999) dir=0; }
			else{ a[j]=a[j]-0.1; if(a[j]<-0.999) dir=1; }
			printf("a=%f",a[j]);
		}
		else if(i==2){
			if(dir==1){ b[j]=b[j]+0.1; if(b[j]>0.999) dir=0; }
			else{ b[j]=b[j]-0.1; if(b[j]<-0.999) dir=1; }
			printf("b=%f",b[j]);
		}
		else if(i==3){
			if(dir==1){ c[j]=c[j]+0.1; if(c[j]>0.999) dir=0; }
			else{ c[j]=c[j]-0.1; if(c[j]<-0.999) dir=1; }
			printf("c=%f",c[j]);
		}
		else{
			if(dir==1){ d[j]=d[j]+0.1; if(d[j]>0.999) dir=0; }
			else{ d[j]=d[j]-0.1; if(d[j]<-0.999) dir=1; }
			printf("d=%f",d[j]);
		}
	}
	j=u_d; e=a[j]; f=b[j]; g=c[j]; h=d[j]; l=k[j];
	ox=0; oy=0; v_point=140+j*70;
	for(i=0;i<512;i++){
		_setcolor(0);
		_moveto(i+50,v_point-35); _lineto(i+50,v_point+35);
		_setcolor(7);
		p=wave[i]; q=wave[i+l]; r=wave[i+l*2]; s=wave[i+l*3];
		u=e*(double)p+f*(double)q+g*(double)r+h*(double)s;
		x=i; y=(int)(u/sqrt(e*e+f*f+g*g+h*h)/1312.0);
		_moveto(ox+50,v_point-oy); _lineto(x+50,v_point-y);
		ox=x; oy=y;
	}
}

display_setting(x)
	int x;
{
	if(x==0){
		_setvideomode(_98RESS16COLOR); _displaycursor(_GCURSOROFF);
		_clearscreen(_GCLEARTEXT); _clearscreen(_GCLEARSCREEN);
	}
	else{
		_clearscreen(_GCLEARSCREEN); _displaycursor(_GCURSORON);
		_setvideomode (_DEFAULTMODE);
	}
}

パソコンのC その7 ニューラルネットによるグラニュラシンセサイザ制御

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define	INPUTUNIT	2
#define	HIDDENUNIT	16
#define	OUTPUTUNIT	2
#define	BIASUNIT	1
#define	ERROR		-1

#define	STATPT  	0xe0d2
#define DATAPT  	0xe0d0
#define DRR     	0x0040
#define DSR     	0x0080
#define PUT		8888
#define GET		9999

#define entry_table		0x0080
#define vector_offset_wave 	1
#define vector_offset_w_block	2
#define vector_offset_user_com	10
#define vector_offset_w_bank   	16

static	union REGS inregs,outregs;
static 	struct SREGS segregs;
int	moux,mouy,right,left,old_l,old_r,old_x,old_y;

double	Eta=0.8,Alpha=0.7;
time_t	ltime;
FILE	*fds,*fdo;

double	pattern[16][INPUTUNIT],teacher[16][OUTPUTUNIT],wc[512];
char	dum[50],number[2],name[16];
int	num,ww,wk,wt,kn[16],le[16],xp[16],yp[16];
unsigned char wb[512],wd[512];

unsigned char patch[577] = {
	0x1C,0x66,0x29,0xEB,0xE1,0xFE,0x37,0x00,0xEB,0xE3,0xFE,0x37,
	0x00,0xEB,0xE2,0xFE,0x37,0x00,0xEB,0xE4,0xFE,0x37,0x00,0x1C,
	0xA4,0x41,0x1C,0x20,0x41,0xC8,0xF8,0x47,0x7F,0x77,0x7D,0xFE,
	0xD6,0x39,0x00,0x70,0xF9,0x70,0xE2,0x28,0xEF,0x76,0x20,0x97,
	0x7F,0xAC,0x80,0xC6,0x04,0x3F,0x7F,0x00,0x00,0xF8,0xAF,0xC6,
	0x1F,0xF8,0x6F,0xF0,0xC7,0x0B,0xF8,0x6F,0xF8,0xFE,0xD6,0xFE,
	0xDF,0x37,0x78,0x00,0x1E,0x20,0xF8,0x6C,0xF0,0xEF,0x78,0x20,
	0x6C,0x0F,0x2F,0x77,0x37,0x7A,0x00,0x1E,0x27,0x78,0x6F,0x00,
	0xFE,0xD6,0xEF,0x7A,0x6F,0x00,0xCE,0x0C,0x6F,0xC0,0xC6,0x0F,
	0x6F,0xD0,0xC6,0x1F,0x37,0x7A,0x01,0x1E,0x37,0x7A,0x00,0x37,
	0x78,0x00,0x1E,0xEF,0x77,0x6F,0x0F,0xFE,0xDE,0x3A,0x00,0x68,
	0xE3,0xE1,0xFE,0x2E,0xF7,0x20,0xE3,0xE1,0xFE,0x87,0x1E,0xEF,
	0x77,0x6F,0x0F,0xFE,0xDE,0x3A,0x00,0x6A,0xE3,0xE3,0xFE,0x2E,
	0xF7,0x20,0xE3,0xE3,0xFE,0x87,0x1E,0xA8,0x62,0xCE,0x08,0xA9,
	0x62,0xFE,0xD6,0xB1,0x62,0xC8,0x08,0xB0,0x62,0x1C,0x46,0x04,
	0x1C,0xBB,0x41,0x27,0xF1,0x1E,0x27,0x5F,0x6C,0xF0,0xA7,0xA7,
	0xA7,0x3A,0xC9,0x41,0xF3,0x4A,0xEA,0xC8,0x58,0x11,0x58,0x11,
	0x58,0x11,0x41,0x12,0xE9,0x12,0xE9,0x12,0xE9,0x12,0xE9,0x12,
	0x7F,0x13,0xDC,0x13,0xE9,0x41,0x8C,0x14,0xF2,0x14,0x3F,0x15,
	0x1C,0x16,0xC3,0x16,0x27,0x5F,0xFE,0xAB,0xEB,0x19,0x14,0xC6,
	0x6C,0x07,0xA6,0x3A,0xFB,0x41,0xF3,0x4A,0xEA,0xC8,0x1C,0x43,
	0x51,0x42,0x0B,0x42,0x2A,0x14,0x33,0x14,0x41,0x14,0x4F,0x14,
	0x84,0x14,0xE3,0xE2,0xFE,0x2E,0xE3,0xE1,0xFE,0x67,0xCE,0x10,
	0xE3,0xE4,0xFE,0x2E,0xE3,0xE3,0xFE,0x67,0xCE,0x1C,0x30,0xFF,
	0x1C,0x48,0x04,0x1E,0x30,0xCF,0x1C,0x48,0x04,0x3A,0x00,0x68,
	0xE3,0xE2,0xFE,0x2E,0xF3,0x28,0xE3,0xE2,0xFE,0x87,0x1C,0x48,
	0x04,0x1E,0x30,0xDF,0x1C,0x48,0x04,0x3A,0x00,0x6A,0xE3,0xE4,
	0xFE,0x2E,0xF3,0x28,0xE3,0xE4,0xFE,0x87,0x1C,0x48,0x04,0x1E,
	0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,0x27,0x60,0xEB,0xE5,
	0xFE,0x26,0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,0x27,0x60,
	0xEB,0xE6,0xFE,0x26,0x33,0x00,0x1C,0x79,0x42,0x83,0xFB,0x6F,
	0x10,0xCE,0xF7,0x1E,0x30,0xFA,0xFB,0x31,0xF9,0x6E,0xC0,0xE8,
	0x37,0xF0,0xFB,0x31,0xF9,0x6E,0xD0,0xE8,0x37,0x0F,0xE3,0xE6,
	0xFE,0x28,0x31,0x48,0xF8,0x6A,0x02,0x35,0x20,0xF8,0x12,0x30,
	0x00,0xF8,0x70,0x31,0x08,0xF8,0x70,0xFC,0x6E,0x90,0x30,0xFA,
	0xE2,0x2E,0xFB,0x31,0xE8,0x26,0xFC,0x6C,0x0F,0xFC,0x6E,0xA0,
	0xE2,0x2E,0xFB,0x31,0xF9,0x6E,0x10,0xE8,0x26,0xFC,0x6E,0x10,
	0xE2,0x2E,0xFB,0x31,0xF9,0x6E,0x20,0xE8,0x26,0xFB,0x31,0xF9,
	0x6E,0x30,0xE8,0x37,0x00,0xFB,0x31,0xF9,0x6E,0x40,0xE8,0x37,
	0x00,0xE3,0xE5,0xFE,0x2E,0xFB,0x60,0xFB,0x31,0xF9,0x6E,0x50,
	0xE8,0x26,0xFB,0x31,0xF9,0x6E,0x60,0xE8,0x37,0x80,0xFB,0x31,
	0xF9,0x6E,0x70,0xE8,0x37,0x00,0xFB,0x31,0xF9,0x6E,0xA0,0xE8,
	0x37,0x0F,0xFB,0x31,0xF9,0x6E,0xB0,0xE8,0x37,0x0F,0xFB,0x31,
	0xF9,0x6E,0xC0,0xE8,0x37,0x0F,0xFB,0x31,0xF9,0x6E,0xD0,0xE8,
	0x37,0x0F,0xFB,0x31,0xF9,0x6E,0x90,0xE8,0x37,0x03,0x1E,0x33,
	0x00,0x1C,0x27,0x43,0x83,0xFB,0x6F,0x10,0xCE,0xF7,0x30,0xFA,
	0xFB,0x31,0xF9,0x6E,0xC0,0xE8,0x37,0xF0,0xFB,0x31,0xF9,0x6E,
	0xD0,0xE8,0x37,0x0F,0xFB,0x31,0xF9,0x6E,0x90,0xE8,0x37,0x00,
	0x1E };
unsigned char speed_get_7[66] = {
	0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,0xE7,0x60,0x28,0x27,0xF1,0xA9,
	0x62,0xC6,0xFC,0xB1,0x62,0xE7,0x60,0x29,0x27,0xF1,0xA9,0x62,0xC6,0xFC,
	0xB1,0x62,0xE7,0x60,0x2C,0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,0xE7,
	0x60,0x2D,0x7F,0x00,0x00,0xFE,0xD6,0xAC,0xC6,0xC6,0xFC,0xE0,0x2E,0x2F,
	0xFE,0x27,0xF0,0x90,0x7A,0x01,0x00,0xCE,0xF0,0x1E };
unsigned char speed_put_8[68] = {
	0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,0xE7,0x60,0x28,0x27,0xF1,0xA9,
	0x62,0xC6,0xFC,0xB1,0x62,0xE7,0x60,0x29,0x27,0xF1,0xA9,0x62,0xC6,0xFC,
	0xB1,0x62,0xE7,0x60,0x2C,0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,0x62,0xE7,
	0x60,0x2D,0x7F,0x00,0x00,0xFE,0xD6,0x27,0xF1,0xA9,0x62,0xC6,0xFC,0xB1,
	0x62,0x27,0x60,0xE8,0x26,0x90,0x7A,0x01,0x00,0xCE,0xEE,0x1E };

unsigned get_entry(x) int x; { unsigned ent,z; z=(unsigned)x;
	ent=msb_data_get(entry_table)+msb_data_get(entry_table+1)*256;
	return(msb_data_get(ent+2*z)+msb_data_get(ent+2*z+1)*256); }
msb_data_put(add,data) unsigned add; int data; {
	set_command(0xe3); put_data(add/256); set_command(0xe5);
	put_data(add%256); set_command(0xeb); put_data(data); }
msb_data_get(add) unsigned add; {
	set_command(0xe3); put_data(add/256); set_command(0xe5);
	put_data(add%256); set_command(0xae); return(get_data()); }
msb_hot_reset(){ inp(DATAPT); inp(DATAPT); inp(DATAPT); set_command(0xff); }
put_command(x) int x; { while((inp(STATPT)&DRR)!=0){} outp(STATPT,x); }
put_data(x) int x; { while((inp(STATPT)&DRR)!=0){} outp(DATAPT,x); }
get_data(){ while((inp(STATPT)&DSR)!=0){} return(inp(DATAPT)); }
set_command(x) int x; { put_command(x); while(get_data()!=0xfe){} }
turbo_command_set(x,add,count) int x; unsigned add,count; {
	if(x==PUT) set_command(0xcd);
	else if(x==GET) set_command(0xcc);
	put_data(add/256); put_data(add%256);
	put_data(count/256); put_data(count%256); }

typedef struct _BP_NEURON {
	unsigned		count;
	double			out;
	struct _BP_NEURON	*next;
	struct _BP_NEURON	**link;
	double			*weight;
	double			*dw;
	double			delta;
} BP_NEURON;

BP_NEURON	input[INPUTUNIT];
BP_NEURON	hidden[HIDDENUNIT];
BP_NEURON	output[OUTPUTUNIT];
BP_NEURON	bias[BIASUNIT];

BpPropagate1(neuron) BP_NEURON *neuron;
{
	double net;
	net=Sigma(neuron); neuron->out=Sigmoid2(net);
}

BpPropagate(cluster) BP_NEURON *cluster;
{
	do{ BpPropagate1(cluster); }while(cluster=cluster->next);
}

BpInput(cluster,pattern) BP_NEURON *cluster; double *pattern;
{
	int i=0;
	do{ cluster->out=pattern[i++]; }while(cluster=cluster->next);
}

BpReset(cluster) BP_NEURON *cluster;
{
	BP_NEURON **n1; n1=&cluster;
	do{
		do{ cluster->delta=0.0; }while(cluster=cluster->next);
		n1=(BP_NEURON**)((int)n1+sizeof(BP_NEURON*));
		cluster=*n1;
	}while(cluster);
}

BpOutputLearning(n1,teacher) BP_NEURON *n1; double *teacher;
{
	int i=0; double tmp; BP_NEURON *n2; n2=n1;
	do{
		tmp=n2->out;
		n2->delta=(teacher[i++]-tmp)*tmp*(1-tmp);
	}while(n2=n2->next);
	do{
		for(i=0;icount;i++){
		    n1->link[i]->delta+=n1->delta*n1->weight[i];
		    n1->dw[i]=Eta*n1->delta*n1->link[i]->out+Alpha*n1->dw[i];
		    n1->weight[i]+=n1->dw[i];
		}
	}while(n1=n1->next);
}

BpHiddenLearning(n1) BP_NEURON *n1;
{
	int i; BP_NEURON *n2; n2=n1;
	do{ n2->delta*=n2->out*(1-n2->out); }while(n2=n2->next);
	do{
		for(i=0;icount;i++){
		    n1->link[i]->delta+=n1->delta*n1->weight[i];
		    n1->dw[i]=Eta*n1->delta*n1->link[i]->out+Alpha*n1->dw[i];
		    n1->weight[i]+=n1->dw[i];
		}
	}while(n1=n1->next);
}

nn_setting(){
	srand(3);
	Cluster1D(input,INPUTUNIT);
	Cluster1D(hidden,HIDDENUNIT);
	Cluster1D(output,OUTPUTUNIT);
	InitWeight(UNIFORM,0.5,-0.5);
	Connect(input,hidden);
	Connect(hidden,output);
	Connect(bias,hidden);
	Connect(bias,output);
	InitLinkAlloc(CONST,0.0,0.0);
	LinkAlloc1D(hidden,dw);
	LinkAlloc1D(output,dw);
	bias->out=1.0;
	strcpy(name,"hidden_"); strcat(name,number); strcat(name,".dat");
  	if((fds=fopen(name,"r"))==NULL){
		printf("\n\tHidden Weight File is not found.\n");
	}
	else{ WeightLoad( name, 0, hidden ); fclose(fds); }
	strcpy(name,"output_"); strcat(name,number); strcat(name,".dat");
  	if((fds=fopen(name,"r"))==NULL){
		printf("\n\tOutput Weight File is not found.\n");
	}
	else{ WeightLoad( name, 0, output ); fclose(fds); }
}

neuro_file_write(){
	strcpy(name,"output_"); strcat(name,number); strcat(name,".dat");
	WeightSave( name, "w", output );
	strcpy(name,"hidden_"); strcat(name,number); strcat(name,".dat");
	WeightSave( name, "w", hidden );
}

hex_data(){
	int i,j,k,ww;
	char name[32],nm[5];
	nm[1]=0;
	printf("\n\n\n\t! ! !  C A U T I O N  ! ! !");
	printf("\n\n\n\tWriting ROM Data file takes very long time!");
	printf("\n\n\n\t\t\t\tAre you OK ?   (y/n) --- ");
	while(1){ if(kbhit()){ if(getch()=='y') break; else return(0); } }
	for(i=0;i<8;i++){
		nm[0]=i+0x30;
		strcpy(name,"d-"); strcat(name,nm); strcat(name,".bin");
		fds=fopen(name,"wb"); printf("\n\nROM Bank = [%d]",i);
		for(j=0;j<4;j++){
			printf("\n\tWave = [%d]  ",j);
			for(ww=0;ww<32;ww++){
				putchar('.'); wave_calc(ww,4*i+j);
				for(k=0;k<512;k++) fputc(wb[k],fds);
			}
		}
		fclose(fds);
	}
}

intro_grain(){
	int i,j,d,xx,yy,yyy,x_max,dt,pt;
	_clearscreen(_GCLEARSCREEN); _setlinestyle(0x5555); _setcolor(7);
	_rectangle(_GBORDER,0,0,639,399);
	_settextposition(3,20); printf("Sample of Normal Sound Waveform");
	wave_calc(16,16,0); xx=30; yy=500; x_max=2300; _setlinestyle(0xffff);
	for(i=0;i512)&&(i<1024)) _lineto(xx+i/4,yyy);
		else _lineto(xx+i/4,pt);
	}
	mouse(1);
	while(1){ if(kbhit()){ d=getch(); if(d==0x1b) return; else break; } }
	mouse(0);
	_clearscreen(_GCLEARSCREEN); _setlinestyle(0x5555); _setcolor(7);
	_rectangle(_GBORDER,0,0,639,399);
	_settextposition(3,20); printf("Normal [ Smooth ] Grain");
	wave_calc(16,16,0); xx=50; yy=500; _setlinestyle(0xffff);
	_moveto(25,yy-7*128/5); _lineto(50,yy-7*128/5);
	for(i=0;i<512;i++){
		if(wb[i]<128) dt=wb[i]+128;
		else dt=128-(wb[i]&0x7f);
		yyy=yy-7*dt/5; _lineto(xx+i,yyy);
	}
	_lineto(615,yy-7*128/5);
	mouse(1);
	while(1){ if(kbhit()){ d=getch(); if(d==0x1b) return; else break; } }
	mouse(0);
	for(j=30;j>4;j=j-2){
		_clearscreen(_GCLEARSCREEN); _setlinestyle(0x5555);
		_rectangle(_GBORDER,0,0,639,399);
		_settextposition(3,15);
		printf("Waveshape Conversion of Grain : Parameter = %d",j);
		wave_calc(16,j,0); xx=50; yy=480; _setlinestyle(0xffff);
		_moveto(25,yy-7*128/5); _lineto(50,yy-7*128/5);
		for(i=0;i<512;i++){
			if(wb[i]<128) dt=wb[i]+128;
			else dt=128-(wb[i]&0x7f);
			yyy=yy-7*dt/5; _lineto(xx+i,yyy);
		}
		_lineto(615,yy-7*128/5);
		mouse(1);
		while(1){if(kbhit()){d=getch();if(d==0x1b)return;else break;}}
		mouse(0);
	}
	for(j=31;j>5;j=j-2){
		_clearscreen(_GCLEARSCREEN); _setlinestyle(0x5555);
		_rectangle(_GBORDER,0,0,639,399);
		_settextposition(3,15);
		printf("Waveshape Conversion of Grain : Parameter = %d",j);
		wave_calc(16,j,1); xx=50; yy=480; _setlinestyle(0xffff);
		_moveto(25,yy-7*128/5); _lineto(50,yy-7*128/5);
		for(i=0;i<512;i++){
			if(wb[i]<128) dt=wb[i]+128;
			else dt=128-(wb[i]&0x7f);
			yyy=yy-7*dt/5; _lineto(xx+i,yyy);
		}
		_lineto(615,yy-7*128/5);
		mouse(1);
		while(1){if(kbhit()){d=getch();if(d==0x1b)return;else break;}}
		mouse(0);
	}
	for(j=16;j>-1;j--){
		_clearscreen(_GCLEARSCREEN); _setlinestyle(0x5555);
		_rectangle(_GBORDER,0,0,639,399);
		_settextposition(3,15);
		printf("Waveshape Conversion of Grain : Parameter = %d",j);
		wave_calc(j,9,2); xx=50; yy=480; _setlinestyle(0xffff);
		_moveto(25,yy-7*128/5); _lineto(50,yy-7*128/5);
		for(i=0;i<512;i++){
			if(wb[i]<128) dt=wb[i]+128;
			else dt=128-(wb[i]&0x7f);
			yyy=yy-7*dt/5; _lineto(xx+i,yyy);
		}
		_lineto(615,yy-7*128/5);
		mouse(1);
		while(1){if(kbhit()){d=getch();if(d==0x1b)return;else break;}}
		mouse(0);
	}
	for(j=1;j<32;j++){
		_clearscreen(_GCLEARSCREEN); _setlinestyle(0x5555);
		_rectangle(_GBORDER,0,0,639,399);
		_settextposition(3,15);
		printf("Waveshape Conversion of Grain : Parameter = %d",j);
		wave_calc(j,9,2); xx=50; yy=480; _setlinestyle(0xffff);
		_moveto(25,yy-7*128/5); _lineto(50,yy-7*128/5);
		for(i=0;i<512;i++){
			if(wb[i]<128) dt=wb[i]+128;
			else dt=128-(wb[i]&0x7f);
			yyy=yy-7*dt/5; _lineto(xx+i,yyy);
		}
		_lineto(615,yy-7*128/5);
		mouse(1);
		while(1){if(kbhit()){d=getch();if(d==0x1b)return;else break;}}
		mouse(0);
	}
	for(j=30;j>15;j--){
		_clearscreen(_GCLEARSCREEN); _setlinestyle(0x5555);
		_rectangle(_GBORDER,0,0,639,399);
		_settextposition(3,15);
		printf("Waveshape Conversion of Grain : Parameter = %d",j);
		wave_calc(j,9,2); xx=50; yy=480; _setlinestyle(0xffff);
		_moveto(25,yy-7*128/5); _lineto(50,yy-7*128/5);
		for(i=0;i<512;i++){
			if(wb[i]<128) dt=wb[i]+128;
			else dt=128-(wb[i]&0x7f);
			yyy=yy-7*dt/5; _lineto(xx+i,yyy);
		}
		_lineto(615,yy-7*128/5);
		mouse(1);
		while(1){if(kbhit()){d=getch();if(d==0x1b)return;else break;}}
		mouse(0);
	}
}

wave_draw(z,mode)
	int z,mode;
{
	unsigned add;
	int i,j,xx,yy,xxx,yyy,tx,ty,dt,yyyy;
	xx=24+336*(z/8); xxx=xx+285; yy=9+48*(z%8); yyy=yy+43;
	_setcolor(0); _rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy); _setcolor(7);
	if(mode==0) _setlinestyle(0x9999);
	else if(mode==1) _setlinestyle(0xffff);
	xx=25+336*(z/8); xxx=xx+128; yy=10+48*(z%8); yyy=yy+42;
	_rectangle(_GBORDER,xx,yy,xxx,yyy);
	xx=157+336*(z/8); xxx=xx+145; yy=34+48*(z%8); yyy=yy+18;
	_rectangle(_GBORDER,xx,yy,xxx,yyy);
	ty=2+3*(z%8); tx=42*(z/8); _settextposition(ty,tx); printf("[%X]",z);
	_settextposition(ty-1,tx+20); printf(" KN=%02x  LE=%02x",kn[z],le[z]);
	xx=26+336*(z/8); xxx=xx+kn[z]; yy=13+48*(z%8); yyy=yy+4;
	_setlinestyle(0x5555); _rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy);
	xx=158+336*(z/8); xxx=xx+143*le[z]/127; yy=35+48*(z%8); yyy=yy+16;
	_setlinestyle(0x5555); _rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy);
	xx=25+336*(z/8); xxx=xx+128; yy=10+48*(z%8); yyy=yy+42;
	_setlinestyle(0xffff);
	for(i=0;i<512;i=i+4){
		if(i==0) _moveto(xx,yyy-14);
		else{
			if(wb[i]<128) dt=wb[i]+128;
			else dt=128-(wb[i]&0x7f);
			yyyy=yyy-56*dt/256+14;
			if(yyyy>yyy) yyyy=yyy;
			_lineto(xx+i/4,yyyy);
		}
		if (i==508) _lineto(xx+127,yyy-14);
	}
	if(mode==1) wave_sound(z);
}

wave_calc(ww,wk)
	int ww,wk;
{
	int i,wl,a,b;
	double p=3.1415926535,q,r,u,v=0;
	if(ww<16) wl=512-24*(16-ww);
	else wl=512-24*(ww-16);
	for(i=0;i<512;i++){
		if(i=0) wd[i]=(char)u;
		else wd[i]=(char)(128-u);
	}
	if(ww<16){
		a=(512-wl)/2; b=a+wl; for(i=0;i16){
		a=wl/2; b=a+(512-wl); for(i=0;i127-16) d=127-16;
	set_command(0xa9); put_data(d); put_data(kn[z]);
}

parameter_file_write(){
	int i;
	strcpy(name,"grain_"); strcat(name,number); strcat(name,".dat");
	fds=fopen(name,"wb");
	for(i=0;i<16;i++){ fputc(kn[i],fds); fputc(le[i],fds); }
	for(i=0;i<16;i++){ fputc(xp[i],fds); fputc(yp[i],fds); }
	fputc(ww,fds); fputc(wk,fds); fputc(wt,fds);
	fclose(fds);
}

parameter_file_read(){
	int i;
	strcpy(name,"grain_"); strcat(name,number); strcat(name,".dat");
	if((fds=fopen(name,"rb"))==NULL){
		for(i=0;i<16;i++){ kn[15-i]=32+i*4; le[15-i]=4+i*8; }
		for(i=0;i<16;i++){ xp[i]=8; yp[i]=8; }
		ww=16; wk=16; wt=0;
	}
	else{
		for(i=0;i<16;i++){ kn[i]=fgetc(fds); le[i]=fgetc(fds); }
		for(i=0;i<16;i++){ xp[i]=fgetc(fds); yp[i]=fgetc(fds); }
		ww=fgetc(fds); wk=fgetc(fds); wt=fgetc(fds);
		fclose(fds);
	}
}

mouse(flag) int flag; {
	if(flag==0){inregs.x.ax=2;} else if(flag==1){inregs.x.ax=1;}
	int86(51,&inregs,&outregs); }
getmouse(right,left,x,y) int *right,*left,*x,*y; {
	inregs.x.ax=3; int86(51,&inregs,&outregs); *right=outregs.x.bx;
	*left=outregs.x.ax; *x=outregs.x.cx; *y=outregs.x.dx; }
setmouse(x,y) int x,y; { inregs.x.ax=4; inregs.x.cx=x; inregs.x.dx=y;
	int86(51,&inregs,&outregs); }
m_style(x,y,buf) int x,y; char *buf; { inregs.x.ax=9; inregs.x.bx=x;
	inregs.x.cx=y; segread(&segregs); segregs.es=segregs.ds;
	inregs.x.dx=(int)buf; int86x(51,&inregs,&outregs,&segregs); }
m_view(xmin,ymin,xmax,ymax) int xmin,ymin,xmax,ymax; {
	inregs.x.ax=16; inregs.x.cx=xmin; inregs.x.dx=xmax;
	int86(51,&inregs,&outregs); inregs.x.ax=17; inregs.x.cx=ymin;
	inregs.x.dx=ymax; int86(51,&inregs,&outregs); }
m_color(color) int color; {
	inregs.x.ax=18; inregs.x.bx=color; int86(51,&inregs,&outregs); }
symbol(x,y,color,str,skip) int x,y,color,skip; unsigned char str[80]; {
	int i=0; _setcolor(color);
	while(str[i]!=0){ _gputchar(x,y,str[i++],_GXOR); x += skip; } }
static int style[]={
	0,0,0,0,0,0,0,0,0x0070,0x0e88,0x1184,0x2142,0x2221,0x9210,
	0xe107,0x1108,0x1108,0xe107,0x1108,0x1108,0xe107,0x4108,
	0x4208,0xfc07,0,0,0,0,0,0,0,0 };

main(argc,argv)
	int argc; char *argv[];
{
	int i,d;
	num=1; number[1]=0;
	number[0]='0'+num;
	inregs.x.ax=0; int86(51,&inregs,&outregs);
	if(outregs.x.ax==0){printf("\nPlease install mouse.sys !\n");exit(0);}
	m_style(0,8,style); m_color(1); m_view(0,0,639,399);
	msb_hot_reset(); wave_initialize_firmware_load();
	setmouse(320,200);
	parameter_file_read();
	_main_loop_top:
	mouse(0);
	printf("\x1b[2J\n\n\n\n\n");
	printf("      An Experiment of  ");
	printf("NEURO GRANULATION");
	printf("\n\n\n\n\t\t\tYOICHI NAGASHIMA");
	printf("\n\n\n\t\t\t\t\tSummer Symposium 1992 of JMACS\n\n");
	printf("\n\t\t(1) Introduction of Granular Synthesis");
	printf("\n\t\t(2) Select Sample Data File Group : [1]-[8]");
	printf("\n\t\t(3) Setting : Granulation Parameter --> Teacher");
	printf("\n\t\t(4) Mapping : Teacher Pattern --> Control Space");
	printf("\n\t\t(5) Learning : Neural Network Back Propagation");
	printf("\n\t\t(6) Running : Real-Time Neuro Granulation !");
	printf("\n\t\t(7) Writing Current Wave to HEX Data File");
	printf("\n\t\t\t\t\t\t\t\tselect : ");
	while(1){
		if(kbhit()){
			d=getch();
			if(d==0x1b){
				printf("\x1b[2J"); set_command(0xfe); exit(0);
			}
			else if((d>'0')&&(d<'8')){
				d=d-'0'; printf("\x1b[2J"); break;
			}
		}
	}
	switch(d){
		case 1: _setvideomode(_98RESS16COLOR);
			_displaycursor(_GCURSOROFF);
			intro_grain();
			_clearscreen(_GCLEARSCREEN);
			_displaycursor(_GCURSORON); break;
		case 2: file_group_change(); break;
		case 3: granulation_para(); break;
		case 4: teacher_mapping(); break;
		case 5: neural_net_learning(); break;
		case 6: sample_run(); break;
		case 7: hex_data(); break;
	}
	goto _main_loop_top;
}

file_group_change(){
	int d;
	printf("\x1b[2J\n\n\n\t\tSelect Sample Data File Group : [1]-[8]");
	printf("\n\n\n\t\t\tCurrent = %d",num);
	printf("\n\n\n\t\t\t\t\tselect [1-8] ----- ");
	while(1){
		if(kbhit()){
			d=getch();
			if(d==0x1b) return(0);
			else if((d>'0')&&(d<'9')){
				number[0]=d; num=d-'0';
				parameter_file_read(); break;
			}
		}
	}
}

neural_net_learning()
{
	int i,j,cnt=0,flg=0,err_count=30000;
	double dif,err,err_limit=0.005;
	long time1,time2;
	for(i=0;i<16;i++){
		pattern[i][0]=(double)xp[i]/17L;
		pattern[i][1]=(double)yp[i]/17L;
		teacher[i][0]=(double)kn[i]/127L;
		teacher[i][1]=(double)le[i]/127L;
	}
	printf("\n\nNeural Network Computation Start ...\n\n");
	nn_setting(); time(<ime); time1=ltime;
	do{
		for(err=0.0,i=0;i<16;i++){
			BpInput(input,pattern[i]);
			BpPropagate(hidden);
			BpPropagate(output);
			for(j=0;jerr_limit)&&(cnt++'){ /* Wave Width +1 */
			if(ww!=31){ flg=1; ww++; break; }
		}
		else if(c=='<'){ /* Wave Width -1 */
			if(ww!=0){ flg=1; ww--; break; }
		}
		else if(c=='+'){ /* Wave Type +1 */
			if(wk!=31){ flg=1; wk++; break; }
		}
		else if(c=='-'){ /* Wave Type -1 */
			if(wk!=0){ flg=1; wk--; break; }
		}
		else if((c=='d')||(c=='D')){ /* Wave : 2*sin */
			wt=0; flg=1; break;
		}
		else if((c=='t')||(c=='T')){ /* Wave : 3*sin */
			wt=1; flg=1; break;
		}
		else if((c=='q')||(c=='Q')){ /* Wave : 4*sin */
			wt=2; flg=1; break;
		}
	}
	if(flg!=0){ flg=0; goto _loop_loop; }
	parameter_file_write();
	mouse(0);
	_clearscreen(_GCLEARSCREEN); _displaycursor(_GCURSORON);
	set_command(0xa8);
}

mark_draw(p1,p2,pp,pt)
	int p1,p2,pp,pt;
{
	int i,j,xx,xxx,yy,yyy,fg=0;
	if(pp==99){
		xx=475; xxx=583; yy=79+16*p2; yyy=yy+18; _setlinestyle(0x5555);
		_setcolor(7); _rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy);
		xx=478; xxx=580; yy=79+16*p1; yyy=yy+18; _setlinestyle(0xffff);
		_setcolor(0); _rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy);
		return(0);
	}
	if(pp==1){
		xx=20+23*p1; xxx=xx+20; yy=5+23*(16-p2); yyy=yy+20;
		_setlinestyle(0xffff); _setcolor(7);
		_rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy); return(0);
	}
	xx=20+23*p1; xxx=xx+20; yy=5+23*(16-p2); yyy=yy+20; _setcolor(0);
	_setlinestyle(0xffff); _rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy);
	for(i=0;i<16;i++){ if((xp[i]==p1)&&(yp[i]==p2)) fg++; }
	_setcolor(7);
	if(pp==2){
		_setlinestyle(0x5555);
		if(fg==0) _rectangle(_GBORDER,xx,yy,xxx,yyy);
		else _rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy);
	}
	else if(pp==3){
		if((fg!=0)&&(p1==xp[pt])&&(p2==yp[pt])){
			_setlinestyle(0xffff);
			_rectangle(_GBORDER,xx,yy,xxx,yyy);
			_setlinestyle(0x6969);
			_rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy);
		}
		else if(fg!=0){
			_setlinestyle(0x5555);
			_rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy);
		}
		else{
			_setlinestyle(0x5555);
			_rectangle(_GBORDER,xx,yy,xxx,yyy);
		}
	}
}

teacher_mapping(){
	int i,j,c,d,xx,xxx,yy,yyy,rsb;
	int rsc=0,rsd=0,px=64,py=64,ox=64,oy=64,pt=15,ot=15;
	_setvideomode(_98RESS16COLOR); _setcolor(7);
	_displaycursor(_GCURSOROFF); _clearscreen(_GCLEARSCREEN);
	wave_calc(ww,wk); wave_set();
	for(i=0;i<17;i++){ for(j=0;j<17;j++) mark_draw(i,j,2,0); }
	_settextposition(2,57); printf("[[ WW=%d WK=%d ]]  ",ww,wk);
	_settextposition(23,59); printf("[[ Group = %d ]]  ",num);
	_setlinestyle(0x5555); _setcolor(7);
	xx=475; xxx=583; yy=69; yyy=91+16*16;
	_rectangle(_GFILLINTERIOR,xx,yy,xxx,yyy);
	for(i=0;i<16;i++){_settextposition(i+5,61); printf("Pattern <%X>",i);}
	mark_draw(pt,ot,99,0); wave_sound(pt);
	mark_draw(xp[pt],yp[pt],3,pt); mark_draw(8,8,1,0);
	while(1){
		while(!kbhit()){
			set_command(0xaa); d=get_data();
			if(d==0xcf){
				mouse(0);
				rsc=d; rsb=d; px=get_data();
				i=ox*100/753; j=oy*100/753;
				mark_draw(i,j,3,pt);
				i=px*100/753; j=py*100/753;
				mark_draw(i,j,1,0); ox=px;
				mouse(1);
			}
			else if(d==0xdf){
				mouse(0);
				rsd=d; rsb=d; py=get_data();
				i=ox*100/753; j=oy*100/753;
				mark_draw(i,j,3,pt);
				i=px*100/753; j=py*100/753;
				mark_draw(i,j,1,0); oy=py;
				mouse(1);
			}
		}
		c=getch();
		if(c==0x1b){ /* Escape */
			break;
		}
		else if(c==0x0b){ /* Up */
			mouse(0);
			pt=(pt+15)&0x0f; mark_draw(pt,ot,99,0);
			mark_draw(xp[ot],yp[ot],2,0);
			mark_draw(xp[pt],yp[pt],3,pt);
			wave_sound(pt); ot=pt;
			mouse(1);
		}
		else if(c==0x0a){ /* Down */
			mouse(0);
			pt=(pt+1)&0x0f; mark_draw(pt,ot,99);
			mark_draw(xp[ot],yp[ot],2,0);
			mark_draw(xp[pt],yp[pt],3,pt);
			wave_sound(pt); ot=pt;
			mouse(1);
		}
		else if(c==0x0d){ /* RETURN */
			mouse(0);
			i=xp[pt]; j=yp[pt];
			xp[pt]=px*100/753; yp[pt]=py*100/753;
			mark_draw(i,j,3,0);
			i=px*100/753; j=py*100/753;
			mark_draw(i,j,1,0);
			mouse(1);
		}
	}
	parameter_file_write();
	mouse(0);
	_clearscreen(_GCLEARSCREEN); _displaycursor(_GCURSORON);
	set_command(0xa8);
}

sample_run()
{
	int i,j,c,d,xx,xxx,yy,yyy;
	int px=64,py=64,opx=8,opy=8,rsb=0xcf;
	_setvideomode(_98RESS16COLOR); _setcolor(7);
	_displaycursor(_GCURSOROFF); _clearscreen(_GCLEARSCREEN);
	wave_calc(ww,wk); wave_set();
	for(i=0;i<17;i++){ for(j=0;j<17;j++) mark_draw(i,j,2,0); }
	_settextposition(3,54); printf("*** Real-Time Control ***");
	_settextposition(6,60); printf("WW=%d WK=%d ",ww,wk);
	_settextposition(16,59); printf("[[ Group = %d ]]  ",num);
	nn_setting(); mark_draw(8,8,1,0); nn_out(px,py,rsb);
	while(1){
		while(!kbhit()){
			set_command(0xaa); d=get_data();
			if(d==0xcf){
				rsb=d; px=get_data();
				i=px*100/753; j=py*100/753;
				if((opx!=i)||(opy!=j)){
					mark_draw(opx,opy,2,0);
					mark_draw(i,j,1,0);
					nn_out(px,py,rsb); opx=i; opy=j;
				}
			}
			else if(d==0xdf){
				rsb=d; py=get_data();
				i=px*100/753; j=py*100/753;
				if((opx!=i)||(opy!=j)){
					mark_draw(opx,opy,2,0);
					mark_draw(i,j,1,0);
					nn_out(px,py,rsb); opx=i; opy=j;
				}
			}
		}
		if(getch()==0x1b) break;
	}
	_clearscreen(_GCLEARSCREEN); _displaycursor(_GCURSORON);
	set_command(0xa8);
}

nn_out(px,py,rsb)
	int px,py,rsb;
{
	int i,j,k,l,d;
	double test[INPUTUNIT];
	test[0]=(double)px/127L; test[1]=(double)py/127L;
	BpInput(input,test); BpPropagate(hidden); BpPropagate(output);
	k=(int)(127.9L*output[0].out);
	_settextposition(10,60); printf("Key No. = %02X ",k);
	l=(int)(127.9L*output[1].out);
	_settextposition(12,60); printf("Loop End = %02X ",l);
	if(l>127-16) l=127-16;
	set_command(0xa9); put_data(l); put_data(k);
}

ADSP2101 アセンブラ/C

.MODULE/RAM/BOOT=0/ABS=0 test;

{ SYSTEM I/O ADDRESS DEFINES }
.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

.CONST comcrdy	= 0x0803;
.CONST comdrdy	= 0x0804;
.CONST cpudt 	= 0x0810;
.CONST dspdt	= 0x0811;

{ BOAD REGISTERS }
.CONST hs_readonly	= 0x0600;
.CONST handshake	= 0x0700;
.CONST dipswitch	= 0x0400;
.CONST codec_ctrl	= 0x0400;

{ CONTROLE BITS }
.CONST d2c	= 0x2000;
.CONST c2d	= 0x1000;
.CONST s8	= 0x8000;
.CONST s4	= 0x4000;
.CONST s2	= 0x2000;
.CONST s1	= 0x1000;
.CONST dc	= 0x1000;
.CONST c2denb	= 0x2000;
.CONST extenb	= 0x4000;

{ MEMORY STRUCTURE }
.CONST	dm_top		= 0x0800;
.CONST	dm_bottom	= 0x3BFF;
.CONST	pm_top		= 0x0000;
.CONST	pm_bottom	= 0x3FFF;

.CONST	ext_dm_top	= 0x0800;
.CONST	ext_dm_bottom	= 0x37FF;
.CONST	int_dm_top	= 0x3800;
.CONST	int_dm_bottom	= 0x3BFF;
.CONST	ext_pm_top	= 0x0800;
.CONST	ext_pm_bottom	= 0x3FFF;
.CONST	int_pm_top	= 0x0000;
.CONST	int_pm_bottom	= 0x07FF;

{ TERMINATE PROGRAM }
.CONST	end_of_prog	= 0x001D;

/******************** Special Macro Defines ********************/
#define data_to_mem(data,memory)	AX0=data; DM(memory)=AX0;
#define data_to_reg(data,register)	AX0=data; DM(register)=AX0;
#define interrupt_disable		IFC=0x3f; IMASK=0x00;
#define interrupt_s0_rx_enable		IFC=0x3f; IMASK=0x08;
#define interrupt_s0_tx_enable		IFC=0x3f; IMASK=0x10;
#define waiting_for_interrupt		idle;
#define direct_tx_port_0(i_reg,m_reg)	AX0=DM(i_reg,m_reg); TX0=AX0;
#define serial_port_0_disable		data_to_reg(0x0408,Sys_Ctrl_Reg)
#define serial_port_0_enable		data_to_reg(0x1408,Sys_Ctrl_Reg)
#define autobuf_port_0_disable		data_to_reg(0x0000,Sport0_Autobuf_Ctrl)
#define autobuf_port_0_enable		data_to_reg(0x0017,Sport0_Autobuf_Ctrl)
.MACRO codec_to_AR_reg_mono;
	SR0=DM(I3,M3); AY0=DM(I3,M3);
	SR=lshift SR0 by 8 (lo); AR=SR0 or AY0;
.ENDMACRO;
.MACRO SR0_reg_to_codec_mono;
	AY0=0xff; AR=SR0 and AY0; SR=lshift SR0 by -8 (lo);
	DM(I2,M2)=SR0; DM(I2,M2)=AR;
.ENDMACRO;

/******************** Buffer Memory Defines ********************/
.VAR/DM/RAM/ABS=0x0900/CIRC	fifo_tx[8];
.VAR/DM/RAM/ABS=0x0910/CIRC	fifo_rx[8];

/******************** Reset Vectors Defines ********************/
jump start;	nop; nop; nop;	{Reset Vector}
	RTI;	nop; nop; nop;	{irq2}
	RTI;	nop; nop; nop;	{sport0 TX}
	RTI;	nop; nop; nop;	{sport0 RX}
	RTI;	nop; nop; nop;	{irq0}
	RTI;	nop; nop; nop;	{irq1}
	RTI;	nop; nop; nop;	{timer}

/******************** Common Subroutines ********************/
codec_initialize:
	call codec_system_init;
	call codec_init_command_mode;
	call codec_init_data_mode;
	rts;
codec_system_init:
	data_to_reg(0x01,Dm_Wait_Reg)		/* 1 wait */
	data_to_reg(0xff,Sport0_Rfsdiv)		/* Multi-ch, 256bit */
	data_to_reg(0x00,Sport0_Sclkdiv)	/* divide rate */
	data_to_reg(0xff,Sport0_Tx_Words0)	/* LSB 16bit Tx */
	data_to_reg(0xff,Sport0_Rx_Words0)	/*           Rx */
	data_to_reg(0x00,Sport0_Tx_Words1)	/* MSB 16bit Tx */
	data_to_reg(0x00,Sport0_Rx_Words1)	/*           Rx */
	rts;
codec_init_command_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x08,fifo_tx+0)	/* olb=1, clb=0(control) */
	data_to_mem(0x1c,fifo_tx+1)	/* 32KHz, Stereo, 16bit */
	data_to_mem(0x9a,fifo_tx+2)	/* 256bit, x1, gen.clk */
	data_to_mem(0x00,fifo_tx+3)	/* (reserved) */
	data_to_mem(0xc0,fifo_tx+4)	/* (reserved) */
	data_to_mem(0x00,fifo_tx+6)	/* (reserved) */
	I0=^fifo_tx; M0=1; L0=8; 
	I1=^fifo_rx; M1=1; L1=8; 
	data_to_reg(0xc707,Sport0_Ctrl_Reg)	/* CODEC --> Setting Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
loop_1:
	waiting_for_interrupt
	call check_1;
	if ne jump loop_1;
	interrupt_s0_tx_enable
	data_to_mem(0x000C,fifo_tx+0)		/* clb set */
	waiting_for_interrupt
	waiting_for_interrupt
	waiting_for_interrupt
	rts;
check_1: 
	AX0=DM(fifo_rx+0); AY0=0x04; AR=AX0 and AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+1); AY0=DM(fifo_rx+1); AR=AX0-AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+2); AY0=DM(fifo_rx+2); AF=AX0 xor AY0;
	AX0=0xFF7F; AR=AX0 and AF;
chk_exit:
	rts;
codec_init_data_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x00,fifo_tx+0)	/* (dummy) */
	data_to_mem(0x00,fifo_tx+1)	/*    |    */
	data_to_mem(0x00,fifo_tx+2)	/*    |    */
	data_to_mem(0x00,fifo_tx+3)	/* (dummy) */
	data_to_mem(0xc0,fifo_tx+4)	/* Output Enable, Lch : 0db ATT */
	data_to_mem(0x00,fifo_tx+5)	/*                Rch : 0db ATT */
	data_to_mem(0xc6,fifo_tx+6)	/* Input <- Line, Lch : gain 9dB */
	data_to_mem(0xf6,fifo_tx+7)	/*                Rch : gain 9dB */
	I0=^fifo_tx; M0=1; L0=8;
	I1=^fifo_rx; M1=1; L1=8;
	I2=^fifo_tx; M2=1; L2=2;		/* Data Buffer for Tx (L) */
	I3=^fifo_rx; M3=1; L3=2;		/*                 Rx (L) */
	data_to_reg(0x8607,Sport0_Ctrl_Reg)	/* CODEC --> Running Mode */
	data_to_reg(dc,codec_ctrl)		/* Change to Data Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
	rts;

.VAR/DM/RAM/ABS=0x0800		interface;
				/* Offset : +0 = original wave data */
				/*        : +1 = effected wave data */
				/*        : +2 = delay offset for 2nd */
				/*        : +3 = delay offset for 3rd */
				/*        : +4 = delay offset for 4th */
				/*        : +5 = multiply data for 1st */
				/*        : +6 = multiply data for 2nd */
				/*        : +7 = multiply data for 3rd */
				/*        : +8 = multiply data for 4th */
.VAR/DM/RAM/ABS=0x2000/CIRC	data_fifo[4096];

start:
	call codec_initialize;		/* DSP_HEAD.H */
	I4=^data_fifo; M4=1; L4=4096;
	I5=^data_fifo; M5=1; L5=0;
main:
	codec_to_AR_reg_mono;		/* Get A/D Data : DSP_HEAD.H */
	DM(I4,M4)=AR;
	DM(interface+0)=AR;
	AY0=I4;
	AY1=0x2fff;
	MY0=DM(interface+5);
	MR=AR*MY0 (ss);
	AX0=DM(interface+2);
	AR=AX0+AY0;
	AR=AR and AY1;
	I5=AR;
	AR=DM(I5,M5);
	MY0=DM(interface+6);
	MR=MR+AR*MY0 (ss);
	AX0=DM(interface+3);
	AR=AX0+AY0;
	AR=AR and AY1;
	I5=AR;
	AR=DM(I5,M5);
	MY0=DM(interface+7);
	MR=MR+AR*MY0 (ss);
	AX0=DM(interface+4);
	AR=AX0+AY0;
	AR=AR and AY1;
	I5=AR;
	AR=DM(I5,M5);
	MY0=DM(interface+8);
	MR=MR+AR*MY0 (ss);
	SR0=MR1;
	DM(interface+1)=SR0;
	SR0_reg_to_codec_mono;		/* Put to D/A : DSP_HEAD.H */
	waiting_for_interrupt
	jump main;

.ENDMOD;

=============================================================================


.MODULE/RAM/BOOT = 0/ABS = 0	transfer;

.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

{ SYSTEM I/O ADDRESS DEFINES }
.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

.CONST comcrdy	= 0x0803;
.CONST comdrdy	= 0x0804;
.CONST cpudt 	= 0x0810;
.CONST dspdt	= 0x0811;

{ BOAD REGISTERS }
.CONST hs_readonly	= 0x0600;
.CONST handshake	= 0x0700;
.CONST dipswitch	= 0x0400;
.CONST codec_ctrl	= 0x0400;

{ CONTROLE BITS }
.CONST d2c	= 0x2000;
.CONST c2d	= 0x1000;
.CONST s8	= 0x8000;
.CONST s4	= 0x4000;
.CONST s2	= 0x2000;
.CONST s1	= 0x1000;
.CONST dc	= 0x1000;
.CONST c2denb	= 0x2000;
.CONST extenb	= 0x4000;

{ MEMORY STRUCTURE }
.CONST	dm_top		= 0x0800;
.CONST	dm_bottom	= 0x3BFF;
.CONST	pm_top		= 0x0000;
.CONST	pm_bottom	= 0x3FFF;

.CONST	ext_dm_top	= 0x0800;
.CONST	ext_dm_bottom	= 0x37FF;
.CONST	int_dm_top	= 0x3800;
.CONST	int_dm_bottom	= 0x3BFF;
.CONST	ext_pm_top	= 0x0800;
.CONST	ext_pm_bottom	= 0x3FFF;
.CONST	int_pm_top	= 0x0000;
.CONST	int_pm_bottom	= 0x07FF;

{ TERMINATE PROGRAM }
.CONST	end_of_prog	= 0x001D;

/******************** Special Macro Defines ********************/
#define data_to_mem(data,memory)	AX0=data; DM(memory)=AX0;
#define data_to_reg(data,register)	AX0=data; DM(register)=AX0;
#define interrupt_disable		IFC=0x3f; IMASK=0x00;
#define interrupt_s0_rx_enable		IFC=0x3f; IMASK=0x08;
#define interrupt_s0_tx_enable		IFC=0x3f; IMASK=0x10;
#define waiting_for_interrupt		idle;
#define direct_tx_port_0(i_reg,m_reg)	AX0=DM(i_reg,m_reg); TX0=AX0;
#define serial_port_0_disable		data_to_reg(0x0408,Sys_Ctrl_Reg)
#define serial_port_0_enable		data_to_reg(0x1408,Sys_Ctrl_Reg)
#define autobuf_port_0_disable		data_to_reg(0x0000,Sport0_Autobuf_Ctrl)
#define autobuf_port_0_enable		data_to_reg(0x0017,Sport0_Autobuf_Ctrl)
.MACRO codec_to_AR_reg_mono;
	SR0=DM(I3,M3); AY0=DM(I3,M3);
	SR=lshift SR0 by 8 (lo); AR=SR0 or AY0;
.ENDMACRO;
.MACRO SR0_reg_to_codec_mono;
	AY0=0xff; AR=SR0 and AY0; SR=lshift SR0 by -8 (lo);
	DM(I2,M2)=SR0; DM(I2,M2)=AR;
.ENDMACRO;

/******************** Buffer Memory Defines ********************/
.VAR/DM/RAM/ABS=0x0900/CIRC	fifo_tx[8];
.VAR/DM/RAM/ABS=0x0910/CIRC	fifo_rx[8];

/******************** Reset Vectors Defines ********************/
jump start;	nop; nop; nop;	{Reset Vector}
	RTI;	nop; nop; nop;	{irq2}
	RTI;	nop; nop; nop;	{sport0 TX}
	RTI;	nop; nop; nop;	{sport0 RX}
	RTI;	nop; nop; nop;	{irq0}
	RTI;	nop; nop; nop;	{irq1}
	RTI;	nop; nop; nop;	{timer}

/******************** Common Subroutines ********************/
codec_initialize:
	call codec_system_init;
	call codec_init_command_mode;
	call codec_init_data_mode;
	rts;
codec_system_init:
	data_to_reg(0x01,Dm_Wait_Reg)		/* 1 wait */
	data_to_reg(0xff,Sport0_Rfsdiv)		/* Multi-ch, 256bit */
	data_to_reg(0x00,Sport0_Sclkdiv)	/* divide rate */
	data_to_reg(0xff,Sport0_Tx_Words0)	/* LSB 16bit Tx */
	data_to_reg(0xff,Sport0_Rx_Words0)	/*           Rx */
	data_to_reg(0x00,Sport0_Tx_Words1)	/* MSB 16bit Tx */
	data_to_reg(0x00,Sport0_Rx_Words1)	/*           Rx */
	rts;
codec_init_command_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x08,fifo_tx+0)	/* olb=1, clb=0(control) */
	data_to_mem(0x1c,fifo_tx+1)	/* 32KHz, Stereo, 16bit */
	data_to_mem(0x9a,fifo_tx+2)	/* 256bit, x1, gen.clk */
	data_to_mem(0x00,fifo_tx+3)	/* (reserved) */
	data_to_mem(0xc0,fifo_tx+4)	/* (reserved) */
	data_to_mem(0x00,fifo_tx+6)	/* (reserved) */
	I0=^fifo_tx; M0=1; L0=8; 
	I1=^fifo_rx; M1=1; L1=8; 
	data_to_reg(0xc707,Sport0_Ctrl_Reg)	/* CODEC --> Setting Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
loop_1:
	waiting_for_interrupt
	call check_1;
	if ne jump loop_1;
	interrupt_s0_tx_enable
	data_to_mem(0x000C,fifo_tx+0)		/* clb set */
	waiting_for_interrupt
	waiting_for_interrupt
	waiting_for_interrupt
	rts;
check_1: 
	AX0=DM(fifo_rx+0); AY0=0x04; AR=AX0 and AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+1); AY0=DM(fifo_rx+1); AR=AX0-AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+2); AY0=DM(fifo_rx+2); AF=AX0 xor AY0;
	AX0=0xFF7F; AR=AX0 and AF;
chk_exit:
	rts;
codec_init_data_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x00,fifo_tx+0)	/* (dummy) */
	data_to_mem(0x00,fifo_tx+1)	/*    |    */
	data_to_mem(0x00,fifo_tx+2)	/*    |    */
	data_to_mem(0x00,fifo_tx+3)	/* (dummy) */
	data_to_mem(0xc0,fifo_tx+4)	/* Output Enable, Lch : 0db ATT */
	data_to_mem(0x00,fifo_tx+5)	/*                Rch : 0db ATT */
	data_to_mem(0xc6,fifo_tx+6)	/* Input <- Line, Lch : gain 9dB */
	data_to_mem(0xf6,fifo_tx+7)	/*                Rch : gain 9dB */
	I0=^fifo_tx; M0=1; L0=8;
	I1=^fifo_rx; M1=1; L1=8;
	I2=^fifo_tx; M2=1; L2=2;		/* Data Buffer for Tx (L) */
	I3=^fifo_rx; M3=1; L3=2;		/*                 Rx (L) */
	data_to_reg(0x8607,Sport0_Ctrl_Reg)	/* CODEC --> Running Mode */
	data_to_reg(dc,codec_ctrl)		/* Change to Data Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
	rts;

{ interupt vector table }
        jump start; RTI; RTI; RTI;      {Reset Vector}    
	RTI; RTI; RTI; RTI;		{irq2}
        RTI; RTI; RTI; RTI;             {sport0 TX}
        RTI; RTI; RTI; RTI;             {sport0 RX}
        RTI; RTI; RTI; RTI;             {irq0}
        RTI; RTI; RTI; RTI;             {irq1}
        RTI; RTI; RTI; RTI;             {timer}

#include ;

start:
	AX0 = 0x0408; DM(Sys_Ctrl_Reg) = AX0;
	AX0 = 1; DM(Dm_Wait_Reg)  = AX0;
	
	AX0 = 0; DM(comdrdy) = AX0; DM(comcrdy) = AX0;
	AX0 = d2c; DM(handshake) = AX0;

testtop:
	CALL get_wd;

	AY0 = SI;
	AR = AY0 + 1;
	SI = AR;

	CALL put_wd;

	JUMP testtop;

.ENDMOD;

=============================================================================


.MODULE/RAM/BOOT = 0/ABS = 0	test;

.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

{ SYSTEM I/O ADDRESS DEFINES }
.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

.CONST comcrdy	= 0x0803;
.CONST comdrdy	= 0x0804;
.CONST cpudt 	= 0x0810;
.CONST dspdt	= 0x0811;

{ BOAD REGISTERS }
.CONST hs_readonly	= 0x0600;
.CONST handshake	= 0x0700;
.CONST dipswitch	= 0x0400;
.CONST codec_ctrl	= 0x0400;

{ CONTROLE BITS }
.CONST d2c	= 0x2000;
.CONST c2d	= 0x1000;
.CONST s8	= 0x8000;
.CONST s4	= 0x4000;
.CONST s2	= 0x2000;
.CONST s1	= 0x1000;
.CONST dc	= 0x1000;
.CONST c2denb	= 0x2000;
.CONST extenb	= 0x4000;

{ MEMORY STRUCTURE }
.CONST	dm_top		= 0x0800;
.CONST	dm_bottom	= 0x3BFF;
.CONST	pm_top		= 0x0000;
.CONST	pm_bottom	= 0x3FFF;

.CONST	ext_dm_top	= 0x0800;
.CONST	ext_dm_bottom	= 0x37FF;
.CONST	int_dm_top	= 0x3800;
.CONST	int_dm_bottom	= 0x3BFF;
.CONST	ext_pm_top	= 0x0800;
.CONST	ext_pm_bottom	= 0x3FFF;
.CONST	int_pm_top	= 0x0000;
.CONST	int_pm_bottom	= 0x07FF;

{ TERMINATE PROGRAM }
.CONST	end_of_prog	= 0x001D;

/******************** Special Macro Defines ********************/
#define data_to_mem(data,memory)	AX0=data; DM(memory)=AX0;
#define data_to_reg(data,register)	AX0=data; DM(register)=AX0;
#define interrupt_disable		IFC=0x3f; IMASK=0x00;
#define interrupt_s0_rx_enable		IFC=0x3f; IMASK=0x08;
#define interrupt_s0_tx_enable		IFC=0x3f; IMASK=0x10;
#define waiting_for_interrupt		idle;
#define direct_tx_port_0(i_reg,m_reg)	AX0=DM(i_reg,m_reg); TX0=AX0;
#define serial_port_0_disable		data_to_reg(0x0408,Sys_Ctrl_Reg)
#define serial_port_0_enable		data_to_reg(0x1408,Sys_Ctrl_Reg)
#define autobuf_port_0_disable		data_to_reg(0x0000,Sport0_Autobuf_Ctrl)
#define autobuf_port_0_enable		data_to_reg(0x0017,Sport0_Autobuf_Ctrl)
.MACRO codec_to_AR_reg_mono;
	SR0=DM(I3,M3); AY0=DM(I3,M3);
	SR=lshift SR0 by 8 (lo); AR=SR0 or AY0;
.ENDMACRO;
.MACRO SR0_reg_to_codec_mono;
	AY0=0xff; AR=SR0 and AY0; SR=lshift SR0 by -8 (lo);
	DM(I2,M2)=SR0; DM(I2,M2)=AR;
.ENDMACRO;

/******************** Buffer Memory Defines ********************/
.VAR/DM/RAM/ABS=0x0900/CIRC	fifo_tx[8];
.VAR/DM/RAM/ABS=0x0910/CIRC	fifo_rx[8];

/******************** Reset Vectors Defines ********************/
jump start;	nop; nop; nop;	{Reset Vector}
	RTI;	nop; nop; nop;	{irq2}
	RTI;	nop; nop; nop;	{sport0 TX}
	RTI;	nop; nop; nop;	{sport0 RX}
	RTI;	nop; nop; nop;	{irq0}
	RTI;	nop; nop; nop;	{irq1}
	RTI;	nop; nop; nop;	{timer}

/******************** Common Subroutines ********************/
codec_initialize:
	call codec_system_init;
	call codec_init_command_mode;
	call codec_init_data_mode;
	rts;
codec_system_init:
	data_to_reg(0x01,Dm_Wait_Reg)		/* 1 wait */
	data_to_reg(0xff,Sport0_Rfsdiv)		/* Multi-ch, 256bit */
	data_to_reg(0x00,Sport0_Sclkdiv)	/* divide rate */
	data_to_reg(0xff,Sport0_Tx_Words0)	/* LSB 16bit Tx */
	data_to_reg(0xff,Sport0_Rx_Words0)	/*           Rx */
	data_to_reg(0x00,Sport0_Tx_Words1)	/* MSB 16bit Tx */
	data_to_reg(0x00,Sport0_Rx_Words1)	/*           Rx */
	rts;
codec_init_command_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x08,fifo_tx+0)	/* olb=1, clb=0(control) */
	data_to_mem(0x1c,fifo_tx+1)	/* 32KHz, Stereo, 16bit */
	data_to_mem(0x9a,fifo_tx+2)	/* 256bit, x1, gen.clk */
	data_to_mem(0x00,fifo_tx+3)	/* (reserved) */
	data_to_mem(0xc0,fifo_tx+4)	/* (reserved) */
	data_to_mem(0x00,fifo_tx+6)	/* (reserved) */
	I0=^fifo_tx; M0=1; L0=8; 
	I1=^fifo_rx; M1=1; L1=8; 
	data_to_reg(0xc707,Sport0_Ctrl_Reg)	/* CODEC --> Setting Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
loop_1:
	waiting_for_interrupt
	call check_1;
	if ne jump loop_1;
	interrupt_s0_tx_enable
	data_to_mem(0x000C,fifo_tx+0)		/* clb set */
	waiting_for_interrupt
	waiting_for_interrupt
	waiting_for_interrupt
	rts;
check_1: 
	AX0=DM(fifo_rx+0); AY0=0x04; AR=AX0 and AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+1); AY0=DM(fifo_rx+1); AR=AX0-AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+2); AY0=DM(fifo_rx+2); AF=AX0 xor AY0;
	AX0=0xFF7F; AR=AX0 and AF;
chk_exit:
	rts;
codec_init_data_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x00,fifo_tx+0)	/* (dummy) */
	data_to_mem(0x00,fifo_tx+1)	/*    |    */
	data_to_mem(0x00,fifo_tx+2)	/*    |    */
	data_to_mem(0x00,fifo_tx+3)	/* (dummy) */
	data_to_mem(0xc0,fifo_tx+4)	/* Output Enable, Lch : 0db ATT */
	data_to_mem(0x00,fifo_tx+5)	/*                Rch : 0db ATT */
	data_to_mem(0xc6,fifo_tx+6)	/* Input <- Line, Lch : gain 9dB */
	data_to_mem(0xf6,fifo_tx+7)	/*                Rch : gain 9dB */
	I0=^fifo_tx; M0=1; L0=8;
	I1=^fifo_rx; M1=1; L1=8;
	I2=^fifo_tx; M2=1; L2=2;		/* Data Buffer for Tx (L) */
	I3=^fifo_rx; M3=1; L3=2;		/*                 Rx (L) */
	data_to_reg(0x8607,Sport0_Ctrl_Reg)	/* CODEC --> Running Mode */
	data_to_reg(dc,codec_ctrl)		/* Change to Data Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
	rts;

.VAR/DM/RAM/ABS=0x2010/CIRC	s0_ctl_send[8]; /* 2010-2017 */
.VAR/DM/RAM/ABS=0x2020/CIRC	s0_ctl_recv[8]; /* 2020-2027 */
.VAR/DM/RAM/ABS=0x2030/CIRC	s0_dat_send[8]; /* 2030-2037 */
.VAR/DM/RAM/ABS=0x2040/CIRC	s0_dat_recv[8]; /* 2040-2047 */

#define	ts1	0
#define	ts2	1
#define	ts3	2
#define	ts4	3
#define	ts5	4
#define	ts6	5
#define	ts7	6
#define	ts8	7

{ interupt vector table }
        jump start; RTI; RTI; RTI;	{Reset Vector}    
	RTI; RTI; RTI; RTI;		{irq2}
        jump s0send; RTI; RTI; RTI;	{sport0 TX}
        jump s0recv; RTI; RTI; RTI;	{sport0 RX}
        RTI; RTI; RTI; RTI;		{irq0}
        RTI; RTI; RTI; RTI;		{irq1}
        RTI; RTI; RTI; RTI;		{timer}

#include ;

start:
	ifc=0x3f; IMASK=0x00;			/* int disable */
	AX0 = 0x0408; DM(Sys_Ctrl_Reg) = AX0;	/* sport0 disable */
	AX0 = 1; DM(Dm_Wait_Reg)  = AX0;
	
initcodec:
	AX0=0x00ff; DM(Sport0_Tx_Words0)=AX0;	/* 64bit */ 
	AX0=0x00ff; DM(Sport0_Rx_Words0)=AX0;	/* 64bit */
	AX0=0x0000; DM(Sport0_Tx_Words1)=AX0;
	AX0=0x0000; DM(Sport0_Rx_Words1)=AX0;
	AX0=0xc707; DM(Sport0_Ctrl_Reg)=AX0;	/* rfs send mode */
	AX0=0x0000; DM(Sport0_Sclkdiv)=AX0;
	AX0=255;   DM(Sport0_Rfsdiv)=AX0;	/* multichannel,256bit */
{ control mode timeslot }
	AX0=0x8;	DM(s0_ctl_send+ts1)=AX0; /* olb=1,clb=0 */
/*	AX0=0x0;	DM(s0_ctl_send+ts1)=AX0;    olb=0,clb=0 */
	AX0=0x34;	DM(s0_ctl_send+ts2)=AX0; /* 48KHz,st,16bit */
	AX0=0x9a;	DM(s0_ctl_send+ts3)=AX0; /* 256b,x1,gen.clk */
	AX0=0;		DM(s0_ctl_send+ts4)=AX0;
	AX0=0xc0;	DM(s0_ctl_send+ts5)=AX0;
	AX0=0;		DM(s0_ctl_send+ts6)=AX0;
	AX0=0;		DM(s0_ctl_send+ts7)=AX0;
	AX0=0;		DM(s0_ctl_send+ts8)=AX0;
	AX0=0xffff;	DM(s0_ctl_recv+ts1)=AX0;
	AX0=0xffff;	DM(s0_ctl_recv+ts2)=AX0;
	AX0=0xffff;	DM(s0_ctl_recv+ts3)=AX0;
{ data time slot }	
	AX0=0x0;	DM(s0_dat_send+ts1)=AX0;
	AX0=0x0;	DM(s0_dat_send+ts2)=AX0;
	AX0=0x0;	DM(s0_dat_send+ts3)=AX0;
	AX0=0x0;	DM(s0_dat_send+ts4)=AX0;
	AX0=0xc2;	DM(s0_dat_send+ts5)=AX0; /* out enb,Rch:3dbATT */
	AX0=0x02;	DM(s0_dat_send+ts6)=AX0; /* Rch:3dbATT */
	AX0=0xdc;	DM(s0_dat_send+ts7)=AX0; /* AUX-I,Lch:gain24dB */
	AX0=0xfc;	DM(s0_dat_send+ts8)=AX0; /* AUX-I,Rch:gain24dB */
	AX0=0xaa;	DM(s0_dat_recv+ts1)=AX0;
	AX0=0xaa;	DM(s0_dat_recv+ts2)=AX0;
	AX0=0xaa;	DM(s0_dat_recv+ts3)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts4)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts5)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts6)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts7)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts8)=AX0;
{ control mode set }
	I0 =^s0_ctl_send; M0=1; L0=8; 
	I1 =^s0_ctl_recv; M1=1; L1=8; 
	AX0 =DM(I0,M0); TX0=AX0;
	AX0=0x17; 		/* tx=I0,rx=I1 autobuf enable */
	DM(Sport0_Autobuf_Ctrl)=AX0;
	AX0 = 0x1408; DM(Sys_Ctrl_Reg) = AX0;	/* sport0 enable */
	ifc=0x3f; 				/* all int clr */
	IMASK=0x08;				/* s0 rx int enable */
loop1:	idle;
	CALL control_mode_chk;
	IF NE jump loop1;
	ifc=0x3f; 
	IMASK=0x10;				/* s0 tx int enb */
	AX0=0x0C; DM(s0_ctl_send+ts1)=AX0;	/* clb set */
/*	AX0=0x04; DM(s0_ctl_send+ts1)=AX0;	   clb set */
	idle;
	idle;
	idle;
	IMASK=0; IFC=0x3F; 			
	AX0=0x00; DM(Sport0_Autobuf_Ctrl)=AX0;
	AX0 = 0x0408; DM(Sys_Ctrl_Reg) = AX0;	/* sport0 disable */
{ dsp set data mode }	
	AX0=0x8607; DM(Sport0_Ctrl_Reg)=AX0;	/* rfs receive mode */
	AX0=0xff;   DM(Sport0_Tx_Words0)=AX0;   /* 64bit */
	AX0=0xff;   DM(Sport0_Rx_Words0)=AX0;	/* 64bit */
	AX0=0x00;   DM(Sport0_Tx_Words1)=AX0;
	AX0=0x00;   DM(Sport0_Rx_Words1)=AX0;
	I0 =^s0_dat_send; M0=1; L0=8;
	I1 =^s0_dat_recv; M1=1; L1=8;
	I2 =^s0_dat_recv; M2=1; L2=4;
	I3 =^s0_dat_send; M3=1; L3=4;
	AX0 = 0x1408; DM(Sys_Ctrl_Reg) = AX0; /* sport0 enable */
	AX0=DM(I0,M0); TX0=AX0;		
	AX0=0x17; 		/* tx=I0,rx=I1 autobuf enable */
	DM(Sport0_Autobuf_Ctrl)=AX0;   
	AX0=dc;	DM(codec_ctrl)=AX0;	/* set dc to high */
	IFC=0x3f; IMASK=0x08;	
loop2:	idle;
	AY1=DM(I2,M2); DM(I3,M3)=AY1;	/* left  msb 8bit */
	AY1=DM(I2,M2); DM(I3,M3)=AY1;	/* left  lsb 8bit */
	AY1=DM(I2,M2); DM(I3,M3)=AY1;	/* right msb 8bit */
	AY1=DM(I2,M2); DM(I3,M3)=AY1;	/* right msb 8bit */
	jump loop2;
	
control_mode_chk: 
	{ for CS4215 rev.1 }
	AX0=DM(s0_ctl_recv+ts1);
	AY0=0x04; AR=AX0 and AY0;
	IF NE jump chk1;
	AX0=DM(s0_ctl_send+ts2);
	AY0=DM(s0_ctl_recv+ts2);
	AR=AX0-AY0;
	IF NE jump chk1;
	AX0=DM(s0_ctl_send+ts3);
	AY0=DM(s0_ctl_recv+ts3);
	AF=AX0 xor AY0;
	AX0=0xFF7F; AR=AX0 and AF;
chk1:	rts;

s0send:	rti;
s0recv:	rti;

.ENDMOD;

=============================================================================


.MODULE/RAM/BOOT = 0/ABS = 0	sine;

.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

{ SYSTEM I/O ADDRESS DEFINES }
.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

.CONST comcrdy	= 0x0803;
.CONST comdrdy	= 0x0804;
.CONST cpudt 	= 0x0810;
.CONST dspdt	= 0x0811;

{ BOAD REGISTERS }
.CONST hs_readonly	= 0x0600;
.CONST handshake	= 0x0700;
.CONST dipswitch	= 0x0400;
.CONST codec_ctrl	= 0x0400;

{ CONTROLE BITS }
.CONST d2c	= 0x2000;
.CONST c2d	= 0x1000;
.CONST s8	= 0x8000;
.CONST s4	= 0x4000;
.CONST s2	= 0x2000;
.CONST s1	= 0x1000;
.CONST dc	= 0x1000;
.CONST c2denb	= 0x2000;
.CONST extenb	= 0x4000;

{ MEMORY STRUCTURE }
.CONST	dm_top		= 0x0800;
.CONST	dm_bottom	= 0x3BFF;
.CONST	pm_top		= 0x0000;
.CONST	pm_bottom	= 0x3FFF;

.CONST	ext_dm_top	= 0x0800;
.CONST	ext_dm_bottom	= 0x37FF;
.CONST	int_dm_top	= 0x3800;
.CONST	int_dm_bottom	= 0x3BFF;
.CONST	ext_pm_top	= 0x0800;
.CONST	ext_pm_bottom	= 0x3FFF;
.CONST	int_pm_top	= 0x0000;
.CONST	int_pm_bottom	= 0x07FF;

{ TERMINATE PROGRAM }
.CONST	end_of_prog	= 0x001D;

/******************** Special Macro Defines ********************/
#define data_to_mem(data,memory)	AX0=data; DM(memory)=AX0;
#define data_to_reg(data,register)	AX0=data; DM(register)=AX0;
#define interrupt_disable		IFC=0x3f; IMASK=0x00;
#define interrupt_s0_rx_enable		IFC=0x3f; IMASK=0x08;
#define interrupt_s0_tx_enable		IFC=0x3f; IMASK=0x10;
#define waiting_for_interrupt		idle;
#define direct_tx_port_0(i_reg,m_reg)	AX0=DM(i_reg,m_reg); TX0=AX0;
#define serial_port_0_disable		data_to_reg(0x0408,Sys_Ctrl_Reg)
#define serial_port_0_enable		data_to_reg(0x1408,Sys_Ctrl_Reg)
#define autobuf_port_0_disable		data_to_reg(0x0000,Sport0_Autobuf_Ctrl)
#define autobuf_port_0_enable		data_to_reg(0x0017,Sport0_Autobuf_Ctrl)
.MACRO codec_to_AR_reg_mono;
	SR0=DM(I3,M3); AY0=DM(I3,M3);
	SR=lshift SR0 by 8 (lo); AR=SR0 or AY0;
.ENDMACRO;
.MACRO SR0_reg_to_codec_mono;
	AY0=0xff; AR=SR0 and AY0; SR=lshift SR0 by -8 (lo);
	DM(I2,M2)=SR0; DM(I2,M2)=AR;
.ENDMACRO;

/******************** Buffer Memory Defines ********************/
.VAR/DM/RAM/ABS=0x0900/CIRC	fifo_tx[8];
.VAR/DM/RAM/ABS=0x0910/CIRC	fifo_rx[8];

/******************** Reset Vectors Defines ********************/
jump start;	nop; nop; nop;	{Reset Vector}
	RTI;	nop; nop; nop;	{irq2}
	RTI;	nop; nop; nop;	{sport0 TX}
	RTI;	nop; nop; nop;	{sport0 RX}
	RTI;	nop; nop; nop;	{irq0}
	RTI;	nop; nop; nop;	{irq1}
	RTI;	nop; nop; nop;	{timer}

/******************** Common Subroutines ********************/
codec_initialize:
	call codec_system_init;
	call codec_init_command_mode;
	call codec_init_data_mode;
	rts;
codec_system_init:
	data_to_reg(0x01,Dm_Wait_Reg)		/* 1 wait */
	data_to_reg(0xff,Sport0_Rfsdiv)		/* Multi-ch, 256bit */
	data_to_reg(0x00,Sport0_Sclkdiv)	/* divide rate */
	data_to_reg(0xff,Sport0_Tx_Words0)	/* LSB 16bit Tx */
	data_to_reg(0xff,Sport0_Rx_Words0)	/*           Rx */
	data_to_reg(0x00,Sport0_Tx_Words1)	/* MSB 16bit Tx */
	data_to_reg(0x00,Sport0_Rx_Words1)	/*           Rx */
	rts;
codec_init_command_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x08,fifo_tx+0)	/* olb=1, clb=0(control) */
	data_to_mem(0x1c,fifo_tx+1)	/* 32KHz, Stereo, 16bit */
	data_to_mem(0x9a,fifo_tx+2)	/* 256bit, x1, gen.clk */
	data_to_mem(0x00,fifo_tx+3)	/* (reserved) */
	data_to_mem(0xc0,fifo_tx+4)	/* (reserved) */
	data_to_mem(0x00,fifo_tx+6)	/* (reserved) */
	I0=^fifo_tx; M0=1; L0=8; 
	I1=^fifo_rx; M1=1; L1=8; 
	data_to_reg(0xc707,Sport0_Ctrl_Reg)	/* CODEC --> Setting Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
loop_1:
	waiting_for_interrupt
	call check_1;
	if ne jump loop_1;
	interrupt_s0_tx_enable
	data_to_mem(0x000C,fifo_tx+0)		/* clb set */
	waiting_for_interrupt
	waiting_for_interrupt
	waiting_for_interrupt
	rts;
check_1: 
	AX0=DM(fifo_rx+0); AY0=0x04; AR=AX0 and AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+1); AY0=DM(fifo_rx+1); AR=AX0-AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+2); AY0=DM(fifo_rx+2); AF=AX0 xor AY0;
	AX0=0xFF7F; AR=AX0 and AF;
chk_exit:
	rts;
codec_init_data_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x00,fifo_tx+0)	/* (dummy) */
	data_to_mem(0x00,fifo_tx+1)	/*    |    */
	data_to_mem(0x00,fifo_tx+2)	/*    |    */
	data_to_mem(0x00,fifo_tx+3)	/* (dummy) */
	data_to_mem(0xc0,fifo_tx+4)	/* Output Enable, Lch : 0db ATT */
	data_to_mem(0x00,fifo_tx+5)	/*                Rch : 0db ATT */
	data_to_mem(0xc6,fifo_tx+6)	/* Input <- Line, Lch : gain 9dB */
	data_to_mem(0xf6,fifo_tx+7)	/*                Rch : gain 9dB */
	I0=^fifo_tx; M0=1; L0=8;
	I1=^fifo_rx; M1=1; L1=8;
	I2=^fifo_tx; M2=1; L2=2;		/* Data Buffer for Tx (L) */
	I3=^fifo_rx; M3=1; L3=2;		/*                 Rx (L) */
	data_to_reg(0x8607,Sport0_Ctrl_Reg)	/* CODEC --> Running Mode */
	data_to_reg(dc,codec_ctrl)		/* Change to Data Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
	rts;


.VAR/DM/RAM/ABS=0x1000/CIRC	dac_buf[0x1000];
.VAR/DM/RAM/ABS=0x2000		dac_amt;
.VAR/DM/RAM/ABS=0x2010/CIRC	s0_ctl_send[8];
.VAR/DM/RAM/ABS=0x2020/CIRC	s0_ctl_recv[8];
.VAR/DM/RAM/ABS=0x2030/CIRC	s0_dat_send[8];
.VAR/DM/RAM/ABS=0x2040/CIRC	s0_dat_recv[8];

#define	ts1	0
#define	ts2	1
#define	ts3	2
#define	ts4	3
#define	ts5	4
#define	ts6	5
#define	ts7	6
#define	ts8	7

{ interupt vector table }
        jump start; RTI; RTI; RTI;	{Reset Vector}    
	RTI; RTI; RTI; RTI;		{irq2}
        jump s0send; RTI; RTI; RTI;	{sport0 TX}
        jump s0recv; RTI; RTI; RTI;	{sport0 RX}
        RTI; RTI; RTI; RTI;		{irq0}
        RTI; RTI; RTI; RTI;		{irq1}
        RTI; RTI; RTI; RTI;		{timer}

#include ;

start:
	ifc=0x3f; IMASK=0x00;			/* int disable */
	AX0 = 0x0408; DM(Sys_Ctrl_Reg) = AX0;	/* sport0 disable */
	AX0 = 1; DM(Dm_Wait_Reg)  = AX0;
	
initcodec:
	AX0=0x00ff; DM(Sport0_Tx_Words0)=AX0;	/* 64bit */ 
	AX0=0x00ff; DM(Sport0_Rx_Words0)=AX0;	/* 64bit */
	AX0=0x0000; DM(Sport0_Tx_Words1)=AX0;
	AX0=0x0000; DM(Sport0_Rx_Words1)=AX0;
	AX0=0xc707; DM(Sport0_Ctrl_Reg)=AX0;	/* rfs send mode */
	AX0=0x0000; DM(Sport0_Sclkdiv)=AX0;
	AX0=255;   DM(Sport0_Rfsdiv)=AX0;	/* multichannel,256bit */
{ control mode timeslot }
	AX0=0x8;	DM(s0_ctl_send+ts1)=AX0; /* olb=1,clb=0 */
/*	AX0=0x0;	DM(s0_ctl_send+ts1)=AX0;    olb=0,clb=0 */
	AX0=0x34;	DM(s0_ctl_send+ts2)=AX0; /* 48KHz,st,16bit */
	AX0=0x9a;	DM(s0_ctl_send+ts3)=AX0; /* 256b,x1,gen.clk */
	AX0=0;		DM(s0_ctl_send+ts4)=AX0;
	AX0=0xc0;	DM(s0_ctl_send+ts5)=AX0;
	AX0=0;		DM(s0_ctl_send+ts6)=AX0;
	AX0=0;		DM(s0_ctl_send+ts7)=AX0;
	AX0=0;		DM(s0_ctl_send+ts8)=AX0;
	AX0=0xffff;	DM(s0_ctl_recv+ts1)=AX0;
	AX0=0xffff;	DM(s0_ctl_recv+ts2)=AX0;
	AX0=0xffff;	DM(s0_ctl_recv+ts3)=AX0;
{ data time slot }	
	AX0=0x0;	DM(s0_dat_send+ts1)=AX0;
	AX0=0x0;	DM(s0_dat_send+ts2)=AX0;
	AX0=0x0;	DM(s0_dat_send+ts3)=AX0;
	AX0=0x0;	DM(s0_dat_send+ts4)=AX0;
	AX0=0xc5;	DM(s0_dat_send+ts5)=AX0;
	AX0=0x05;	DM(s0_dat_send+ts6)=AX0;
	AX0=0xd0;	DM(s0_dat_send+ts7)=AX0;
	AX0=0xf0;	DM(s0_dat_send+ts8)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts1)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts2)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts3)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts4)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts5)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts6)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts7)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts8)=AX0;
{ control mode set }
	I0 =^s0_ctl_send; M0=1; L0=8; 
	I1 =^s0_ctl_recv; M1=1; L1=8; 
	AX0 =DM(I0,M0); TX0=AX0;
	AX0=0x17; 
	SI = RX0; 
	DM(Sport0_Autobuf_Ctrl)=AX0;
	AX0 = 0x1408; 
	SI = RX0; 
	DM(Sys_Ctrl_Reg) = AX0;
	IMASK=0x08;				/* s0 rx int enb */
loop1:	idle;
	CALL control_mode_chk;
	IF NE jump loop1;
	IMASK=0x10;				/* s0 tx int enb */
	AX0=0x0C; DM(s0_ctl_send+ts1)=AX0;	/* clb set */
	idle;
	idle;
	IMASK=0;
{ dsp set data mode }	
	AX0=0x8607; DM(Sport0_Ctrl_Reg)=AX0;
	AX0=0x00ff; DM(Sport0_Tx_Words0)=AX0; 
	AX0=0x00ff; DM(Sport0_Rx_Words0)=AX0; 
	AX0=0x0000; DM(Sport0_Tx_Words1)=AX0;
	AX0=0x0000; DM(Sport0_Rx_Words1)=AX0;
{ }
	I2 =^dac_buf; M2=1; L2=DM(dac_amt); 
	SI =DM(dac_amt); SR=LSHIFT SI BY -1 (LO);
	AY0=^dac_buf; AR=SR0+AY0;
	I3=AR; M3=1; L3=DM(dac_amt); 
	I0 =^s0_dat_send; M0=1; L0=8; 
	I1 =^s0_dat_recv; M1=1; L1=8; 
	AX0=0x16; SI = RX0; 
	DM(Sport0_Autobuf_Ctrl)=AX0;
	CALL sin_dat_set;
	IMASK=0x10;			
	AX0=dc;	DM(codec_ctrl)=AX0;	/* set dc to high */
loop2:	idle;
	CALL sin_dat_set;
	jump loop2;
	
control_mode_chk: 
	{ for CS4215 rev. 1 }
	AX0=DM(s0_ctl_recv+ts1);
	AY0=0x04; AR=AX0 and AY0;
	IF NE jump chk1;
	AX0=DM(s0_ctl_send+ts2);
	AY0=DM(s0_ctl_recv+ts2);
	AR=AX0-AY0;
	IF NE jump chk1;
	AX0=DM(s0_ctl_send+ts3);
	AY0=DM(s0_ctl_recv+ts3);
	AF=AX0 xor AY0;
	AX0=0xFF7F; AR=AX0 and AF;
chk1:	rts;

sin_dat_set:
	SI=DM(I2,M2);
	SR=LSHIFT SI BY -8 (LO);
	DM(s0_dat_send+ts1)=SR0;
	DM(s0_dat_send+ts2)=SI;
	SI=DM(I3,M3);
	SR=LSHIFT SI BY -8 (LO);
	DM(s0_dat_send+ts3)=SR0;
	DM(s0_dat_send+ts4)=SI;
	rts;

s0send:	rti;
s0recv:	rti;

.ENDMOD;

=============================================================================


.MODULE/RAM/BOOT = 0/ABS = 0	selftest;

.CONST dm_test_top = 0x0820;
.CONST pm_test_top = 0x1000;
.CONST complete	= 0;
.CONST dm_fail	= 1;
.CONST pm_fail	= 2;
.CONST erradr	= 0x0812;

.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

{ SYSTEM I/O ADDRESS DEFINES }
.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

.CONST comcrdy	= 0x0803;
.CONST comdrdy	= 0x0804;
.CONST cpudt 	= 0x0810;
.CONST dspdt	= 0x0811;

{ BOAD REGISTERS }
.CONST hs_readonly	= 0x0600;
.CONST handshake	= 0x0700;
.CONST dipswitch	= 0x0400;
.CONST codec_ctrl	= 0x0400;

{ CONTROLE BITS }
.CONST d2c	= 0x2000;
.CONST c2d	= 0x1000;
.CONST s8	= 0x8000;
.CONST s4	= 0x4000;
.CONST s2	= 0x2000;
.CONST s1	= 0x1000;
.CONST dc	= 0x1000;
.CONST c2denb	= 0x2000;
.CONST extenb	= 0x4000;

{ MEMORY STRUCTURE }
.CONST	dm_top		= 0x0800;
.CONST	dm_bottom	= 0x3BFF;
.CONST	pm_top		= 0x0000;
.CONST	pm_bottom	= 0x3FFF;

.CONST	ext_dm_top	= 0x0800;
.CONST	ext_dm_bottom	= 0x37FF;
.CONST	int_dm_top	= 0x3800;
.CONST	int_dm_bottom	= 0x3BFF;
.CONST	ext_pm_top	= 0x0800;
.CONST	ext_pm_bottom	= 0x3FFF;
.CONST	int_pm_top	= 0x0000;
.CONST	int_pm_bottom	= 0x07FF;

{ TERMINATE PROGRAM }
.CONST	end_of_prog	= 0x001D;

/******************** Special Macro Defines ********************/
#define data_to_mem(data,memory)	AX0=data; DM(memory)=AX0;
#define data_to_reg(data,register)	AX0=data; DM(register)=AX0;
#define interrupt_disable		IFC=0x3f; IMASK=0x00;
#define interrupt_s0_rx_enable		IFC=0x3f; IMASK=0x08;
#define interrupt_s0_tx_enable		IFC=0x3f; IMASK=0x10;
#define waiting_for_interrupt		idle;
#define direct_tx_port_0(i_reg,m_reg)	AX0=DM(i_reg,m_reg); TX0=AX0;
#define serial_port_0_disable		data_to_reg(0x0408,Sys_Ctrl_Reg)
#define serial_port_0_enable		data_to_reg(0x1408,Sys_Ctrl_Reg)
#define autobuf_port_0_disable		data_to_reg(0x0000,Sport0_Autobuf_Ctrl)
#define autobuf_port_0_enable		data_to_reg(0x0017,Sport0_Autobuf_Ctrl)
.MACRO codec_to_AR_reg_mono;
	SR0=DM(I3,M3); AY0=DM(I3,M3);
	SR=lshift SR0 by 8 (lo); AR=SR0 or AY0;
.ENDMACRO;
.MACRO SR0_reg_to_codec_mono;
	AY0=0xff; AR=SR0 and AY0; SR=lshift SR0 by -8 (lo);
	DM(I2,M2)=SR0; DM(I2,M2)=AR;
.ENDMACRO;

/******************** Buffer Memory Defines ********************/
.VAR/DM/RAM/ABS=0x0900/CIRC	fifo_tx[8];
.VAR/DM/RAM/ABS=0x0910/CIRC	fifo_rx[8];

/******************** Reset Vectors Defines ********************/
jump start;	nop; nop; nop;	{Reset Vector}
	RTI;	nop; nop; nop;	{irq2}
	RTI;	nop; nop; nop;	{sport0 TX}
	RTI;	nop; nop; nop;	{sport0 RX}
	RTI;	nop; nop; nop;	{irq0}
	RTI;	nop; nop; nop;	{irq1}
	RTI;	nop; nop; nop;	{timer}

/******************** Common Subroutines ********************/
codec_initialize:
	call codec_system_init;
	call codec_init_command_mode;
	call codec_init_data_mode;
	rts;
codec_system_init:
	data_to_reg(0x01,Dm_Wait_Reg)		/* 1 wait */
	data_to_reg(0xff,Sport0_Rfsdiv)		/* Multi-ch, 256bit */
	data_to_reg(0x00,Sport0_Sclkdiv)	/* divide rate */
	data_to_reg(0xff,Sport0_Tx_Words0)	/* LSB 16bit Tx */
	data_to_reg(0xff,Sport0_Rx_Words0)	/*           Rx */
	data_to_reg(0x00,Sport0_Tx_Words1)	/* MSB 16bit Tx */
	data_to_reg(0x00,Sport0_Rx_Words1)	/*           Rx */
	rts;
codec_init_command_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x08,fifo_tx+0)	/* olb=1, clb=0(control) */
	data_to_mem(0x1c,fifo_tx+1)	/* 32KHz, Stereo, 16bit */
	data_to_mem(0x9a,fifo_tx+2)	/* 256bit, x1, gen.clk */
	data_to_mem(0x00,fifo_tx+3)	/* (reserved) */
	data_to_mem(0xc0,fifo_tx+4)	/* (reserved) */
	data_to_mem(0x00,fifo_tx+6)	/* (reserved) */
	I0=^fifo_tx; M0=1; L0=8; 
	I1=^fifo_rx; M1=1; L1=8; 
	data_to_reg(0xc707,Sport0_Ctrl_Reg)	/* CODEC --> Setting Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
loop_1:
	waiting_for_interrupt
	call check_1;
	if ne jump loop_1;
	interrupt_s0_tx_enable
	data_to_mem(0x000C,fifo_tx+0)		/* clb set */
	waiting_for_interrupt
	waiting_for_interrupt
	waiting_for_interrupt
	rts;
check_1: 
	AX0=DM(fifo_rx+0); AY0=0x04; AR=AX0 and AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+1); AY0=DM(fifo_rx+1); AR=AX0-AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+2); AY0=DM(fifo_rx+2); AF=AX0 xor AY0;
	AX0=0xFF7F; AR=AX0 and AF;
chk_exit:
	rts;
codec_init_data_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x00,fifo_tx+0)	/* (dummy) */
	data_to_mem(0x00,fifo_tx+1)	/*    |    */
	data_to_mem(0x00,fifo_tx+2)	/*    |    */
	data_to_mem(0x00,fifo_tx+3)	/* (dummy) */
	data_to_mem(0xc0,fifo_tx+4)	/* Output Enable, Lch : 0db ATT */
	data_to_mem(0x00,fifo_tx+5)	/*                Rch : 0db ATT */
	data_to_mem(0xc6,fifo_tx+6)	/* Input <- Line, Lch : gain 9dB */
	data_to_mem(0xf6,fifo_tx+7)	/*                Rch : gain 9dB */
	I0=^fifo_tx; M0=1; L0=8;
	I1=^fifo_rx; M1=1; L1=8;
	I2=^fifo_tx; M2=1; L2=2;		/* Data Buffer for Tx (L) */
	I3=^fifo_rx; M3=1; L3=2;		/*                 Rx (L) */
	data_to_reg(0x8607,Sport0_Ctrl_Reg)	/* CODEC --> Running Mode */
	data_to_reg(dc,codec_ctrl)		/* Change to Data Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
	rts;


{ interupt vector table }
        JUMP start; RTI; RTI; RTI;      {Reset Vector}    
	JUMP handler; RTI; RTI; RTI;	{irq2}
        RTI; RTI; RTI; RTI;             {sport0 TX}
        RTI; RTI; RTI; RTI;             {sport0 RX}
        RTI; RTI; RTI; RTI;             {irq0}
        RTI; RTI; RTI; RTI;             {irq1}
        RTI; RTI; RTI; RTI;             {timer}

#include ;

start:
	AX0 = 0x0408; DM(Sys_Ctrl_Reg) = AX0;
	AX0 = 1; DM(Dm_Wait_Reg)  = AX0;
	
dm_5555:
	SI = 0x5555;
	CALL dmfill;
	SI = 0x1234;	{ jam }
	PX = 0x67;
	CALL pmfill;
	AX0 = 0x5555;
	CALL dmcheck;
	AR = PASS AX1;
	IF EQ JUMP dm_AAAA;
		AX0 = 0; DM(comdrdy) = AX0; DM(comcrdy) = AX0;
		DM(erradr) = AY1;
		SI = dm_fail; CALL put_wd;
		IDLE;
dm_AAAA:
	SI = 0xAAAA;
	CALL dmfill;
	SI = 0xfedc;	{ jam }
	PX = 0xb9;
	CALL pmfill;
	AX0 = 0xAAAA;
	CALL dmcheck;
	AR = PASS AX1;
	IF EQ JUMP dm_end;
		AX0 = 0; DM(comdrdy) = AX0; DM(comcrdy) = AX0;
		DM(comcrdy) = AX0;
		DM(erradr) = AY1;
		SI = dm_fail; CALL put_wd;
		IDLE;
dm_end:
pm_5555:
	SI = 0x5555;
	PX = 0x55;
	CALL pmfill;
	SI = 0x1234;	{ JAM }
	CALL dmfill;
	AX0 = 0x5555;
	PX = 0x55;
	CALL pmcheck;
	AR = PASS AX1;
	IF EQ JUMP pm_AAAA;
		AX0 = 0; DM(comdrdy) = AX0; DM(comcrdy) = AX0;
		DM(erradr) = AY1;
		SI = pm_fail; CALL put_wd;
		IDLE;
pm_AAAA:
	SI = 0xAAAA;
	PX = 0xAA;
	CALL pmfill;
	SI = 0xfedc;	{ JAM }
	CALL dmfill;
	AX0 = 0xAAAA;
	PX = 0xAA;
	CALL pmcheck;
	AR = PASS AX1;
	IF EQ JUMP pm_end;
		AX0 = 0; DM(comdrdy) = AX0; DM(comcrdy) = AX0;
		DM(erradr) = AY1;
		SI = pm_fail; CALL put_wd;
		IDLE;
pm_end:
	AX0 = 0; DM(comdrdy) = AX0; DM(comcrdy) = AX0;
	DM(erradr) = AY1;
	SI = complete; CALL put_wd;
int_cpu:
	CALL intcheck;

int_dsp:
	CALL intdsp;

	IDLE;
{--------------------}
{ fill data memory with SI }
dmfill:
	I0 = dm_test_top; M0 = 1; L0 = 0;
	CNTR = dm_bottom - dm_test_top + 1;
	DO fill_0 UNTIL CE;
fill_0:		DM(I0, M0) = SI;
	RTS;

{--------------------}
{ check data memory with AX0 }
dmcheck:
	I0 = dm_test_top; M0 = 1; L0 = 0;
	AR = dm_bottom - dm_test_top + 1;
	AF = PASS AR;
check_0:	AY0 = DM(I0, M0);
		AR = AX0 - AY0;
		IF EQ JUMP continue_0;
			AX1 = dm_bottom + 1;
			AR = AX1 - AF; AY1 = AR;
			AX1 = 0xFFFF;
			RTS;
continue_0:	AF = AF - 1;
		IF NE JUMP check_0;
	AX1 = dm_bottom ;
	AR = AX1 - AF; AY1 = AR;
	AX1 = 0;
	RTS;

{--------------------}
{ fill program memory with SI }
pmfill:
	I4 = pm_test_top; M4 = 1; L4 = 0;
	CNTR = pm_bottom - pm_test_top + 1;
	DO fill_1 UNTIL CE;
fill_1:		PM(I4, M4) = SI;
	RTS;

{--------------------}
{ check program memory with AX0 }
pmcheck:
	AX1 = PX;
	I4 = pm_test_top; M4 = 1; L4 = 0;
	AR = pm_bottom - pm_test_top + 1;
	AF = PASS AR;
check_1:	AY0 = PM(I4, M4);
		AR = AX0 - AY0;
		IF NE JUMP else_1;

		AY0 = PX;
		AR = AX1 - AY0;
		IF NE JUMP else_1;

continue_1:	AF = AF - 1;
		IF NE JUMP check_1;
	AX1 = pm_bottom ;
	AR = AX1 - AF; AY1 = AR;
	AX1 = 0;
	RTS;

else_1: AX1 = pm_bottom + 1;
	AR = AX1 - AF; AY1 = AR;
	AX1 = 0xFFFF;
	RTS;

{--------------------}
{ check Interrupt to CPU }
intcheck:
	AX1 = 0;   DM(handshake) = AX1; /* make int signal to low */
	CALL get_wd;	/* wait for CPU executes put_word(NULL); */

	AX1 = d2c; DM(handshake) = AX1; /* make int signal hi */
	RTS;

{--------------------}
{ check Interrupt to DSP }
intdsp:
	AX0=c2denb; DM(codec_ctrl)=AX0;
	IFC=0x20;
	CALL put_wd; /* dummy data */
	IMASK = 0x20;
	IDLE;
	AX0=0; DM(codec_ctrl)=AX0;
	IMASK = 0;
	RTS;

{--------------------}
{ Interrupt handler }
handler:
	AX1 = DM(handshake);
	RTI;

.ENDMOD;

=============================================================================


.MODULE/RAM/BOOT=0/ABS=0	test;

.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

{ SYSTEM I/O ADDRESS DEFINES }
.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

.CONST comcrdy	= 0x0803;
.CONST comdrdy	= 0x0804;
.CONST cpudt 	= 0x0810;
.CONST dspdt	= 0x0811;

{ BOAD REGISTERS }
.CONST hs_readonly	= 0x0600;
.CONST handshake	= 0x0700;
.CONST dipswitch	= 0x0400;
.CONST codec_ctrl	= 0x0400;

{ CONTROLE BITS }
.CONST d2c	= 0x2000;
.CONST c2d	= 0x1000;
.CONST s8	= 0x8000;
.CONST s4	= 0x4000;
.CONST s2	= 0x2000;
.CONST s1	= 0x1000;
.CONST dc	= 0x1000;
.CONST c2denb	= 0x2000;
.CONST extenb	= 0x4000;

{ MEMORY STRUCTURE }
.CONST	dm_top		= 0x0800;
.CONST	dm_bottom	= 0x3BFF;
.CONST	pm_top		= 0x0000;
.CONST	pm_bottom	= 0x3FFF;

.CONST	ext_dm_top	= 0x0800;
.CONST	ext_dm_bottom	= 0x37FF;
.CONST	int_dm_top	= 0x3800;
.CONST	int_dm_bottom	= 0x3BFF;
.CONST	ext_pm_top	= 0x0800;
.CONST	ext_pm_bottom	= 0x3FFF;
.CONST	int_pm_top	= 0x0000;
.CONST	int_pm_bottom	= 0x07FF;

{ TERMINATE PROGRAM }
.CONST	end_of_prog	= 0x001D;

/******************** Special Macro Defines ********************/
#define data_to_mem(data,memory)	AX0=data; DM(memory)=AX0;
#define data_to_reg(data,register)	AX0=data; DM(register)=AX0;
#define interrupt_disable		IFC=0x3f; IMASK=0x00;
#define interrupt_s0_rx_enable		IFC=0x3f; IMASK=0x08;
#define interrupt_s0_tx_enable		IFC=0x3f; IMASK=0x10;
#define waiting_for_interrupt		idle;
#define direct_tx_port_0(i_reg,m_reg)	AX0=DM(i_reg,m_reg); TX0=AX0;
#define serial_port_0_disable		data_to_reg(0x0408,Sys_Ctrl_Reg)
#define serial_port_0_enable		data_to_reg(0x1408,Sys_Ctrl_Reg)
#define autobuf_port_0_disable		data_to_reg(0x0000,Sport0_Autobuf_Ctrl)
#define autobuf_port_0_enable		data_to_reg(0x0017,Sport0_Autobuf_Ctrl)
.MACRO codec_to_AR_reg_mono;
	SR0=DM(I3,M3); AY0=DM(I3,M3);
	SR=lshift SR0 by 8 (lo); AR=SR0 or AY0;
.ENDMACRO;
.MACRO SR0_reg_to_codec_mono;
	AY0=0xff; AR=SR0 and AY0; SR=lshift SR0 by -8 (lo);
	DM(I2,M2)=SR0; DM(I2,M2)=AR;
.ENDMACRO;

/******************** Buffer Memory Defines ********************/
.VAR/DM/RAM/ABS=0x0900/CIRC	fifo_tx[8];
.VAR/DM/RAM/ABS=0x0910/CIRC	fifo_rx[8];

/******************** Reset Vectors Defines ********************/
jump start;	nop; nop; nop;	{Reset Vector}
	RTI;	nop; nop; nop;	{irq2}
	RTI;	nop; nop; nop;	{sport0 TX}
	RTI;	nop; nop; nop;	{sport0 RX}
	RTI;	nop; nop; nop;	{irq0}
	RTI;	nop; nop; nop;	{irq1}
	RTI;	nop; nop; nop;	{timer}

/******************** Common Subroutines ********************/
codec_initialize:
	call codec_system_init;
	call codec_init_command_mode;
	call codec_init_data_mode;
	rts;
codec_system_init:
	data_to_reg(0x01,Dm_Wait_Reg)		/* 1 wait */
	data_to_reg(0xff,Sport0_Rfsdiv)		/* Multi-ch, 256bit */
	data_to_reg(0x00,Sport0_Sclkdiv)	/* divide rate */
	data_to_reg(0xff,Sport0_Tx_Words0)	/* LSB 16bit Tx */
	data_to_reg(0xff,Sport0_Rx_Words0)	/*           Rx */
	data_to_reg(0x00,Sport0_Tx_Words1)	/* MSB 16bit Tx */
	data_to_reg(0x00,Sport0_Rx_Words1)	/*           Rx */
	rts;
codec_init_command_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x08,fifo_tx+0)	/* olb=1, clb=0(control) */
	data_to_mem(0x1c,fifo_tx+1)	/* 32KHz, Stereo, 16bit */
	data_to_mem(0x9a,fifo_tx+2)	/* 256bit, x1, gen.clk */
	data_to_mem(0x00,fifo_tx+3)	/* (reserved) */
	data_to_mem(0xc0,fifo_tx+4)	/* (reserved) */
	data_to_mem(0x00,fifo_tx+6)	/* (reserved) */
	I0=^fifo_tx; M0=1; L0=8; 
	I1=^fifo_rx; M1=1; L1=8; 
	data_to_reg(0xc707,Sport0_Ctrl_Reg)	/* CODEC --> Setting Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
loop_1:
	waiting_for_interrupt
	call check_1;
	if ne jump loop_1;
	interrupt_s0_tx_enable
	data_to_mem(0x000C,fifo_tx+0)		/* clb set */
	waiting_for_interrupt
	waiting_for_interrupt
	waiting_for_interrupt
	rts;
check_1: 
	AX0=DM(fifo_rx+0); AY0=0x04; AR=AX0 and AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+1); AY0=DM(fifo_rx+1); AR=AX0-AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+2); AY0=DM(fifo_rx+2); AF=AX0 xor AY0;
	AX0=0xFF7F; AR=AX0 and AF;
chk_exit:
	rts;
codec_init_data_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x00,fifo_tx+0)	/* (dummy) */
	data_to_mem(0x00,fifo_tx+1)	/*    |    */
	data_to_mem(0x00,fifo_tx+2)	/*    |    */
	data_to_mem(0x00,fifo_tx+3)	/* (dummy) */
	data_to_mem(0xc0,fifo_tx+4)	/* Output Enable, Lch : 0db ATT */
	data_to_mem(0x00,fifo_tx+5)	/*                Rch : 0db ATT */
	data_to_mem(0xc6,fifo_tx+6)	/* Input <- Line, Lch : gain 9dB */
	data_to_mem(0xf6,fifo_tx+7)	/*                Rch : gain 9dB */
	I0=^fifo_tx; M0=1; L0=8;
	I1=^fifo_rx; M1=1; L1=8;
	I2=^fifo_tx; M2=1; L2=2;		/* Data Buffer for Tx (L) */
	I3=^fifo_rx; M3=1; L3=2;		/*                 Rx (L) */
	data_to_reg(0x8607,Sport0_Ctrl_Reg)	/* CODEC --> Running Mode */
	data_to_reg(dc,codec_ctrl)		/* Change to Data Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
	rts;

.VAR/DM/RAM/ABS=0x2010/CIRC	s0_ctl_send[8]; /* 2010-2017 */
.VAR/DM/RAM/ABS=0x2020/CIRC	s0_ctl_recv[8]; /* 2020-2027 */
.VAR/DM/RAM/ABS=0x2030/CIRC	s0_dat_send[8]; /* 2030-2037 */
.VAR/DM/RAM/ABS=0x2040/CIRC	s0_dat_recv[8]; /* 2040-2047 */

#define	ts1	0
#define	ts2	1
#define	ts3	2
#define	ts4	3
#define	ts5	4
#define	ts6	5
#define	ts7	6
#define	ts8	7

{ interupt vector table }
        jump start; RTI; RTI; RTI;	{Reset Vector}
	RTI; RTI; RTI; RTI;		{irq2}
        jump s0send; RTI; RTI; RTI;	{sport0 TX}
        jump s0recv; RTI; RTI; RTI;	{sport0 RX}
        RTI; RTI; RTI; RTI;		{irq0}
        RTI; RTI; RTI; RTI;		{irq1}
        RTI; RTI; RTI; RTI;		{timer}

#include ;

start:
	ifc=0x3f; IMASK=0x00;			/* int disable */
	AX0=0x0408;	DM(Sys_Ctrl_Reg) = AX0;	/* sport0 disable */
	AX0=0x0001;	DM(Dm_Wait_Reg)  = AX0;
initcodec:
	AX0=0x00ff;	DM(Sport0_Tx_Words0)=AX0;	/* 64bit */ 
	AX0=0x00ff;	DM(Sport0_Rx_Words0)=AX0;	/* 64bit */
	AX0=0x0000;	DM(Sport0_Tx_Words1)=AX0;
	AX0=0x0000;	DM(Sport0_Rx_Words1)=AX0;
	AX0=0xc707;	DM(Sport0_Ctrl_Reg)=AX0;	/* rfs send mode */
	AX0=0x0000;	DM(Sport0_Sclkdiv)=AX0;
	AX0=0x00ff;	DM(Sport0_Rfsdiv)=AX0;		/* multi-ch,256bit */
{ control mode timeslot }
	AX0=0x8;	DM(s0_ctl_send+ts1)=AX0; /* olb=1,clb=0 */
	AX0=0x34;	DM(s0_ctl_send+ts2)=AX0; /* 48KHz,st,16bit */
	AX0=0x9a;	DM(s0_ctl_send+ts3)=AX0; /* 256b,x1,gen.clk */
	AX0=0;		DM(s0_ctl_send+ts4)=AX0;
	AX0=0xc0;	DM(s0_ctl_send+ts5)=AX0;
	AX0=0;		DM(s0_ctl_send+ts6)=AX0;
	AX0=0;		DM(s0_ctl_send+ts7)=AX0;
	AX0=0;		DM(s0_ctl_send+ts8)=AX0;
	AX0=0xffff;	DM(s0_ctl_recv+ts1)=AX0;
	AX0=0xffff;	DM(s0_ctl_recv+ts2)=AX0;
	AX0=0xffff;	DM(s0_ctl_recv+ts3)=AX0;
{ data time slot }	
	AX0=0x0;	DM(s0_dat_send+ts1)=AX0;
	AX0=0x0;	DM(s0_dat_send+ts2)=AX0;
	AX0=0x0;	DM(s0_dat_send+ts3)=AX0;
	AX0=0x0;	DM(s0_dat_send+ts4)=AX0;
	AX0=0xc0;	DM(s0_dat_send+ts5)=AX0; /* out enb,Rch:0dbATT */
	AX0=0x00;	DM(s0_dat_send+ts6)=AX0; /* Rch:0dbATT */
	AX0=0xc6;	DM(s0_dat_send+ts7)=AX0; /* Line-I,Lch:gain9dB */
	AX0=0xf6;	DM(s0_dat_send+ts8)=AX0; /* Rch:gain9dB */
	AX0=0xaa;	DM(s0_dat_recv+ts1)=AX0;
	AX0=0xaa;	DM(s0_dat_recv+ts2)=AX0;
	AX0=0xaa;	DM(s0_dat_recv+ts3)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts4)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts5)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts6)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts7)=AX0;
	AX0=0x0;	DM(s0_dat_recv+ts8)=AX0;
{ control mode set }
	I0 =^s0_ctl_send; M0=1; L0=8; 
	I1 =^s0_ctl_recv; M1=1; L1=8; 
	AX0 =DM(I0,M0); TX0=AX0;
	AX0=0x17; 			/* tx=I0,rx=I1 autobuf enable */
	DM(Sport0_Autobuf_Ctrl)=AX0;
	AX0 = 0x1408;	DM(Sys_Ctrl_Reg) = AX0;	/* sport0 enable */
	ifc=0x3f; 				/* all int clr */
	IMASK=0x08;				/* s0 rx int enable */
loop1:	idle;
	CALL control_mode_chk;
	IF NE jump loop1;
	ifc=0x3f; 
	IMASK=0x10;					/* s0 tx int enb */
	AX0=0x0C;	DM(s0_ctl_send+ts1)=AX0;	/* clb set */
	idle;
	idle;
	idle;
	IMASK=0; IFC=0x3F; 			
	AX0=0x00;	DM(Sport0_Autobuf_Ctrl)=AX0;
	AX0=0x0408;	DM(Sys_Ctrl_Reg) = AX0;		/* sport0 disable */
{ dsp set data mode }	
	AX0=0x8607;	DM(Sport0_Ctrl_Reg)=AX0;	/* rfs receive mode */
	AX0=0xff;	DM(Sport0_Tx_Words0)=AX0;	/* 64bit */
	AX0=0xff;	DM(Sport0_Rx_Words0)=AX0;	/* 64bit */
	AX0=0x00;	DM(Sport0_Tx_Words1)=AX0;
	AX0=0x00;	DM(Sport0_Rx_Words1)=AX0;
	I0 =^s0_dat_send; M0=1; L0=8;
	I1 =^s0_dat_recv; M1=1; L1=8;
	I2 =^s0_dat_recv; M2=1; L2=4;
	I3 =^s0_dat_send; M3=1; L3=4;
	AX0 = 0x1408;	DM(Sys_Ctrl_Reg)=AX0;		/* sport0 enable */
	AX0=DM(I0,M0); TX0=AX0;		
	AX0=0x17; 			/* tx=I0,rx=I1 autobuf enable */
	DM(Sport0_Autobuf_Ctrl)=AX0;   
	AX0=dc;		DM(codec_ctrl)=AX0;		/* set dc to high */
	IFC=0x3f; IMASK=0x08;	
loop2:	idle;
	AY1=DM(I2,M2);	DM(I3,M3)=AY1;	/* left  msb 8bit */
	AY1=DM(I2,M2);	DM(I3,M3)=AY1;	/* left  lsb 8bit */
	AY1=DM(I2,M2);	DM(I3,M3)=AY1;	/* right msb 8bit */
	AY1=DM(I2,M2);	DM(I3,M3)=AY1;	/* right msb 8bit */
	jump loop2;
control_mode_chk: 
	AX0=DM(s0_ctl_recv+ts1);
	AY0=0x04; AR=AX0 and AY0;
	IF NE jump chk1;
	AX0=DM(s0_ctl_send+ts2);
	AY0=DM(s0_ctl_recv+ts2);
	AR=AX0-AY0;
	IF NE jump chk1;
	AX0=DM(s0_ctl_send+ts3);
	AY0=DM(s0_ctl_recv+ts3);
	AF=AX0 xor AY0;
	AX0=0xFF7F; AR=AX0 and AF;
chk1:	rts;
s0send:	rti;
s0recv:	rti;

.ENDMOD;

=============================================================================


.MODULE/RAM/BOOT=0/ABS=0 test;

{ SYSTEM I/O ADDRESS DEFINES }
.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

.CONST comcrdy	= 0x0803;
.CONST comdrdy	= 0x0804;
.CONST cpudt 	= 0x0810;
.CONST dspdt	= 0x0811;

{ BOAD REGISTERS }
.CONST hs_readonly	= 0x0600;
.CONST handshake	= 0x0700;
.CONST dipswitch	= 0x0400;
.CONST codec_ctrl	= 0x0400;

{ CONTROLE BITS }
.CONST d2c	= 0x2000;
.CONST c2d	= 0x1000;
.CONST s8	= 0x8000;
.CONST s4	= 0x4000;
.CONST s2	= 0x2000;
.CONST s1	= 0x1000;
.CONST dc	= 0x1000;
.CONST c2denb	= 0x2000;
.CONST extenb	= 0x4000;

{ MEMORY STRUCTURE }
.CONST	dm_top		= 0x0800;
.CONST	dm_bottom	= 0x3BFF;
.CONST	pm_top		= 0x0000;
.CONST	pm_bottom	= 0x3FFF;

.CONST	ext_dm_top	= 0x0800;
.CONST	ext_dm_bottom	= 0x37FF;
.CONST	int_dm_top	= 0x3800;
.CONST	int_dm_bottom	= 0x3BFF;
.CONST	ext_pm_top	= 0x0800;
.CONST	ext_pm_bottom	= 0x3FFF;
.CONST	int_pm_top	= 0x0000;
.CONST	int_pm_bottom	= 0x07FF;

{ TERMINATE PROGRAM }
.CONST	end_of_prog	= 0x001D;

/******************** Special Macro Defines ********************/
#define data_to_mem(data,memory)	AX0=data; DM(memory)=AX0;
#define data_to_reg(data,register)	AX0=data; DM(register)=AX0;
#define interrupt_disable		IFC=0x3f; IMASK=0x00;
#define interrupt_s0_rx_enable		IFC=0x3f; IMASK=0x08;
#define interrupt_s0_tx_enable		IFC=0x3f; IMASK=0x10;
#define waiting_for_interrupt		idle;
#define direct_tx_port_0(i_reg,m_reg)	AX0=DM(i_reg,m_reg); TX0=AX0;
#define serial_port_0_disable		data_to_reg(0x0408,Sys_Ctrl_Reg)
#define serial_port_0_enable		data_to_reg(0x1408,Sys_Ctrl_Reg)
#define autobuf_port_0_disable		data_to_reg(0x0000,Sport0_Autobuf_Ctrl)
#define autobuf_port_0_enable		data_to_reg(0x0017,Sport0_Autobuf_Ctrl)
.MACRO codec_to_AR_reg_mono;
	SR0=DM(I3,M3); AY0=DM(I3,M3);
	SR=lshift SR0 by 8 (lo); AR=SR0 or AY0;
.ENDMACRO;
.MACRO SR0_reg_to_codec_mono;
	AY0=0xff; AR=SR0 and AY0; SR=lshift SR0 by -8 (lo);
	DM(I2,M2)=SR0; DM(I2,M2)=AR;
.ENDMACRO;

/******************** Buffer Memory Defines ********************/
.VAR/DM/RAM/ABS=0x0900/CIRC	fifo_tx[8];
.VAR/DM/RAM/ABS=0x0910/CIRC	fifo_rx[8];

/******************** Reset Vectors Defines ********************/
jump start;	nop; nop; nop;	{Reset Vector}
	RTI;	nop; nop; nop;	{irq2}
	RTI;	nop; nop; nop;	{sport0 TX}
	RTI;	nop; nop; nop;	{sport0 RX}
	RTI;	nop; nop; nop;	{irq0}
	RTI;	nop; nop; nop;	{irq1}
	RTI;	nop; nop; nop;	{timer}

/******************** Common Subroutines ********************/
codec_initialize:
	call codec_system_init;
	call codec_init_command_mode;
	call codec_init_data_mode;
	rts;
codec_system_init:
	data_to_reg(0x01,Dm_Wait_Reg)		/* 1 wait */
	data_to_reg(0xff,Sport0_Rfsdiv)		/* Multi-ch, 256bit */
	data_to_reg(0x00,Sport0_Sclkdiv)	/* divide rate */
	data_to_reg(0xff,Sport0_Tx_Words0)	/* LSB 16bit Tx */
	data_to_reg(0xff,Sport0_Rx_Words0)	/*           Rx */
	data_to_reg(0x00,Sport0_Tx_Words1)	/* MSB 16bit Tx */
	data_to_reg(0x00,Sport0_Rx_Words1)	/*           Rx */
	rts;
codec_init_command_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x08,fifo_tx+0)	/* olb=1, clb=0(control) */
	data_to_mem(0x1c,fifo_tx+1)	/* 32KHz, Stereo, 16bit */
	data_to_mem(0x9a,fifo_tx+2)	/* 256bit, x1, gen.clk */
	data_to_mem(0x00,fifo_tx+3)	/* (reserved) */
	data_to_mem(0xc0,fifo_tx+4)	/* (reserved) */
	data_to_mem(0x00,fifo_tx+6)	/* (reserved) */
	I0=^fifo_tx; M0=1; L0=8; 
	I1=^fifo_rx; M1=1; L1=8; 
	data_to_reg(0xc707,Sport0_Ctrl_Reg)	/* CODEC --> Setting Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
loop_1:
	waiting_for_interrupt
	call check_1;
	if ne jump loop_1;
	interrupt_s0_tx_enable
	data_to_mem(0x000C,fifo_tx+0)		/* clb set */
	waiting_for_interrupt
	waiting_for_interrupt
	waiting_for_interrupt
	rts;
check_1: 
	AX0=DM(fifo_rx+0); AY0=0x04; AR=AX0 and AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+1); AY0=DM(fifo_rx+1); AR=AX0-AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+2); AY0=DM(fifo_rx+2); AF=AX0 xor AY0;
	AX0=0xFF7F; AR=AX0 and AF;
chk_exit:
	rts;
codec_init_data_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x00,fifo_tx+0)	/* (dummy) */
	data_to_mem(0x00,fifo_tx+1)	/*    |    */
	data_to_mem(0x00,fifo_tx+2)	/*    |    */
	data_to_mem(0x00,fifo_tx+3)	/* (dummy) */
	data_to_mem(0xc0,fifo_tx+4)	/* Output Enable, Lch : 0db ATT */
	data_to_mem(0x00,fifo_tx+5)	/*                Rch : 0db ATT */
	data_to_mem(0xc6,fifo_tx+6)	/* Input <- Line, Lch : gain 9dB */
	data_to_mem(0xf6,fifo_tx+7)	/*                Rch : gain 9dB */
	I0=^fifo_tx; M0=1; L0=8;
	I1=^fifo_rx; M1=1; L1=8;
	I2=^fifo_tx; M2=1; L2=2;		/* Data Buffer for Tx (L) */
	I3=^fifo_rx; M3=1; L3=2;		/*                 Rx (L) */
	data_to_reg(0x8607,Sport0_Ctrl_Reg)	/* CODEC --> Running Mode */
	data_to_reg(dc,codec_ctrl)		/* Change to Data Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
	rts;

.VAR/DM/RAM/ABS=0x0800		interface;
.VAR/DM/RAM/ABS=0x2000/CIRC	data_fifo[4096];

start:
	call codec_initialize;		/* DSP_HEAD.H */
	I4=^data_fifo; M4=1; L4=4096;
	data_to_mem(1024,interface)
	I5=^data_fifo; M5=1; L5=0;
main:
	codec_to_AR_reg_mono;		/* Get A/D Data : DSP_HEAD.H */

	DM(I4,M4)=AR;
	DM(interface+1)=AR;

	AX0=DM(interface);
	AY0=I4;
	AR=AX0+AY0;
	AY0=0x2fff;
	AR=AR and AY0;
	I5=AR;
	SR0=DM(I5,M5);
	DM(interface+2)=SR0;

	SR0_reg_to_codec_mono;		/* Put to D/A : DSP_HEAD.H */
	waiting_for_interrupt
	jump main;

.ENDMOD;

=============================================================================


.MODULE/RAM/BOOT=0/ABS=0 test;

{ SYSTEM I/O ADDRESS DEFINES }
.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

.CONST comcrdy	= 0x0803;
.CONST comdrdy	= 0x0804;
.CONST cpudt 	= 0x0810;
.CONST dspdt	= 0x0811;

{ BOAD REGISTERS }
.CONST hs_readonly	= 0x0600;
.CONST handshake	= 0x0700;
.CONST dipswitch	= 0x0400;
.CONST codec_ctrl	= 0x0400;

{ CONTROLE BITS }
.CONST d2c	= 0x2000;
.CONST c2d	= 0x1000;
.CONST s8	= 0x8000;
.CONST s4	= 0x4000;
.CONST s2	= 0x2000;
.CONST s1	= 0x1000;
.CONST dc	= 0x1000;
.CONST c2denb	= 0x2000;
.CONST extenb	= 0x4000;

{ MEMORY STRUCTURE }
.CONST	dm_top		= 0x0800;
.CONST	dm_bottom	= 0x3BFF;
.CONST	pm_top		= 0x0000;
.CONST	pm_bottom	= 0x3FFF;

.CONST	ext_dm_top	= 0x0800;
.CONST	ext_dm_bottom	= 0x37FF;
.CONST	int_dm_top	= 0x3800;
.CONST	int_dm_bottom	= 0x3BFF;
.CONST	ext_pm_top	= 0x0800;
.CONST	ext_pm_bottom	= 0x3FFF;
.CONST	int_pm_top	= 0x0000;
.CONST	int_pm_bottom	= 0x07FF;

{ TERMINATE PROGRAM }
.CONST	end_of_prog	= 0x001D;

/******************** Special Macro Defines ********************/
#define data_to_mem(data,memory)	AX0=data; DM(memory)=AX0;
#define data_to_reg(data,register)	AX0=data; DM(register)=AX0;
#define interrupt_disable		IFC=0x3f; IMASK=0x00;
#define interrupt_s0_rx_enable		IFC=0x3f; IMASK=0x08;
#define interrupt_s0_tx_enable		IFC=0x3f; IMASK=0x10;
#define waiting_for_interrupt		idle;
#define direct_tx_port_0(i_reg,m_reg)	AX0=DM(i_reg,m_reg); TX0=AX0;
#define serial_port_0_disable		data_to_reg(0x0408,Sys_Ctrl_Reg)
#define serial_port_0_enable		data_to_reg(0x1408,Sys_Ctrl_Reg)
#define autobuf_port_0_disable		data_to_reg(0x0000,Sport0_Autobuf_Ctrl)
#define autobuf_port_0_enable		data_to_reg(0x0017,Sport0_Autobuf_Ctrl)
.MACRO codec_to_AR_reg_mono;
	SR0=DM(I3,M3); AY0=DM(I3,M3);
	SR=lshift SR0 by 8 (lo); AR=SR0 or AY0;
.ENDMACRO;
.MACRO SR0_reg_to_codec_mono;
	AY0=0xff; AR=SR0 and AY0; SR=lshift SR0 by -8 (lo);
	DM(I2,M2)=SR0; DM(I2,M2)=AR;
.ENDMACRO;

/******************** Buffer Memory Defines ********************/
.VAR/DM/RAM/ABS=0x0900/CIRC	fifo_tx[8];
.VAR/DM/RAM/ABS=0x0910/CIRC	fifo_rx[8];

/******************** Reset Vectors Defines ********************/
jump start;	nop; nop; nop;	{Reset Vector}
	RTI;	nop; nop; nop;	{irq2}
	RTI;	nop; nop; nop;	{sport0 TX}
	RTI;	nop; nop; nop;	{sport0 RX}
	RTI;	nop; nop; nop;	{irq0}
	RTI;	nop; nop; nop;	{irq1}
	RTI;	nop; nop; nop;	{timer}

/******************** Common Subroutines ********************/
codec_initialize:
	call codec_system_init;
	call codec_init_command_mode;
	call codec_init_data_mode;
	rts;
codec_system_init:
	data_to_reg(0x01,Dm_Wait_Reg)		/* 1 wait */
	data_to_reg(0xff,Sport0_Rfsdiv)		/* Multi-ch, 256bit */
	data_to_reg(0x00,Sport0_Sclkdiv)	/* divide rate */
	data_to_reg(0xff,Sport0_Tx_Words0)	/* LSB 16bit Tx */
	data_to_reg(0xff,Sport0_Rx_Words0)	/*           Rx */
	data_to_reg(0x00,Sport0_Tx_Words1)	/* MSB 16bit Tx */
	data_to_reg(0x00,Sport0_Rx_Words1)	/*           Rx */
	rts;
codec_init_command_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x08,fifo_tx+0)	/* olb=1, clb=0(control) */
	data_to_mem(0x1c,fifo_tx+1)	/* 32KHz, Stereo, 16bit */
	data_to_mem(0x9a,fifo_tx+2)	/* 256bit, x1, gen.clk */
	data_to_mem(0x00,fifo_tx+3)	/* (reserved) */
	data_to_mem(0xc0,fifo_tx+4)	/* (reserved) */
	data_to_mem(0x00,fifo_tx+6)	/* (reserved) */
	I0=^fifo_tx; M0=1; L0=8; 
	I1=^fifo_rx; M1=1; L1=8; 
	data_to_reg(0xc707,Sport0_Ctrl_Reg)	/* CODEC --> Setting Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
loop_1:
	waiting_for_interrupt
	call check_1;
	if ne jump loop_1;
	interrupt_s0_tx_enable
	data_to_mem(0x000C,fifo_tx+0)		/* clb set */
	waiting_for_interrupt
	waiting_for_interrupt
	waiting_for_interrupt
	rts;
check_1: 
	AX0=DM(fifo_rx+0); AY0=0x04; AR=AX0 and AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+1); AY0=DM(fifo_rx+1); AR=AX0-AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+2); AY0=DM(fifo_rx+2); AF=AX0 xor AY0;
	AX0=0xFF7F; AR=AX0 and AF;
chk_exit:
	rts;
codec_init_data_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x00,fifo_tx+0)	/* (dummy) */
	data_to_mem(0x00,fifo_tx+1)	/*    |    */
	data_to_mem(0x00,fifo_tx+2)	/*    |    */
	data_to_mem(0x00,fifo_tx+3)	/* (dummy) */
	data_to_mem(0xc0,fifo_tx+4)	/* Output Enable, Lch : 0db ATT */
	data_to_mem(0x00,fifo_tx+5)	/*                Rch : 0db ATT */
	data_to_mem(0xc6,fifo_tx+6)	/* Input <- Line, Lch : gain 9dB */
	data_to_mem(0xf6,fifo_tx+7)	/*                Rch : gain 9dB */
	I0=^fifo_tx; M0=1; L0=8;
	I1=^fifo_rx; M1=1; L1=8;
	I2=^fifo_tx; M2=1; L2=2;		/* Data Buffer for Tx (L) */
	I3=^fifo_rx; M3=1; L3=2;		/*                 Rx (L) */
	data_to_reg(0x8607,Sport0_Ctrl_Reg)	/* CODEC --> Running Mode */
	data_to_reg(dc,codec_ctrl)		/* Change to Data Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
	rts;

.VAR/DM/RAM/ABS=0x0800		interface;
.VAR/DM/RAM/ABS=0x2000/CIRC	data_fifo[4096];

start:
	call codec_initialize;
	I4=^data_fifo; M4=1; L4=4096;
	data_to_mem(1024,interface)
	I5=^data_fifo; M5=1; L5=0;
main:
	call codec_to_Y_reg_mono;
	DM(I4,M4)=AY0;		/* Left  */

	AX0=DM(interface);
	AY0=I4;
	AR=AX0+AY0;
	AY0=0x2fff;
	AR=AR and AY0;
	I5=AR;
	AY0=DM(I5,M5);

	call Y_reg_to_codec_mono;
	waiting_for_interrupt
	jump main;

codec_to_Y_reg_mono:
	SR0=DM(I3,M3);			/* Left  MSB 8bit */
	AY0=DM(I3,M3);			/*       LSB 8bit */
	SR=lshift SR0 by 8 (lo);
	AR=SR0 or AY0;
	AY0=AR;
	rts;

Y_reg_to_codec_mono:
	AX0=0xff;
	SR0=AY0;
	SR=lshift SR0 by -8 (lo);
	AR=AX0 and AY0;
	DM(I2,M2)=SR0;			/* Left  MSB 8bit */
	DM(I2,M2)=AR;			/*       LSB 8bit */
	rts;

.ENDMOD;

=============================================================================


.MODULE/RAM/BOOT=0/ABS=0 test;

{ SYSTEM I/O ADDRESS DEFINES }
.const  Sys_Ctrl_Reg= 		    0x3fff;
.const  Dm_Wait_Reg= 	            0x3ffe;
.const  Tperiod_Reg=                0x3ffd;
.const  Tcount_Reg=                 0x3ffc;
.const  Tscale_Reg=                 0x3ffb;
.const  Sport0_Rx_Words1=           0x3ffa;
.const  Sport0_Rx_Words0=           0x3ff9;
.const  Sport0_Tx_Words1=           0x3ff8;
.const  Sport0_Tx_Words0=           0x3ff7;
.const  Sport0_Ctrl_Reg=            0x3ff6;
.const  Sport0_Sclkdiv=             0x3ff5;
.const  Sport0_Rfsdiv=              0x3ff4;
.const  Sport0_Autobuf_Ctrl=        0x3ff3;
.const  Sport1_Ctrl_Reg=            0x3ff2;
.const  Sport1_Sclkdiv=             0x3ff1;
.const  Sport1_Rfsdiv=              0x3ff0;
.const  Sport1_Autobuf_Ctrl=        0x3fef;

.CONST comcrdy	= 0x0803;
.CONST comdrdy	= 0x0804;
.CONST cpudt 	= 0x0810;
.CONST dspdt	= 0x0811;

{ BOAD REGISTERS }
.CONST hs_readonly	= 0x0600;
.CONST handshake	= 0x0700;
.CONST dipswitch	= 0x0400;
.CONST codec_ctrl	= 0x0400;

{ CONTROLE BITS }
.CONST d2c	= 0x2000;
.CONST c2d	= 0x1000;
.CONST s8	= 0x8000;
.CONST s4	= 0x4000;
.CONST s2	= 0x2000;
.CONST s1	= 0x1000;
.CONST dc	= 0x1000;
.CONST c2denb	= 0x2000;
.CONST extenb	= 0x4000;

{ MEMORY STRUCTURE }
.CONST	dm_top		= 0x0800;
.CONST	dm_bottom	= 0x3BFF;
.CONST	pm_top		= 0x0000;
.CONST	pm_bottom	= 0x3FFF;

.CONST	ext_dm_top	= 0x0800;
.CONST	ext_dm_bottom	= 0x37FF;
.CONST	int_dm_top	= 0x3800;
.CONST	int_dm_bottom	= 0x3BFF;
.CONST	ext_pm_top	= 0x0800;
.CONST	ext_pm_bottom	= 0x3FFF;
.CONST	int_pm_top	= 0x0000;
.CONST	int_pm_bottom	= 0x07FF;

{ TERMINATE PROGRAM }
.CONST	end_of_prog	= 0x001D;

/******************** Special Macro Defines ********************/
#define data_to_mem(data,memory)	AX0=data; DM(memory)=AX0;
#define data_to_reg(data,register)	AX0=data; DM(register)=AX0;
#define interrupt_disable		IFC=0x3f; IMASK=0x00;
#define interrupt_s0_rx_enable		IFC=0x3f; IMASK=0x08;
#define interrupt_s0_tx_enable		IFC=0x3f; IMASK=0x10;
#define waiting_for_interrupt		idle;
#define direct_tx_port_0(i_reg,m_reg)	AX0=DM(i_reg,m_reg); TX0=AX0;
#define serial_port_0_disable		data_to_reg(0x0408,Sys_Ctrl_Reg)
#define serial_port_0_enable		data_to_reg(0x1408,Sys_Ctrl_Reg)
#define autobuf_port_0_disable		data_to_reg(0x0000,Sport0_Autobuf_Ctrl)
#define autobuf_port_0_enable		data_to_reg(0x0017,Sport0_Autobuf_Ctrl)
.MACRO codec_to_AR_reg_mono;
	SR0=DM(I3,M3); AY0=DM(I3,M3);
	SR=lshift SR0 by 8 (lo); AR=SR0 or AY0;
.ENDMACRO;
.MACRO SR0_reg_to_codec_mono;
	AY0=0xff; AR=SR0 and AY0; SR=lshift SR0 by -8 (lo);
	DM(I2,M2)=SR0; DM(I2,M2)=AR;
.ENDMACRO;

/******************** Buffer Memory Defines ********************/
.VAR/DM/RAM/ABS=0x0900/CIRC	fifo_tx[8];
.VAR/DM/RAM/ABS=0x0910/CIRC	fifo_rx[8];

/******************** Reset Vectors Defines ********************/
jump start;	nop; nop; nop;	{Reset Vector}
	RTI;	nop; nop; nop;	{irq2}
	RTI;	nop; nop; nop;	{sport0 TX}
	RTI;	nop; nop; nop;	{sport0 RX}
	RTI;	nop; nop; nop;	{irq0}
	RTI;	nop; nop; nop;	{irq1}
	RTI;	nop; nop; nop;	{timer}

/******************** Common Subroutines ********************/
codec_initialize:
	call codec_system_init;
	call codec_init_command_mode;
	call codec_init_data_mode;
	rts;
codec_system_init:
	data_to_reg(0x01,Dm_Wait_Reg)		/* 1 wait */
	data_to_reg(0xff,Sport0_Rfsdiv)		/* Multi-ch, 256bit */
	data_to_reg(0x00,Sport0_Sclkdiv)	/* divide rate */
	data_to_reg(0xff,Sport0_Tx_Words0)	/* LSB 16bit Tx */
	data_to_reg(0xff,Sport0_Rx_Words0)	/*           Rx */
	data_to_reg(0x00,Sport0_Tx_Words1)	/* MSB 16bit Tx */
	data_to_reg(0x00,Sport0_Rx_Words1)	/*           Rx */
	rts;
codec_init_command_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x08,fifo_tx+0)	/* olb=1, clb=0(control) */
	data_to_mem(0x1c,fifo_tx+1)	/* 32KHz, Stereo, 16bit */
	data_to_mem(0x9a,fifo_tx+2)	/* 256bit, x1, gen.clk */
	data_to_mem(0x00,fifo_tx+3)	/* (reserved) */
	data_to_mem(0xc0,fifo_tx+4)	/* (reserved) */
	data_to_mem(0x00,fifo_tx+6)	/* (reserved) */
	I0=^fifo_tx; M0=1; L0=8; 
	I1=^fifo_rx; M1=1; L1=8; 
	data_to_reg(0xc707,Sport0_Ctrl_Reg)	/* CODEC --> Setting Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
loop_1:
	waiting_for_interrupt
	call check_1;
	if ne jump loop_1;
	interrupt_s0_tx_enable
	data_to_mem(0x000C,fifo_tx+0)		/* clb set */
	waiting_for_interrupt
	waiting_for_interrupt
	waiting_for_interrupt
	rts;
check_1: 
	AX0=DM(fifo_rx+0); AY0=0x04; AR=AX0 and AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+1); AY0=DM(fifo_rx+1); AR=AX0-AY0;
	if ne jump chk_exit;
	AX0=DM(fifo_tx+2); AY0=DM(fifo_rx+2); AF=AX0 xor AY0;
	AX0=0xFF7F; AR=AX0 and AF;
chk_exit:
	rts;
codec_init_data_mode:
	interrupt_disable
	serial_port_0_disable
	autobuf_port_0_disable
	data_to_mem(0x00,fifo_tx+0)	/* (dummy) */
	data_to_mem(0x00,fifo_tx+1)	/*    |    */
	data_to_mem(0x00,fifo_tx+2)	/*    |    */
	data_to_mem(0x00,fifo_tx+3)	/* (dummy) */
	data_to_mem(0xc0,fifo_tx+4)	/* Output Enable, Lch : 0db ATT */
	data_to_mem(0x00,fifo_tx+5)	/*                Rch : 0db ATT */
	data_to_mem(0xc6,fifo_tx+6)	/* Input <- Line, Lch : gain 9dB */
	data_to_mem(0xf6,fifo_tx+7)	/*                Rch : gain 9dB */
	I0=^fifo_tx; M0=1; L0=8;
	I1=^fifo_rx; M1=1; L1=8;
	I2=^fifo_tx; M2=1; L2=2;		/* Data Buffer for Tx (L) */
	I3=^fifo_rx; M3=1; L3=2;		/*                 Rx (L) */
	data_to_reg(0x8607,Sport0_Ctrl_Reg)	/* CODEC --> Running Mode */
	data_to_reg(dc,codec_ctrl)		/* Change to Data Mode */
	direct_tx_port_0(I0,M0)
	autobuf_port_0_enable			/* tx=I0, rx=I1 */
	serial_port_0_enable
	interrupt_s0_rx_enable
	rts;

.VAR/DM/RAM/ABS=0x3820		buffer[16];
.VAR/DM/RAM/ABS=0x0800		interface[16];
.VAR/DM/RAM/ABS=0x1000/CIRC	data_fifo_l[2048];
.VAR/DM/RAM/ABS=0x2000/CIRC	data_fifo_r[2048];

start:
	call codec_initialize;
	I4=^data_fifo_l; M4=1; L4=2048; 
	I5=^data_fifo_r; M5=1; L5=2048; 
	data_to_mem(1024,interface+0)
	data_to_mem(1024,interface+1)
	I6=^data_fifo_l+1024; M6=1; L6=0; 
	I7=^data_fifo_r+1024; M7=1; L7=0; 
main:
	call codec_to_Y_reg;
	DM(I4,M4)=AY0;		/* Left  */
	DM(I5,M5)=AY1;		/* Right */
	SR0=AY0;
	SR=ashift SR0 by -1 (lo);
	DM(buffer+0)=SR0;
	SR0=AY1;
	SR=ashift SR0 by -1 (lo);
	DM(buffer+1)=SR0;

jump dummy;

	AX0=DM(interface+0);
	AY0=I4;
	AR=AX0+AY0;
	AY0=0x17ff;
	AR=AR and AY0;
	I6=AR;
	SR0=DM(I6,M6);
	SR=ashift SR0 by -1 (lo);
	AX0=SR0;
	AX1=DM(interface+1);
	AY1=I5;
	AR=AX1+AY1;
	AY1=0x17ff;
	AR=AR and AY1;
	I7=AR;
	SR0=DM(I7,M7);
	SR=ashift SR0 by -1 (lo);
	AX1=SR0;

	AY0=DM(buffer+0);
	AR=AX0+AY0;
	AY0=AR;
	AY1=DM(buffer+1);
	AR=AX1+AY1;
	AY1=AR;

dummy:

	call Y_reg_to_codec;
	waiting_for_interrupt
	jump main;

codec_to_Y_reg:
	SR0=DM(I3,M3);			/* Left  MSB 8bit */
	AY0=DM(I3,M3);			/*       LSB 8bit */
	SR=lshift SR0 by 8 (lo);
	AR=SR0 or AY0;
	AY0=AR;
	SR0=DM(I3,M3);			/* Right MSB 8bit */
	AY1=DM(I3,M3);			/*       LSB 8bit */
	SR=lshift SR0 by 8 (lo);
	AR=SR0 or AY1;
	AY1=AR;
	rts;

Y_reg_to_codec:
	AX0=0xff;
	SR0=AY0;
	SR=lshift SR0 by -8 (lo);
	AR=AX0 and AY0;
	DM(I2,M2)=SR0;			/* Left  MSB 8bit */
	DM(I2,M2)=AR;			/*       LSB 8bit */
	SR0=AY1;
	SR=lshift SR0 by -8 (lo);
	AR=AX0 and AY1;
	DM(I2,M2)=SR0;			/* Right MSB 8bit */
	DM(I2,M2)=AR;			/*       LSB 8bit */
	rts;

.ENDMOD;

IndyのC その1 世界一高価なMIDIスルーボックス

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

/*** (^_^;) Bug Recovery of SGI Media Library --> Original MIDI Defines !! (^_^;) ***/
#define MES(x)			( (x[0]).mm.msgbuf )
#define midi_get_status(x)	( ( ( ( MES(x) & 0xff000000 ) == 0x60000000 ) || \
				( ( MES(x) & 0xff000000 ) == 0x40000000 ) ) ? \
				( ( MES(x) & 0x0f00000 ) >> 16 ) : 0 )	/* Serial Port #2 Only ! */
#define midi_get_channel(x)	( ( MES(x) & 0x00f0000 ) >> 16 )	/* 1 - 16 channel Only ! */
#define midi_get_keyno(x)	( ( MES(x) & 0x0007f00 ) >> 8 )		/* device ID : #1 Only ! */
#define midi_get_velocity(x)	( ( MES(x) & 0x000007f ) )
#define midi_set_status(x,d)	( MES(x) = ( ( d > 0xbf ) && ( d < 0xe0 ) ) ? 0x40000000 : 0x60000000, \
				  MES(x) &= 0xff0fffff, MES(x) |= ( ( d & 0xf0 ) << 16 ) )
#define midi_set_channel(x,d)	( MES(x) &= 0xfff0ffff, MES(x) |= ( ( d & 0x0f ) << 16 ) )
#define midi_set_keyno(x,d)	( MES(x) &= 0xffff00ff, MES(x) |= ( ( d & 0x7f ) << 8 ) )
#define midi_set_velocity(x,d)	( MES(x) &= 0xffffff00, MES(x) |= ( d & 0x7f ) )

static unsigned char status, channel, keyno, velocity;
int retval;

MIport *midi_port;
MIevent midi[10000];

void midi_initialize();
int midi_receive();
int midi_transmit();

void main(unsigned int argc, char** argv)
{
	int count = 0;
	midi_initialize();
	printf("\nLet's start MIDI! (^_^)/\n\n");
	schedctl(NDPRI, 0, NDPNORMMAX);
	while(1){
		retval = midi_receive();
		if( retval > 0 ){
			if( (status==0x90)&&(velocity!=0) ) count++;
			if( (status==0x90)&&(velocity==0) ) count--;
			fprintf(stderr," %d ", count );
			retval = midi_transmit();
			if( retval < 0 ) exit(-1);
		}
	}
	MIclose( midi_port );
	MIfreeport( midi_port );
}

void midi_initialize()
{
	MIconfig *c;
	c = MInewconfig();
	midi_port = MInewport();
	retval = MIopen( midi_port, "rw", &c );
	if( retval < 0 ) exit(-1);
}

int midi_receive()
{
	retval = MIreceive( midi_port, midi, 1 );
	if( retval > 0 ){
		status = midi_get_status( midi );
		if( ( status > 0xef ) || ( status < 0x80 ) ) return(0);
		channel = midi_get_channel( midi );
		keyno = midi_get_keyno( midi );
		if( ( status > 0xbf ) && ( status < 0xe0 ) ) return(2);
		velocity = midi_get_velocity( midi );
		return(3);
	}
	return(retval);
}

int midi_transmit()
{
	midi_set_status( midi, status );
	midi_set_channel( midi, channel );
	midi_set_keyno( midi, keyno );
	midi_set_velocity ( midi, velocity );
	return( MIsend( midi_port, midi, 1 ) );
}

IndyのC その2 世界一高価なオーディオスルー

#include 
#include 
#include 
#include 
#include 
#include 
#include 

int	audio_pid;

void	owari();
void	do_audio();

main(int argc, char **argv)
{
	Widget      toplevel, button;

	audio_pid = sproc( do_audio, PR_SALL );
	if (audio_pid == -1) exit(-1);

	toplevel = XtInitialize( argv[0], "", NULL, 0, &argc, argv );
	button = XtCreateManagedWidget( "  Stop Audio  ", xmPushButtonWidgetClass, toplevel, NULL, 0 );
	XtAddCallback( button, XmNactivateCallback, owari, NULL );
	XtRealizeWidget(toplevel);
	XtMainLoop();
}

void do_audio()
{
	ALconfig	config;
	ALport		input_port, output_port;
	short		*buff[48000];
	int		i;

	config = ALnewconfig();
	ALsetqueuesize( config, 48000 );
	input_port = ALopenport( "input", "r", config );
	output_port = ALopenport( "output", "w", config );
	if (input_port == NULL || output_port == NULL) exit(-1);

	while (1)
	{
		ALreadsamps( input_port, buff, 256 );
		for(i=0;i<256;i++)
			ALwritesamps( output_port, buff, 1 );
	}
}

void owari(Widget w, caddr_t d1, caddr_t d2)
{
	kill(audio_pid, SIGKILL);
	exit(0); 
}

IndyのC その3 サウンドファイル多重再生用プレーヤ

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

static char	*filename, *filenametail, *sampbuf;
static int	fd, bytes_per_samp, samps_per_frame, frames_per_sec;
static int	bytes_per_buf, samps_per_buf, frames_per_buf;
static long	filefmt, bits_per_samp, compression;
static double	file_rate, secs_per_frame, secs_per_buf;

int InitAudio();
int PlaySamples();
void GetPathTail();

void main(int argc, char **argv)
{
	AFfilehandle audio_file;
	ALport audio_port;
	extern int   optind;
	filename = argv[optind];
	GetPathTail(filename, &filenametail);
	fd = open(filename, O_RDONLY);
	AFidentifyfd(fd);
	audio_file = AFopenfd(fd,"r",AF_NULL_FILESETUP);
	InitAudio(audio_file, &audio_port);
	PlaySamples(audio_file, audio_port);
	AFclosefile(audio_file);
	ALcloseport(audio_port);
	free(sampbuf);
}

static int PlaySamples(AFfilehandle audio_file, ALport audio_port)
{
	int num_bufs, leftover_bytes, leftover_samps, leftover_frames, i;
	int bytes_read, samples_read, frames_read, total_frames, total_samps, total_samp_bytes;
	float file_playingtime, fileplayingtime;
	char compressionname[10];
	int samp_count = 0, frame_count = 0;
	double sec_count = 0.0;
    
	total_frames = AFgetframecnt(audio_file, AF_DEFAULT_TRACK);
	total_samps      =  total_frames * samps_per_frame;
	total_samp_bytes =  total_samps * bytes_per_samp;
	num_bufs         = total_samp_bytes / bytes_per_buf;
	leftover_bytes   = total_samp_bytes % bytes_per_buf;
	leftover_samps   = leftover_bytes / bytes_per_samp;
	leftover_frames  = leftover_samps / samps_per_frame;

	GetPathTail(filename, &filenametail);
	schedctl(NDPRI, 0, NDPNORMMAX);
	AFseekframe(audio_file, AF_DEFAULT_TRACK, 0);
    
	for (i=0; num_bufs<0 || i0) printf("\n%d sample frames  %6.3f secs\n", frame_count, sec_count); 
	frames_read = AFreadframes(audio_file, AF_DEFAULT_TRACK, sampbuf, leftover_frames);
	samples_read = frames_read * samps_per_frame;
	ALwritesamps(audio_port, sampbuf, samples_read);
	while(ALgetfilled(audio_port) > 0) sginap(1);
	sginap(10);
}

static int InitAudio(AFfilehandle audio_file, ALport *audio_port)
{
	ALconfig audio_port_config;
	long pvbuf[4], audio_rate, output_rate, samp_type, samp_wordsize, vers;
	samps_per_frame   = AFgetchannels(audio_file, AF_DEFAULT_TRACK);
	file_rate         = AFgetrate(audio_file, AF_DEFAULT_TRACK);
	compression       = AFgetcompression(audio_file, AF_DEFAULT_TRACK);
	filefmt           = AFgetfilefmt(audio_file, &vers);
	AFgetsampfmt(audio_file, AF_DEFAULT_TRACK, &samp_type, &bits_per_samp);
	pvbuf[0] = AL_OUTPUT_COUNT; pvbuf[2] = AL_MONITOR_CTL;
	ALgetparams(AL_DEFAULT_DEVICE, pvbuf, 4);
	audio_rate = frames_per_sec = (long) file_rate;
	output_rate = GetOutputRate();
	if (bits_per_samp <= 8) { bytes_per_samp = 1; samp_wordsize  = AL_SAMPLE_8; }
	else if (bits_per_samp <= 16) { bytes_per_samp = 2; samp_wordsize  = AL_SAMPLE_16; }
	else if (bits_per_samp <= 24) { bytes_per_samp = 4; samp_wordsize  = AL_SAMPLE_24; }
	secs_per_frame = 1.0 / ((double)frames_per_sec);
	frames_per_buf = (frames_per_sec+1)/2;
	samps_per_buf = frames_per_buf * samps_per_frame;
	bytes_per_buf = samps_per_buf * bytes_per_samp;
	secs_per_buf  = secs_per_frame * frames_per_buf;
	sampbuf = malloc(bytes_per_buf);
	audio_port_config = ALnewconfig();
	ALsetwidth(audio_port_config, samp_wordsize);
	ALsetchannels(audio_port_config, samps_per_frame);
	ALsetqueuesize(audio_port_config, samps_per_buf*2);
	*audio_port = ALopenport("player", "w", audio_port_config);
}

int GetInputRate()
{
	long buf[6];
	buf[0] = AL_INPUT_RATE; buf[2] = AL_INPUT_SOURCE; buf[4] = AL_DIGITAL_INPUT_RATE;
	ALgetparams(AL_DEFAULT_DEVICE, buf, 6);
	if ((buf[1] == AL_RATE_AES_1)||(buf[3] == AL_INPUT_DIGITAL)){
		if (ALgetdefault(AL_DEFAULT_DEVICE, AL_DIGITAL_INPUT_RATE) >= 0) return (buf[5]);
	}
	else if (buf[1] > 0) return (buf[1]);
	return (AL_RATE_UNDEFINED);
} 

int GetOutputRate()
{
	long buf[4];
	buf[0] = AL_OUTPUT_RATE; buf[2] = AL_DIGITAL_INPUT_RATE;
	ALgetparams(AL_DEFAULT_DEVICE, buf, 4);
	if (buf[1] > 0) return (buf[1]);
	else if (buf[1] == AL_RATE_AES_1){
		if (ALgetdefault(AL_DEFAULT_DEVICE,AL_DIGITAL_INPUT_RATE) >= 0) return (buf[3]);
	}
	else if (buf[1] == AL_RATE_INPUTRATE) return (GetInputRate());
	return (AL_RATE_UNDEFINED);
}

static void GetPathTail(char *thepath, char **thetail)
{
    char *p;
    p = strrchr(thepath, '/');
    if (p) p++;
    else p = thepath;
    *thetail = (char *)malloc(strlen(p) + 1);
    strcpy(*thetail, p);
}

IndyのC その4 "MUROMACHI"エミュレータ

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define XK_MISCELLANY		/* Needed for defininition of Esc */
#include 
#include 
#include 
#include 

#define XSCREENSIZE(d,s)	DisplayWidth(d,s)
#define YSCREENSIZE(d,s)	DisplayHeight(d,s)
#define WORLD_XMIN		0.0
#define WORLD_XMAX		200.0
#define WORLD_YMIN		0.0
#define WORLD_YMAX		200.0
#define	GREY1			8
#define GREY2			15
#define MOUSE_OBJ 		1
#define LEN			700.0

/*** (^_^;) Bug Recovery of SGI Media Library --> Original MIDI Defines !! (^_^;) ***/
#define MES(x)			( (x[0]).mm.msgbuf )
#define midi_set_status(x,d)	( MES(x) = ( ( d > 0xbf ) && ( d < 0xe0 ) ) ? 0x40000000 : 0x60000000, \
				  MES(x) &= 0xff0fffff, MES(x) |= ( ( d & 0xf0 ) << 16 ) )
#define midi_set_channel(x,d)	( MES(x) &= 0xfff0ffff, MES(x) |= ( ( d & 0x0f ) << 16 ) )
#define midi_set_keyno(x,d)	( MES(x) &= 0xffff00ff, MES(x) |= ( ( d & 0x7f ) << 8 ) )
#define midi_set_velocity(x,d)	( MES(x) &= 0xffffff00, MES(x) |= ( d & 0x7f ) )

char *progname = "midi";
long xmin, xmax, ymin, ymax;
unsigned int screen_w, screen_h;
float xrat, yrat;
int screen_num, old_x, old_y, old_button, color_no, action_mode;

Display *display;
MIport *midi_port;
MIevent midi[100];

Window glx_create_window();
void set_wm_hints();
void define_geom();
void draw_screen();
void midi_initialize();
void midi_event_generate();
void midi_transmit();
void midi_tx_3byte();

void main(int argc, char **argv)
{
	Window top, glwin, root, child, windows[2];
	XEvent event;
	char buffer[5];
	int got_expose;
	int x, y, root_x, root_y;
	unsigned int keys_buttons;
	KeySym keysym;
	XComposeStatus compose;

	srandom(1);
	color_no = action_mode = 0;
	midi_initialize();
	midi_tx_3byte( 159, 1, 127 );	/* to Draw */
	midi_tx_3byte( 159, 1, 0 );

	display = XOpenDisplay(NULL);
	screen_num = DefaultScreen(display);
	screen_w = XSCREENSIZE(display, screen_num);
	screen_h = YSCREENSIZE(display, screen_num);
	xmin = ymin = 0;
	xmax = screen_w - 1;
	ymax = screen_h - 1;
	xrat = (WORLD_XMAX - WORLD_XMIN) / (float)(xmax - xmin);
	yrat = (WORLD_YMAX - WORLD_YMIN) / (float)(ymax - ymin);
	top = XCreateSimpleWindow(display,RootWindow(display,screen_num),0,0,screen_w, screen_h,0,0,0);
	set_wm_hints(argc, argv, top);
	glwin = glx_create_window(display, top, 0, 0, screen_w, screen_h, 0);
	XSelectInput(display,top,KeyPressMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
	XSelectInput(display, glwin, ExposureMask);
	windows[0] = glwin;
	windows[1] = top;
	XSetWMColormapWindows(display, top, windows, 2);
	XMapWindow(display, glwin);
	XMapWindow(display, top);
	GLXwinset(display, glwin);
	define_geom();
	XGrabButton(display, AnyButton, AnyModifier, RootWindow(display, screen_num), False,
		ButtonPressMask | ButtonReleaseMask | PointerMotionMask, GrabModeAsync, GrabModeAsync,
		None, None);
	got_expose = FALSE;
	while (1) {
		gflush();
		XNextEvent(display, &event);
		switch (event.type) {
			case Expose:
				if (event.xexpose.count) continue; 
				got_expose = TRUE;
				break;
			case MotionNotify:
			case ButtonPress:
			case ButtonRelease:
				break;
			case KeyPress:
				XLookupString(&event, buffer, sizeof(buffer), &keysym, &compose);
				if (keysym == XK_Escape) {
					XCloseDisplay(display);
					MIclose( midi_port );
					MIfreeport( midi_port );
					exit(0);
				}
				continue;
			default:
				continue;
		}
		if (!XEventsQueued(display, QueuedAlready) && got_expose) {
			XQueryPointer(display,glwin,&root,&child,&root_x,&root_y,&x,&y,&keys_buttons);
			midi_event_generate(x, y, keys_buttons);
			draw_screen(x, y, keys_buttons);
		}
	}
}

static Window glx_create_window(Display* dpy, Window parent, int x, int y,
			 	int w, int h, int borderWidth)
{
	static GLXconfig params[] = {{GLX_NORMAL,GLX_RGB,FALSE},{GLX_NORMAL,GLX_DOUBLE,TRUE},{0,0,0},};
	GLXconfig *next, *retconfig;
	Colormap cmap = DefaultColormap(dpy, DefaultScreen(dpy));
	XVisualInfo *vis;
	XVisualInfo template;
	XSetWindowAttributes cwa;
	int nret;
	Window win;

	retconfig = GLXgetconfig(dpy, DefaultScreen(dpy), params);
	for (next = retconfig; next->buffer; next++) {
		if (next->buffer == GLX_NORMAL) {
			if (next->mode == GLX_COLORMAP) {
				cmap = next->arg;
			}
			else if (next->mode == GLX_VISUAL) {
				template.visualid = next->arg;
				template.screen = DefaultScreen(dpy);
				vis = XGetVisualInfo(dpy,VisualScreenMask | VisualIDMask,&template,&nret);
			}
		}
	}
	cwa.colormap = cmap;
	cwa.border_pixel = 0;
	win = XCreateWindow(dpy, parent, x, y, w, h,
			borderWidth, vis->depth, InputOutput, vis->visual,
			CWColormap|CWBorderPixel, &cwa);
	for (next = retconfig; next->buffer; next++) {
		if ((next->buffer == GLX_NORMAL) && (next->mode == GLX_WINDOW)) {
			next->arg = win;
			break;
		}
	}
	GLXlink(dpy, retconfig);
	return win;
}

static void set_wm_hints(int argc, char *argv[], Window top)
{
	XTextProperty windowName;
	XSizeHints size_hints;
	XClassHint class_hints;
	XWMHints wm_hints;
	MotifWmHints mwm_hints;
	Atom mwm_hints_atom;

	size_hints.flags = USPosition | PBaseSize | PMaxSize;
	size_hints.x = 0;
	size_hints.y = 0;
	size_hints.base_width = size_hints.max_width = screen_w;
	size_hints.base_height = size_hints.max_height = screen_h;
	XStringListToTextProperty(&progname, 1, &windowName);
	wm_hints.initial_state = NormalState;
	wm_hints.input = True;
	wm_hints.flags = StateHint | InputHint;
	class_hints.res_name = progname;
	class_hints.res_class = "test";
	XSetWMProperties(display,top,&windowName,NULL,argv,argc,&size_hints,&wm_hints,&class_hints);
	mwm_hints_atom = XInternAtom(display, _XA_MOTIF_WM_HINTS, False);
	mwm_hints.flags = MWM_HINTS_DECORATIONS | MWM_HINTS_FUNCTIONS;
	mwm_hints.decorations = 0;
	mwm_hints.functions = MWM_FUNC_CLOSE;
	XChangeProperty(display, top, mwm_hints_atom, mwm_hints_atom, 32, PropModeReplace, &mwm_hints,
			PROP_MWM_HINTS_ELEMENTS);
}

static void define_geom()
{
	Matrix C_spline = {
		{-0.5,  1.5, -1.5,  0.5},
		{ 1.0, -2.5,  2.0, -0.5},
		{-0.5,  0.0,  0.5,  0.0},
		{ 0.0,  1.0,  0.0,  0.0},
	};
	ortho2(WORLD_XMIN, WORLD_XMAX, WORLD_YMIN, WORLD_YMAX);
	defbasis(100,C_spline);
	curveprecision(20);
	curvebasis(100);
	makeobj(MOUSE_OBJ);
	color(WHITE);
	rectf(-4.0,-6.0,4.0,6.0);
	color(BLACK);
	rect(-4.0,-6.0,4.0,6.0);
	rect(-2.75,0.0,-1.25,4.5);
	rect(-0.75,0.0, 0.75,4.5);
	rect( 1.25,0.0, 2.75,4.5);
	color(GREY1);
	rectf(-3.0,-6.0,5.0,-7.0);
	rectf(4.0,-7.0,5.0,5.0);
	closeobj();
}

static void draw_screen(int x, int y, unsigned int keys_buttons)
{
	static Coord geom[4][3] = {
		{ WORLD_XMIN,        6.0-LEN, 0.0 },
		{        0.0,            6.0, 0.0 },
		{ WORLD_XMIN,     WORLD_YMAX, 0.0 },
		{        0.0, WORLD_YMAX+LEN, 0.0 },
	};
	static char disp_message[3][120] = {
		"  =====  [1] DRAW Mode ( left = draw , middle = Error , right --> Paint )  =====",
		"  =====  [2] PAINT Mode ( left = paint , middle = Effect , right --> Stamp )  =====",
		"  =====  [3] STAMP Mode ( left = stamp , middle = Erase , right --> Draw )  =====",
	};
	static int mode_color_table[6] = { 9, 10, 11, 12, 13, 14 };
	static int first_time = TRUE;
	float xoff, yoff;

	if (first_time) {
		frontbuffer(TRUE);
		backbuffer(TRUE);
	}
	color( mode_color_table[color_no] );
	clear();
	color(BLACK);
	cmov2i( (Icoord)( 20 + 10 * action_mode ), (Icoord)( 150 - 40 * action_mode ) );
	charstr( disp_message[action_mode] );
	xoff = (float)(x - xmin) * xrat;
	yoff = (float)(ymax - ymin - y) * yrat;
	geom[0][1] = yoff + 6.0 - LEN;
	geom[1][0] = xoff;
	geom[1][1] = yoff + 6.0; 
	geom[3][0] = xoff;
	crv(geom);
	pushmatrix();
	translate(xoff,yoff,0.0);
	callobj(MOUSE_OBJ);
	if (keys_buttons & Button1Mask) rectf(-2.75,0.0,-1.25,4.5);
 	if (keys_buttons & Button2Mask) rectf(-0.75,0.0, 0.75,4.5);
 	if (keys_buttons & Button3Mask) rectf( 1.25,0.0, 2.75,4.5);
	popmatrix();
	if (first_time) {
		frontbuffer(FALSE);
		first_time = FALSE;
	}
	else swapbuffers();
}

static void midi_initialize()
{
	MIconfig *c;
	c = MInewconfig();
	midi_port = MInewport();
	if( MIopen( midi_port, "rw", &c ) < 0 ) exit(-1);
}

static void midi_transmit( int status, int channel, int keyno, int velocity )
{
	midi_set_status( midi, ( status & 0xf0 ) );
	midi_set_channel( midi, ( channel & 0x0f ) );
	midi_set_keyno( midi, ( keyno & 0x7f ) );
	midi_set_velocity ( midi, ( velocity & 0x7f ) );
	if( MIsend( midi_port, midi, 1 ) < 0 ) exit(-1);
}

static void midi_tx_3byte( int status_byte, int keyno, int velocity )
{
	midi_transmit( ( status_byte & 0xf0 ), ( status_byte & 0x0f ), keyno, velocity );
}

static void midi_event_generate(int x, int y, unsigned int keys_buttons)
{
	int d, new_x, new_y, new_button = 0, point_event = 0, color_change = 0;
	new_x = 20 + (int)( (float)x * 80.9 / 1024.0 );
	new_y = 20 + (int)( (float)( 767 - y ) * 80.9 / 768.0 );
	if( keys_buttons & Button1Mask ) new_button = 1;
 	if( keys_buttons & Button2Mask ) new_button = 2;
 	if( keys_buttons & Button3Mask ) new_button = 3;
	if( old_x != new_x ){
		old_x = new_x;
		point_event++;
	}
	if( old_y != new_y ){
		old_x = new_x;
		point_event++;
	}
	if( old_button != new_button ){
		if( ( new_button == 2 ) && ( old_button == 0 ) ){
			color_change++;
			if( action_mode == 0 ){
				midi_tx_3byte( 157, 120, 127 );			/* Error */
				midi_tx_3byte( 157, 120, 0 );
			}
			else if( action_mode == 1 ){
				d = random() % 3;
				midi_tx_3byte( 159, 121 + d, 127 );		/* Effect */
				midi_tx_3byte( 159, 121 + d, 0 );
			}
			else if( action_mode == 2 ){
				d = random() % 3;
				midi_tx_3byte( 159, 65 + d, 127 );		/* Erase */
				midi_tx_3byte( 159, 65 + d, 0 );
			}
		}
		else if( ( new_button == 3 ) && ( old_button == 0 ) ){
			color_change++;
			if( action_mode == 0 ){
				midi_tx_3byte( 159, 60, 127 );			/* to Paint */
				midi_tx_3byte( 159, 60, 0 );
				action_mode = 1;
			}
			else if( action_mode == 1 ){
				d = 71 + 20 * ( random() % 2 ) + random() % 10;
				midi_tx_3byte( 159, d, 127 );			/* to Stamp */
				midi_tx_3byte( 159, d, 0 );
				action_mode = 2;
			}
			else if( action_mode == 2 ){
				d = 1 + 20 * ( random() % 2 ) + random() % 16;
				midi_tx_3byte( 159, d, 127 );			/* to Draw */
				midi_tx_3byte( 159, d, 0 );
				action_mode = 0;
			}
		}
		else if( ( new_button == 1 ) && ( old_button == 0 ) ){
			if( action_mode == 0 ){
				midi_tx_3byte( 157, new_x, new_y );
				midi_tx_3byte( 157, new_x, 0 );
			}
			else if( action_mode == 1 ){
				d = 41 + ( random() % 16 );
				midi_tx_3byte( 159, d, 127 );
				midi_tx_3byte( 159, d, 0 );
				color_change++;
			}
			else if( action_mode == 2 ){
				midi_tx_3byte( 158, new_x, new_y );
				midi_tx_3byte( 158, new_x, 0 );
			}
		}
		old_button = new_button;
	}
	else if( point_event != 0 ){
		if( ( new_button == 1 ) && ( action_mode == 0 ) ){
			midi_tx_3byte( 157, new_x, new_y );
			midi_tx_3byte( 157, new_x, 0 );
		}
		else if( ( new_button == 1 ) && ( action_mode == 2 ) ){
			midi_tx_3byte( 158, new_x, new_y );
			midi_tx_3byte( 158, new_x, 0 );
		}
	}
	if( color_change != 0 ) color_no = (color_no+1) % 6;
}

IndyのC その5 マウスぐりぐりお絵描きMIDI

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define WORLD_XMIN		0.0
#define WORLD_XMAX		200.0
#define WORLD_YMIN		0.0
#define WORLD_YMAX		200.0
#define	GREY			8
#define MOUSE_OBJ 		1
#define LEN			700.0

/*** (^_^;) Bug Recovery of SGI Media Library --> Original MIDI Defines !! (^_^;) ***/
#define MES(x)			( (x[0]).mm.msgbuf )
#define midi_set_status(x,d)	( MES(x) = ( ( d > 0xbf ) && ( d < 0xe0 ) ) ? 0x40000000 : 0x60000000, \
				  MES(x) &= 0xff0fffff, MES(x) |= ( ( d & 0xf0 ) << 16 ) )
#define midi_set_channel(x,d)	( MES(x) &= 0xfff0ffff, MES(x) |= ( ( d & 0x0f ) << 16 ) )
#define midi_set_keyno(x,d)	( MES(x) &= 0xffff00ff, MES(x) |= ( ( d & 0x7f ) << 8 ) )
#define midi_set_velocity(x,d)	( MES(x) &= 0xffffff00, MES(x) |= ( d & 0x7f ) )

MIport *midi_port;
MIevent midi[100];
XtAppContext app_context;

int mx, my, xmin, xmax, ymin, ymax, button[3], color_no, action_mode, error_st;
float xrat, yrat, cx, cy;
int mode_color_table[7] = { 9, 10, 11, 12, 13, 14, 15 };

void init_window(), do_resize(), input(), draw_screen(), quit(), clear_screen();
void draw_mode(), paint_mode(), stamp_mode(), effect_mode(), erase_mode();
void midi_initialize(), midi_event_generate(), midi_transmit(), midi_tx_3byte();

main(int argc, char **argv)
{
	Arg args[20];
	int n;
	Widget glw, toplevel, form, frame, pushb[10];
	GLXconfig glxConfig [] = { { GLX_NORMAL, GLX_RGB, FALSE },
				{ GLX_NORMAL, GLX_DOUBLE, TRUE }, { 0,0,0 }, };
	long background[] = { 0, 0, 0 };

	toplevel = XtAppInitialize(&app_context, "", (XrmOptionDescList)NULL , 0, (Cardinal*)&argc, 
			(String*)argv, (String*)NULL, (ArgList)NULL, 0);
	n = 0;
	XtSetArg(args[n], XtNwidth, 900); n++;
	XtSetArg(args[n], XtNheight, 650); n++;
	form = XtCreateManagedWidget("", xmFormWidgetClass, toplevel, args, n);
	n = 0;
	XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNbottomOffset, 10); n++;
	XtSetArg(args[n], XmNrightOffset, 40); n++;
	pushb[0] = XtCreateManagedWidget("Quit", xmPushButtonWidgetClass, form, args, n);
	XtAddCallback(pushb[0], XmNactivateCallback, quit,0);
	n = 0;
	XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNbottomOffset, 10); n++;
	XtSetArg(args[n], XmNleftOffset, 40); n++;
	pushb[1] = XtCreateManagedWidget("Draw", xmPushButtonWidgetClass, form, args, n);
	XtAddCallback(pushb[1], XmNactivateCallback, draw_mode,0);
	n = 0;
	XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
	XtSetArg(args[n], XmNbottomOffset, 10); n++;
	XtSetArg(args[n], XmNleftWidget, pushb[1]); n++;
	XtSetArg(args[n], XmNleftOffset, 30); n++;
	pushb[2] = XtCreateManagedWidget("Paint", xmPushButtonWidgetClass, form, args, n);
	XtAddCallback(pushb[2], XmNactivateCallback, paint_mode,0);
	n = 0;
	XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
	XtSetArg(args[n], XmNbottomOffset, 10); n++;
	XtSetArg(args[n], XmNleftWidget, pushb[2]); n++;
	XtSetArg(args[n], XmNleftOffset, 30); n++;
	pushb[3] = XtCreateManagedWidget("Stamp", xmPushButtonWidgetClass, form, args, n);
	XtAddCallback(pushb[3], XmNactivateCallback, stamp_mode,0);
	n = 0;
	XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
	XtSetArg(args[n], XmNbottomOffset, 10); n++;
	XtSetArg(args[n], XmNleftWidget, pushb[3]); n++;
	XtSetArg(args[n], XmNleftOffset, 30); n++;
	pushb[4] = XtCreateManagedWidget("Effect", xmPushButtonWidgetClass, form, args, n);
	XtAddCallback(pushb[4], XmNactivateCallback, effect_mode,0);
	n = 0;
	XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
	XtSetArg(args[n], XmNbottomOffset, 10); n++;
	XtSetArg(args[n], XmNleftWidget, pushb[4]); n++;
	XtSetArg(args[n], XmNleftOffset, 30); n++;
	pushb[5] = XtCreateManagedWidget("Erase", xmPushButtonWidgetClass, form, args, n);
	XtAddCallback(pushb[5], XmNactivateCallback, erase_mode,0);
	n = 0;
	XtSetArg(args[n], XtNx, 30); n++;
	XtSetArg(args[n], XtNy, 30); n++;
	XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); n++;
	XtSetArg(args[n], XmNtopOffset, 20); n++;
	XtSetArg(args[n], XmNleftOffset, 20); n++;
	XtSetArg(args[n], XmNrightOffset, 20); n++;
	XtSetArg(args[n], XmNbottomWidget, pushb[0]); n++;
	XtSetArg(args[n], XmNbottomOffset, 10); n++;
	frame = XtCreateManagedWidget("", xmFrameWidgetClass, form, args, n);
	n = 0;
	XtSetArg(args[n], GlxNglxConfig, glxConfig); n++;
	glw = XtCreateManagedWidget("", glxMDrawWidgetClass, frame, args, n);
	XtAddCallback(glw, GlxNginitCallback, init_window, 0);
	XtAddCallback(glw, GlxNinputCallback, input, 0);
	XtAddCallback(glw, GlxNresizeCallback, do_resize, 0);

	WidgetBackgroundToGlC3i(glw,background);
	XtRealizeWidget(toplevel);
	installColormap(toplevel,glw);
	XtAppMainLoop(app_context);
}

static void quit(Widget w, caddr_t client_data, caddr_t call_data)
{
	midi_tx_3byte( 160, 127, 127 );
	midi_tx_3byte( 160, 127, 0 );
	MIclose( midi_port );
	MIfreeport( midi_port );
	exit (0);
}

static void init_window(Widget w, caddr_t client_data, GlxDrawCallbackStruct *call_data)
{
	Matrix C_spline={{-0.5,1.5,-1.5,0.5},{1.0,-2.5,2.0,-0.5},{-0.5,0.0,0.5,0.0},{0.0,1.0,0.0,0.0},};
	ortho2( WORLD_XMIN, WORLD_XMAX, WORLD_YMIN, WORLD_YMAX );
	defbasis( 100, C_spline );
	curveprecision( 20 );
	curvebasis( 100);
	makeobj(MOUSE_OBJ);
		color(WHITE); rectf(-4.0,-6.0,4.0,6.0);
		color(BLACK); rect(-4.0,-6.0,4.0,6.0); rect(-2.75,0.0,-1.25,4.5);
		rect(-0.75,0.0, 0.75,4.5); rect( 1.25,0.0, 2.75,4.5);
		color(GREY); rectf(-3.0,-6.0,5.0,-7.0); rectf(4.0,-7.0,5.0,5.0);
	closeobj();
	button[0] = button[1] = button[2] = 0;
	do_resize(w,client_data,call_data);
	srandom(1);
	color_no = 6;
	action_mode = error_st = 0;
	midi_initialize();
	midi_tx_3byte( 159, 1, 127 );	/* to Draw */
	midi_tx_3byte( 159, 1, 0 );
}

static void do_resize(Widget w, caddr_t client_data, GlxDrawCallbackStruct *call_data)
{
	GLXwinset(XtDisplay(w), XtWindow(w));
	xmin = 0; xmax = (Screencoord)call_data->width - 1;
	ymin = 0; ymax = (Screencoord)call_data->height - 1;
	xrat = (WORLD_XMAX - WORLD_XMIN) / (float)(xmax - xmin);
	yrat = (WORLD_YMAX - WORLD_YMIN) / (float)(ymax - ymin);
	viewport( 0, xmax, 0, ymax );
	mx = xmax / 2; my = ymax / 2;
	draw_screen(w);
}

static void midi_initialize()
{
	MIconfig *c;
	c = MInewconfig();
	midi_port = MInewport();
	if( MIopen( midi_port, "rw", &c ) < 0 ) exit(-1);
}

static void midi_transmit( int status, int channel, int keyno, int velocity )
{
	midi_set_status( midi, ( status & 0xf0 ) );
	midi_set_channel( midi, ( channel & 0x0f ) );
	midi_set_keyno( midi, ( keyno & 0x7f ) );
	midi_set_velocity ( midi, ( velocity & 0x7f ) );
	if( MIsend( midi_port, midi, 1 ) < 0 ) exit(-1);
}

static void midi_tx_3byte( int status_byte, int keyno, int velocity )
{
	midi_transmit( ( status_byte & 0xf0 ), ( status_byte & 0x0f ), keyno, velocity );
}

static void draw_screen(Widget w)
{
	static Coord geom[4][3] = { { WORLD_XMIN, 6.0-LEN, 0.0 }, { 0.0, 6.0, 0.0 },
				{ WORLD_XMIN, WORLD_YMAX, 0.0 }, { 0.0, WORLD_YMAX+LEN, 0.0 }, };

	GLXwinset(XtDisplay(w), XtWindow(w));
	color( mode_color_table[color_no] );
	clear();
	color(BLACK);
	geom[0][1] = cy + 6.0 - LEN;
	geom[1][0] = cx;
	geom[1][1] = cy + 6.0; 
	geom[3][0] = cx;
	crvn(4,geom);
	pushmatrix();
		translate( cx, cy, 0.0 );
		callobj(MOUSE_OBJ);
		if ( button[0] == 1 ) rectf(-2.75,0.0,-1.25,4.5);
		if ( button[1] == 1 ) rectf(-0.75,0.0, 0.75,4.5);
		if ( button[2] == 1 ) rectf( 1.25,0.0, 2.75,4.5);
	popmatrix();
	swapbuffers();
}

static void clear_screen(Widget w)
{
	GLXwinset(XtDisplay(w), XtWindow(w));
	color( mode_color_table[color_no] );
	clear();
	swapbuffers();
	error_st = 0;
}

static void draw_mode(Widget w, caddr_t client_data, caddr_t call_data)
{
	int d, p=0;
	if( action_mode == 0 ) p = 20;
	else error_st == 0;
	d = 1 + p + random() % 16;
	midi_tx_3byte( 159, d, 127 );			/* to Draw */
	midi_tx_3byte( 159, d, 0 );
	action_mode = 0;
	color_no = (color_no+1) % 6;
	clear_screen(w);
}

static void paint_mode(Widget w, caddr_t client_data, caddr_t call_data)
{
	midi_tx_3byte( 159, 60, 127 );			/* to Paint */
	midi_tx_3byte( 159, 60, 0 );
	action_mode = 1;
	color_no = 6;
	clear_screen(w);
}

static void stamp_mode(Widget w, caddr_t client_data, caddr_t call_data)
{
	int d, p=0;
	if( action_mode == 2 ) p = 20;
	else error_st == 0;
	d = 71 + p + random() % 10;
	midi_tx_3byte( 159, d, 127 );			/* to Stamp */
	midi_tx_3byte( 159, d, 0 );
	action_mode = 2;
	color_no = (color_no+1) % 6;
	clear_screen(w);
}

static void effect_mode(Widget w, caddr_t client_data, caddr_t call_data)
{
	int d;
	d = random() % 3;
	midi_tx_3byte( 159, 121 + d, 127 );		/* Effect */
	midi_tx_3byte( 159, 121 + d, 0 );
	action_mode = 3;
	color_no = 6;
	clear_screen(w);
}

static void erase_mode(Widget w, caddr_t client_data, caddr_t call_data)
{
	int d;
	d = random() % 3;
	midi_tx_3byte( 159, 65 + d, 127 );		/* Erase */
	midi_tx_3byte( 159, 65 + d, 0 );
	action_mode = 3;
	color_no = 6;
	clear_screen(w);
}

static void input(Widget w, caddr_t client_data, GlxDrawCallbackStruct *call_data)
{
	int d, new_x, new_y, in_out = 0;

	mx = call_data->event->xbutton.x;
	my = call_data->event->xbutton.y;
	cx = (float)(mx - xmin) * xrat;
	cy = (float)(ymax - ymin - my) * yrat;
	new_x = 20 + (int)( cx * 0.4 );
	new_y = 20 + (int)( cy * 0.4 );
	switch(call_data->event->type){
		case ButtonPress:
			switch(call_data->event->xbutton.button){
				case Button1:
					button[0] = 1;
					if( action_mode == 1 ){
						d = 41 + ( random() % 16 );
						midi_tx_3byte( 159, d, 127 );
						midi_tx_3byte( 159, d, 0 );
						color_no = (color_no+1) % 6;
				   }
					else if( action_mode == 0 ){
						midi_tx_3byte( 157, new_x, new_y );
						midi_tx_3byte( 157, new_x, 0 );
					}
					else if( action_mode == 2 ){
						midi_tx_3byte( 158, new_x, new_y );
						midi_tx_3byte( 158, new_x, 0 );
						color_no = (color_no+1) % 6;
					}
					break;
				case Button2:	button[1] = 1;	break;
				case Button3:	button[2] = 1;	break;
			}
			draw_screen(w);
			break;
		case ButtonRelease:
			switch(call_data->event->xbutton.button){
				case Button1:	button[0] = 0;	break;
				case Button2:	button[1] = 0;	break;
				case Button3:	button[2] = 0;	break;
			}
			draw_screen(w);
			break;
		case MotionNotify:
			if (call_data->event->xmotion.state){
				if( button[0] == 1 ){
					if( (cx<0.0)||(cx>200.0)||(cy<0.0)||(cy>200.0) ) in_out = 1;
					if( (error_st == 0) && (in_out == 1) ){
						midi_tx_3byte( 157, 120, 127 );
						midi_tx_3byte( 157, 120, 0 );
						error_st = 1;
					}
					else if( in_out == 0 ){
						if( action_mode == 0 ){
							midi_tx_3byte( 157, new_x, new_y );
							midi_tx_3byte( 157, new_x, 0 );
						}
						else if( action_mode == 2 ){
							midi_tx_3byte( 158, new_x, new_y );
							midi_tx_3byte( 158, new_x, 0 );
						}
						if( error_st == 1 ) error_st = 0;
					}
				}
				draw_screen(w);
			}
			break;
	}	
}

IndyのC その6 グラニュラサンプリング

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define FIFO_SIZE	48000
#define pi 3.1415926535

unsigned int audio_pid;
int audio_mode, sample_param, pitch_param, offset_param;
char *audio_menu[] = { "through", "distrotion", "oscillator", "granular synth", "granular sampling" };
short buffer[FIFO_SIZE];

void self_kill(), create_sound(), sound_off(), scaler_volume(), scaler_sample(), scaler_pitch(), scaler_offset();
void audio_through(), audio_distrotion(), audio_oscillator(), audio_granular(), audio_effector();

void main(unsigned int argc, char** argv)
{
	Widget toplevel, title, bigbox, sbox[10], pb2[5], pb3[3], volume, sample, pitch, offset;
	XmString xmstr;
	Arg args[10];
	char buf[80];
	int i,n;

/***** Motif Screen Define *****/
	toplevel = XtInitialize( argv[0], "", NULL, 0, &argc, argv );
	bigbox = XtCreateManagedWidget( "", boxWidgetClass, toplevel, NULL, 0 );
	xmstr = XmStringCreateSimple("  (^_^) Audio Test by Yoichi Nagashima (^_^)  ");
	XtSetArg( args[0], XmNlabelString, xmstr );
	title = XtCreateManagedWidget( "", xmLabelWidgetClass, bigbox, args, 1 );
	for(i=0;i<10;i++){
		sbox[i] = XtCreateManagedWidget( "", boxWidgetClass, bigbox, NULL, 0 );
	}
	pb3[0] = XtCreateManagedWidget( "Sound Port OFF", xmPushButtonWidgetClass, sbox[0], NULL, 0 );
	pb3[1] = XtCreateManagedWidget( "Quit to System", xmPushButtonWidgetClass, sbox[0], NULL, 0 );
	pb2[0] = XtCreateManagedWidget( "Mode(1) Through", xmPushButtonWidgetClass, sbox[1], NULL, 0 );
	pb2[1] = XtCreateManagedWidget( "Mode(2) Distortion", xmPushButtonWidgetClass, sbox[1], NULL, 0 );
	pb2[2] = XtCreateManagedWidget( "Mode(3) Oscillator", xmPushButtonWidgetClass, sbox[1], NULL, 0 );
	pb2[3] = XtCreateManagedWidget( "Mode(4) Granular Synth", xmPushButtonWidgetClass, sbox[1], NULL, 0 );
	pb2[4] = XtCreateManagedWidget( "Mode(5) Granular Sampling", xmPushButtonWidgetClass, sbox[1], NULL, 0 );
	n = 0;
	XtSetArg( args[n], XmNminimum, 0 ); n++;
	XtSetArg( args[n], XmNmaximum, 255 ); n++;
	XtSetArg( args[n], XmNscaleHeight, 30 ); n++;
	XtSetArg( args[n], XmNscaleWidth, 250 ); n++;
	XtSetArg( args[n], XmNorientation, XmHORIZONTAL ); n++;
	xmstr = XmStringCreateSimple("Speaker Gain Control");
	XtSetArg( args[n], XmNtitleString, xmstr ); n++;
	XtSetArg( args[n], XmNshowValue, True ); n++;
	volume = XtCreateManagedWidget( "", xmScaleWidgetClass, sbox[2], args, n );
	n = 0;
	XtSetArg( args[n], XmNminimum, 1 ); n++;
	XtSetArg( args[n], XmNmaximum, 99 ); n++;
	XtSetArg( args[n], XmNscaleHeight, 30 ); n++;
	XtSetArg( args[n], XmNscaleWidth, 250 ); n++;
	XtSetArg( args[n], XmNorientation, XmHORIZONTAL ); n++;
	xmstr = XmStringCreateSimple("Gr. Density Control");
	XtSetArg( args[n], XmNtitleString, xmstr ); n++;
	XtSetArg( args[n], XmNshowValue, True ); n++;
	sample = XtCreateManagedWidget( "", xmScaleWidgetClass, sbox[3], args, n );
	n = 0;
	XtSetArg( args[n], XmNminimum, 1 ); n++;
	XtSetArg( args[n], XmNmaximum, 99 ); n++;
	XtSetArg( args[n], XmNscaleHeight, 30 ); n++;
	XtSetArg( args[n], XmNscaleWidth, 250 ); n++;
	XtSetArg( args[n], XmNorientation, XmHORIZONTAL ); n++;
	xmstr = XmStringCreateSimple("Grain Width Control");
	XtSetArg( args[n], XmNtitleString, xmstr ); n++;
	XtSetArg( args[n], XmNshowValue, True ); n++;
	pitch = XtCreateManagedWidget( "", xmScaleWidgetClass, sbox[4], args, n );
	n = 0;
	XtSetArg( args[n], XmNminimum, 1 ); n++;
	XtSetArg( args[n], XmNmaximum, 32 ); n++;
	XtSetArg( args[n], XmNscaleHeight, 30 ); n++;
	XtSetArg( args[n], XmNscaleWidth, 250 ); n++;
	XtSetArg( args[n], XmNorientation, XmHORIZONTAL ); n++;
	xmstr = XmStringCreateSimple("Grain Offset Control");
	XtSetArg( args[n], XmNtitleString, xmstr ); n++;
	XtSetArg( args[n], XmNshowValue, True ); n++;
	offset = XtCreateManagedWidget( "", xmScaleWidgetClass, sbox[5], args, n );

/***** Callback Routine Define *****/
	for(i=0;i<5;i++){
		XtAddCallback( pb2[i], XmNactivateCallback, create_sound, audio_menu[i] );
	}
	XtAddCallback( pb3[0], XmNactivateCallback, sound_off, NULL );
	XtAddCallback( pb3[1], XmNactivateCallback, self_kill, NULL );
	XtAddCallback( volume, XmNvalueChangedCallback, scaler_volume, NULL );
	XtAddCallback( volume, XmNdragCallback, scaler_volume, NULL );
	XtAddCallback( sample, XmNvalueChangedCallback, scaler_sample, NULL );
	XtAddCallback( sample, XmNdragCallback, scaler_sample, NULL );
	XtAddCallback( pitch, XmNvalueChangedCallback, scaler_pitch, NULL );
	XtAddCallback( pitch, XmNdragCallback, scaler_pitch, NULL );
	XtAddCallback( offset, XmNvalueChangedCallback, scaler_offset, NULL );
	XtAddCallback( offset, XmNdragCallback, scaler_offset, NULL );

/***** Start Realize ! *****/
	XtRealizeWidget(toplevel);
	XtMainLoop();
}

void self_kill(Widget w, caddr_t message, caddr_t para)
{
	sound_off();
	printf("\nBye Bye !  (^o^)/\n\n");
	exit(0);
}

void create_sound(Widget w, caddr_t message, caddr_t para)
{
	sound_off();
	if( !strcmp( message, "distrotion" ) ){
		sample_param = 1;
		audio_pid = sproc( audio_distrotion, PR_SALL );
		audio_mode = 1;
	}
	else if( !strcmp( message, "oscillator" ) ){
		sample_param = 1;
		audio_pid = sproc( audio_oscillator, PR_SALL );
		audio_mode = 2;
	}
	else if( !strcmp( message, "granular synth" ) ){
		sample_param = 1;
		pitch_param = 1;
		audio_pid = sproc( audio_granular, PR_SALL );
		audio_mode = 3;
	}
	else if( !strcmp( message, "granular sampling" ) ){
		sample_param = 16;
		pitch_param = 1;
		offset_param = 1;
		audio_pid = sproc( audio_effector, PR_SALL );
		audio_mode = 4;
	}
	else if( !strcmp( message, "through" ) ){
		audio_pid = sproc( audio_through, PR_SALL );
		audio_mode = 5;
	}
	if (audio_pid == -1) exit(-1);
	printf("  Registered Audio PID = %d\n",audio_pid);
}

void sound_off()
{
	long parameter_buffer[4];
	if(audio_pid != 0){
		parameter_buffer[0] = AL_LEFT_SPEAKER_GAIN;
		parameter_buffer[2] = AL_RIGHT_SPEAKER_GAIN;
		ALgetparams( AL_DEFAULT_DEVICE, parameter_buffer, 4 );
		parameter_buffer[1] = 50;
		parameter_buffer[3] = 50;
		ALsetparams( AL_DEFAULT_DEVICE, parameter_buffer, 4 );
		kill(audio_pid, SIGKILL);
		printf("  Killed : Audio PID = %d\n",audio_pid);
		audio_pid = 0; audio_mode = 0;
	}
}

void scaler_volume(Widget w, caddr_t message, caddr_t para)
{
	long parameter_buffer[4];
	XmScaleCallbackStruct  *callback = (XmScaleCallbackStruct *) para;
	parameter_buffer[0] = AL_LEFT_SPEAKER_GAIN;
	parameter_buffer[2] = AL_RIGHT_SPEAKER_GAIN;
	ALgetparams( AL_DEFAULT_DEVICE, parameter_buffer, 4 );
	parameter_buffer[1] = callback->value;
	parameter_buffer[3] = callback->value;
	ALsetparams( AL_DEFAULT_DEVICE, parameter_buffer, 4 );
}

void scaler_sample(Widget w, caddr_t message, caddr_t para)
{
	XmScaleCallbackStruct  *callback = (XmScaleCallbackStruct *) para;
	sample_param = callback->value;
}

void scaler_pitch(Widget w, caddr_t message, caddr_t para)
{
	int i, width;
	XmScaleCallbackStruct  *callback = (XmScaleCallbackStruct *) para;
	pitch_param = callback->value;
	if( audio_mode == 3){
		width = 64 + 4 * pitch_param;
		for(i=0;ivalue;
}

void audio_through()
{
	ALconfig	config;
	ALport		input_port, output_port;
	short		buff[256];
	int		result, i = 256;
	config = ALnewconfig();
	ALsetqueuesize( config, 2048 );
	input_port = ALopenport( "input", "r", config );
	output_port = ALopenport( "output", "w", config );
	if ( input_port == NULL || output_port == NULL ) exit(-1);
	result = prctl(PR_RESIDENT);
	if(result == -1) fprintf(stderr, "permission denied to be [memory-resident]. (;_;)\n"); 
	result = schedctl(NDPRI, 0, NDPHIMAX);
	if(result == -1) fprintf(stderr, "permission denied to set process priority. (;_;)\n");
	while (1)
	{
		ALreadsamps( input_port, buff, i );
		ALwritesamps( output_port, buff, i );
	}
}
void audio_distrotion()
{
	ALconfig	config;
	ALport		input_port, output_port;
	short		buff[256];
	int		result, i = 256;
	config = ALnewconfig();
	ALsetqueuesize( config, 2048 );
	input_port = ALopenport( "input", "r", config );
	output_port = ALopenport( "output", "w", config );
	if ( input_port == NULL || output_port == NULL ) exit(-1);
	result = prctl(PR_RESIDENT);
	if(result == -1) fprintf(stderr, "permission denied to be [memory-resident]. (;_;)\n"); 
	result = schedctl(NDPRI, 0, NDPHIMAX);
	if(result == -1) fprintf(stderr, "permission denied to set process priority. (;_;)\n");
	while (1)
	{
		ALreadsamps( input_port, buff, sample_param );
		for(i=0;i 256 ){
			for(i=0;i<256;i++){
				buff[i] = buffer[read_pointer++];
				if( read_pointer > sample_param * 256 ) read_pointer=0;
			}
			ALwritesamps( output_port, buff, 256 );
		}
	}
}

void audio_granular()
{
	ALconfig	config;
	ALport		output_port;
	short		buff[256];
	unsigned int	read_pointer[20];
	int		i, j;
	config = ALnewconfig();
	ALsetqueuesize( config, FIFO_SIZE );
	output_port = ALopenport( "output", "w", config );
	if ( output_port == NULL ) exit(-1);
	for(i=0;i<20;i++) read_pointer[i] = 0;
	for(i=0;i<128;i++) buffer[i] = (short)( 800.0 * ( 1.0 + sin( 2.0 * pi * (double)i / 128.0 + 2.0 * pi * 3.0 / 4.0 )));
	for(i=128;i 256 ){
			for(i=0;i<256;i++){
				buff[i] = 0;
				for(j=0;j<20;j++){
					buff[i] = buff[i] + buffer[read_pointer[j]++];
					if( read_pointer[j] > ( sample_param + 10 ) * 10 * (j+5) ) read_pointer[j] = 0;
				}
			}
			ALwritesamps( output_port, buff, 256 );
		}
	}
}

void audio_effector()
{
	ALconfig	config;
	ALport		input_port, output_port;
	short		buff[1024];
	float		shift, weight[1024];
	unsigned int	read_pointer[20];
	int		i, j, result;
	config = ALnewconfig();
	ALsetqueuesize( config, 8192 );
	input_port = ALopenport( "input", "r", config );
	output_port = ALopenport( "output", "w", config );
	if ( input_port == NULL || output_port == NULL ) exit(-1);
	result = prctl(PR_RESIDENT);
	if(result == -1) fprintf(stderr, "permission denied to be [memory-resident]. (;_;)\n"); 
	result = schedctl(NDPRI, 0, NDPHIMAX);
	if(result == -1) fprintf(stderr, "permission denied to set process priority. (;_;)\n");
	for(i=0;i<20;i++) read_pointer[i] = 0;
	for(i=0;i<1024;i++) weight[i] = ( 1.0 + sin( 2.0 * pi * (float)i / 1024.0 + 2.0 * pi * 3.0 / 4.0 )) / 2.0;
	for(i=0;i 1001 + 20 * sample_param + j * offset_param ) read_pointer[j] = 0;
			}
		}
		ALwritesamps( output_port, buff, 1024 );
	}
}

IndyのC その7 MIDI制御3次元CG

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define XS 100
#define YS 100

/*** (^_^;) Bug Recovery of SGI Media Library --> Original MIDI Defines !! (^_^;) ***/
#define MES(x)			( (x[0]).mm.msgbuf )
#define midi_set_status(x,d)	( MES(x) = ( ( d > 0xbf ) && ( d < 0xe0 ) ) ? 0x40000000 : 0x60000000, \
				  MES(x) &= 0xff0fffff, MES(x) |= ( ( d & 0xf0 ) << 16 ) )
#define midi_set_channel(x,d)	( MES(x) &= 0xfff0ffff, MES(x) |= ( ( d & 0x0f ) << 16 ) )
#define midi_set_keyno(x,d)	( MES(x) &= 0xffff00ff, MES(x) |= ( ( d & 0x7f ) << 8 ) )
#define midi_set_velocity(x,d)	( MES(x) &= 0xffffff00, MES(x) |= ( d & 0x7f ) )

MIport *midi_port;
MIevent midi[100];

int interval = 10, st = 5, mx, my, sc_x, sc_y, sp = 3, spp = 0, mode = 0, sst, ssp, mmx, mmy;
XtAppContext app_context;

int vert[8][4][3] = { 110,110,0,10,110,0,10,10,0,110,10,0,-110,110,0,-10,110,0,-10,10,0,-110,10,0,
		110,-110,0,10,-110,0,10,-10,0,110,-10,0,-110,-110,0,-10,-110,0,-10,-10,0,-110,-10,0,
		-220,-220,0,-120,-220,0,-120,-120,0,-220,-120,0,220,220,0,120,220,0,120,120,0,220,120,0,
		-220,220,0,-120,220,0,-120,120,0,-220,120,0,220,-220,0,120,-220,0,120,-120,0,220,-120,0 };
int vcolor[8][3] = { 30,30,255,255,30,30,255,30,30,30,30,255,210,250,50,210,250,50,210,250,50,210,250,50 };
int t_mpy[8] = { 1,1,1,1,3,3,3,3 };
int t_sgn[4][8] = { 1,1,1,1,1,1,1,1,-1,1,-1,1,-1,1,-1,1,1,-1,1,-1,1,-1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1 };
long background[3] = { 0, 0, 0 };

GLXconfig glxConfig [] = { { GLX_NORMAL, GLX_DOUBLE, TRUE }, { GLX_NORMAL, GLX_RGB, TRUE },
				{ GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG }, { 0, 0, 0 } };

void init_window(), do_resize(), input(), set_inc(), set_rot(), quit(), handle_timeout(), sound_on(), sound_off();
void midi_initialize(), midi_event_generate(), midi_transmit(), midi_tx_3byte(), midi_tx_2byte();

main(int argc, char **argv)
{
	Arg args[20];
	int n;
	Widget glw, toplevel, form, frame, slider1, slider2, pushb[3];

	toplevel = XtAppInitialize(&app_context, "", (XrmOptionDescList)NULL , 0, (Cardinal*)&argc, 
			(String*)argv, (String*)NULL, (ArgList)NULL, 0);
	n = 0;
	XtSetArg(args[n], XtNwidth, 750); n++;
	XtSetArg(args[n], XtNheight, 700); n++;
	form = XtCreateManagedWidget("", xmFormWidgetClass, toplevel, args, n);
	n = 0;
	XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNtopOffset, 10); n++;
	XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNleftOffset, 30); n++;
	pushb[0] = XtCreateManagedWidget("Quit", xmPushButtonWidgetClass, form, args, n);
	XtAddCallback(pushb[0], XmNactivateCallback, quit,0);
	n = 0;
	XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNtopOffset, 10); n++;
	XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
	XtSetArg(args[n], XmNleftWidget, pushb[0]); n++;
	XtSetArg(args[n], XmNleftOffset, 20); n++;
	pushb[1] = XtCreateManagedWidget("Sound Off", xmPushButtonWidgetClass, form, args, n);
	XtAddCallback(pushb[1], XmNactivateCallback, sound_off,0);
	n = 0;
	XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNtopOffset, 10); n++;
	XtSetArg(args[n], XmNleftAttachment, XmATTACH_WIDGET); n++;
	XtSetArg(args[n], XmNleftWidget, pushb[1]); n++;
	XtSetArg(args[n], XmNleftOffset, 20); n++;
	pushb[2] = XtCreateManagedWidget("Sound On", xmPushButtonWidgetClass, form, args, n);
	XtAddCallback(pushb[2], XmNactivateCallback, sound_on,0);
	n = 0;
	XtSetArg(args[n], XtNwidth, 20); n++;
	XtSetArg(args[n], XtNheight, 420); n++;
	XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
	XtSetArg(args[n], XmNtopWidget, pushb[0]); n++;
	XtSetArg(args[n], XmNtopOffset, 10); n++;
	XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNrightOffset, 30); n++;
	XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNbottomOffset, 30); n++;
	XtSetArg(args[n], XmNminimum, -375); n++;
	XtSetArg(args[n], XmNmaximum, 400); n++;
	XtSetArg(args[n], XmNvalue, st); n++;
	XtSetArg(args[n], XmNsliderSize, 25); n++;
	XtSetArg(args[n], XmNincrement, 1); n++;
	XtSetArg(args[n], XmNpageIncrement, 10); n++;
	XtSetArg(args[n], XmNvalue, -20); n++;
	slider2 = XtCreateManagedWidget("", xmScrollBarWidgetClass, form, args, n);
	XtAddCallback(slider2, XmNdecrementCallback, set_inc,0);
	XtAddCallback(slider2, XmNincrementCallback, set_inc,0);
	XtAddCallback(slider2, XmNdragCallback, set_inc,0);
	XtAddCallback(slider2, XmNpageIncrementCallback, set_inc,0);
	XtAddCallback(slider2, XmNpageDecrementCallback, set_inc,0);
	n = 0;
	XtSetArg(args[n], XtNwidth, 20); n++;
	XtSetArg(args[n], XtNheight, 420); n++;
	XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
	XtSetArg(args[n], XmNtopWidget, pushb[0]); n++;
	XtSetArg(args[n], XmNtopOffset, 10); n++;
	XtSetArg(args[n], XmNrightAttachment, XmATTACH_WIDGET); n++;
	XtSetArg(args[n], XmNrightWidget, slider2); n++;
	XtSetArg(args[n], XmNrightOffset, 20); n++;
	XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNbottomOffset, 30); n++;
	XtSetArg(args[n], XmNminimum, -175); n++;
	XtSetArg(args[n], XmNmaximum, 200); n++;
	XtSetArg(args[n], XmNvalue, sp); n++;
	XtSetArg(args[n], XmNsliderSize, 25); n++;
	XtSetArg(args[n], XmNincrement, 1); n++;
	XtSetArg(args[n], XmNpageIncrement, 10); n++;
	XtSetArg(args[n], XmNvalue, 0); n++;
	slider1 = XtCreateManagedWidget("", xmScrollBarWidgetClass, form, args, n);
	XtAddCallback(slider1, XmNdecrementCallback, set_rot,0);
	XtAddCallback(slider1, XmNincrementCallback, set_rot,0);
	XtAddCallback(slider1, XmNdragCallback, set_rot,0);
	XtAddCallback(slider1, XmNpageIncrementCallback, set_rot,0);
	XtAddCallback(slider1, XmNpageDecrementCallback, set_rot,0);
	n = 0;
	XtSetArg(args[n], XmNtopAttachment, XmATTACH_WIDGET); n++;
	XtSetArg(args[n], XmNtopWidget, pushb[0]); n++;
	XtSetArg(args[n], XmNtopOffset, 10); n++;
	XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNleftOffset, 30); n++;
	XtSetArg(args[n], XmNrightAttachment, XmATTACH_WIDGET); n++;
	XtSetArg(args[n], XmNrightWidget, slider1); n++;
	XtSetArg(args[n], XmNrightOffset, 25); n++;
	XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
	XtSetArg(args[n], XmNbottomOffset, 30); n++;
	XtSetArg(args[n], XmNshadowType, XmSHADOW_IN); n++;
	frame = XtCreateManagedWidget("", xmFrameWidgetClass, form, args, n);
	n = 0;
	XtSetArg(args[n], GlxNglxConfig, glxConfig); n++;
	glw = XtCreateManagedWidget("", glxMDrawWidgetClass, frame, args, n);
	XtAddCallback(glw, GlxNresizeCallback, do_resize, 0);
	XtAddCallback(glw, GlxNginitCallback, init_window, 0);
	XtAddCallback(glw, GlxNinputCallback, input, 0);

	XtAppAddTimeOut(app_context, interval, handle_timeout, glw);
	WidgetBackgroundToGlC3i(glw,background);
	XtRealizeWidget(toplevel);
	installColormap(toplevel,glw);
	XtAppMainLoop(app_context);
}

static void quit(Widget w, caddr_t client_data, caddr_t call_data)
{
	midi_tx_2byte( 0xdf, 0 );
	MIclose( midi_port );
	MIfreeport( midi_port );
	exit (0);
}

static void init_window(Widget w, caddr_t client_data, caddr_t call_data)
{
	do_resize(w);
	RGBmode();
	RGBcolor(0,0,0); clear();
	doublebuffer();
	blendfunction(BF_ONE, BF_MDC);
	mmode(MVIEWING);
	perspective(900,1,-100,0);
	lookat (0,0,500,0,0,0,0);
	midi_initialize();
}

static void do_resize(Widget w, caddr_t client_data, GlxDrawCallbackStruct *call_data)
{
	sp = 0;
	GLXwinset(XtDisplay(w), XtWindow(w));
	sc_x = (Screencoord)call_data->width;
	sc_y = (Screencoord)call_data->height;
	viewport(0, sc_x-1, 0, sc_y-1);
}

static void input(Widget w, caddr_t client_data, GlxDrawCallbackStruct *call_data)
{
	char buffer[1];
	switch(call_data->event->type){
		case ButtonPress:
			switch(call_data->event->xbutton.button){
				case Button1:
					mx = call_data->event->xbutton.x - sc_x/2;
					my = call_data->event->xbutton.y - sc_y/2;
					scale_change();
					break;
			}
			break;
		case MotionNotify:
			if (call_data->event->xmotion.state & Button1Mask){
				mx = call_data->event->xbutton.x - sc_x/2;
				my = call_data->event->xbutton.y - sc_y/2;
				scale_change();
			}
			break;
	}	
}

static void handle_timeout(Widget w)
{
	int i,j;
	XtAppAddTimeOut(app_context, interval, handle_timeout, w);
	GLXwinset(XtDisplay(w), XtWindow(w));
	RGBcolor(0,0,0); clear();
	rotate (st,'y');
	for(i=0;i<4;i++){
	  	RGBcolor(vcolor[i][0],vcolor[i][1],vcolor[i][2]);
		bgnpolygon();
		for(j=0;j<4;j++) v3i(vert[i][j]);
		endpolygon();
	}
	pushmatrix();
	rotate (spp,'z');
	for(i=4;i<8;i++){
	  	RGBcolor(vcolor[i][0],vcolor[i][1],vcolor[i][2]);
		bgnpolygon();
		for(j=0;j<4;j++) v3i(vert[i][j]);
		endpolygon();
	}
	popmatrix();
	spp = spp + sp;
	swapbuffers();
	gflush();
}

static void midi_initialize()
{
	MIconfig *c;
	c = MInewconfig();
	midi_port = MInewport();
	if( MIopen( midi_port, "rw", &c ) < 0 ) exit(-1);
}

static void midi_transmit( int status, int channel, int keyno, int velocity )
{
	midi_set_status( midi, ( status & 0xf0 ) );
	midi_set_channel( midi, ( channel & 0x0f ) );
	midi_set_keyno( midi, ( keyno & 0x7f ) );
	midi_set_velocity ( midi, ( velocity & 0x7f ) );
	if( MIsend( midi_port, midi, 1 ) < 0 ) exit(-1);
}

static void midi_tx_3byte( int status_byte, int keyno, int velocity )
{
	midi_transmit( ( status_byte & 0xf0 ), ( status_byte & 0x0f ), keyno, velocity );
}

static void midi_tx_2byte( int status_byte, int param )
{
	midi_transmit( ( status_byte & 0xf0 ), ( status_byte & 0x0f ), param, 0 );
}

static void sound_on(Widget w, caddr_t client_data, caddr_t call_data)
{
	if(mode == 0){
		mode = 1;
		midi_tx_2byte( 0xdf, 1 );
	}
}

static void sound_off(Widget w, caddr_t client_data, caddr_t call_data)
{
	if(mode != 0){
		mode = 0;
		midi_tx_2byte( 0xdf, 0 );
	}
}

scale_change()
{
	int i;
	for(i=0;i<8;i++){
		vert[i][0][0] = t_sgn[i%4][0] * mx * t_mpy[i] - XS/2;
		vert[i][0][1] = t_sgn[i%4][1] * my * t_mpy[i] + YS/2;
		vert[i][1][0] = t_sgn[i%4][2] * mx * t_mpy[i] - XS/2;
		vert[i][1][1] = t_sgn[i%4][3] * my * t_mpy[i] - YS/2;
		vert[i][2][0] = t_sgn[i%4][4] * mx * t_mpy[i] + XS/2;
		vert[i][2][1] = t_sgn[i%4][5] * my * t_mpy[i] - YS/2;
		vert[i][3][0] = t_sgn[i%4][6] * mx * t_mpy[i] + XS/2;
		vert[i][3][1] = t_sgn[i%4][7] * my * t_mpy[i] + YS/2;
	}
	mmx = (int)( (mx+sc_x/2) * 128 / sc_x );
	if( mmx<0 ) mmx = 0;
	else if( mmx>127 ) mmx = 127;
	mmy = (int)( 127.9 - (my+sc_y/2) * 128 / sc_y );
	if( mmy<0 ) mmy = 0;
	else if( mmy>127 ) mmy = 127;
/*	printf(" [mx/my] changes : mx = %d, mmx = %d, my = %d mmy = %d\n",mx,mmx,my,mmy); */
	midi_tx_3byte( 0xaf, mmx, mmy );
}

static void set_inc(Widget w, caddr_t client_data, XmScrollBarCallbackStruct *call_data)
{
	st = call_data->value;
	if( st == 0 ) sst = 64;
	else if( st > 0 ) sst = 63 + (int)( 62.9 - (st+375) * 63 / 375 );
	else if( st < 0 ) sst = 65 + (int)( 62.9 - (st+375) * 63 / 375 );
/*	printf(" [st] changes : st = %d, sst = %d, sp = %d ssp = %d\n",st,sst,sp,ssp); */
	midi_tx_3byte( 0xbf, ssp, sst );
}

static void set_rot(Widget w, caddr_t client_data, XmScrollBarCallbackStruct *call_data)
{
	sp = call_data->value;
	if( sp == 0 ) ssp = 64;
	else if( sp > 0 ) ssp = 63 + (int)( 62.9 - (sp+175) * 63 / 175 );
	else if( sp < 0 ) ssp = 65 + (int)( 62.9 - (sp+175) * 63 / 175 );
/*	printf(" [sp] changes : st = %d, sst = %d, sp = %d ssp = %d\n",st,sst,sp,ssp); */
	midi_tx_3byte( 0xbf, ssp, sst );
}