Advertisement
Guest User

Untitled

a guest
May 21st, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 13.49 KB | None | 0 0
  1. {$apptype console}
  2. Program PSG_Player;
  3.  
  4. uses windows,mmsystem,myutils;
  5. type
  6. byte_array = ^pload_arr;
  7. pload_arr = array [0..MAXINT div 2] of byte;
  8.  
  9.  
  10. var J:byte_array;
  11.  
  12. const
  13.  
  14. ch_a_volume = $43;
  15. ch_b_volume = $44;
  16. ch_c_volume = $45;
  17. snare_volume    = $54;
  18.    
  19. ch_a_freq_lsb   = $A0;
  20. ch_a_freq_msb   = $B0;
  21. ch_b_freq_lsb   = $A1;
  22. ch_b_freq_msb   = $B1;
  23. ch_c_freq_lsb   = $A2;
  24. ch_c_freq_msb   = $B2;
  25. snare_freq_msb  = $B7;
  26. snare_freq_lsb  = $A7;
  27.  
  28. var
  29. xlat_mode : integer;
  30. mixer_reg, psg_volume_a, psg_volume_b, psg_volume_c,
  31. noise_enabled, noise_vol,adlib_a_vol,
  32. adlib_b_vol,
  33. adlib_c_vol:byte;  
  34. ch_a_flsb, ch_a_fmsb, ch_b_flsb, ch_b_fmsb, ch_c_flsb, ch_c_fmsb:byte;
  35. noise_freq, ch_a_freq, ch_b_freq, ch_c_freq : smallint;
  36.  
  37. var PSG_REGNUM,psg_value:byte;
  38.  
  39.  
  40. procedure Wrt_Adlib;assembler;
  41. asm
  42.                 push    edx    
  43.                 push    ecx
  44.                 push    eax    
  45.                 mov dx, 388h
  46.                 out dx, al     
  47.                 mov ecx, 6     
  48. @@1:            in  al, dx
  49.                 loop    @@1
  50.  
  51.  
  52.                 pop ax     
  53.                 inc dx     
  54.                 push    ax
  55.                 xchg    al, ah
  56.                 out dx, al     
  57.                 dec dx     
  58.                 mov     ecx, 35
  59. @@0:            in  al, dx
  60.                 loop    @@0  
  61.  
  62.  
  63.                 pop eax
  64.                 pop ecx
  65.                 pop edx
  66.                 {retf}
  67. end;
  68.  
  69. procedure Write_Adlib (a, b:byte);
  70. begin
  71. asm
  72.  
  73.                push    edx     
  74.                push    ecx
  75.  
  76.                 mov al, a
  77.                 mov edx, 388h
  78.                 out dx, al     
  79. //..END;
  80. //SLEEP(10);
  81.                 mov ecx, 6     
  82. @@1:            in  al, dx
  83.                 loop    @@1
  84. //ASM
  85.                 mov al, b
  86.                 mov edx, 389h
  87.                 out dx, al     
  88.  
  89.                dec  dx     
  90.                 mov     ecx, 35
  91. @@0:            in  al, dx
  92.                 loop    @@0  
  93. //END;
  94. //SLEEP(10);
  95.  
  96.                pop  ecx
  97.                pop  edx
  98.    
  99. end;
  100. end;
  101.  
  102. procedure Init_Adlib;{near;assembler;}
  103. var i:byte;
  104. begin
  105. for  i:= 1 to 255 do Write_Adlib (i,0);
  106. end;
  107.  
  108. procedure Synt_Init;
  109. const
  110. proper_init:array [0..71] of byte =(
  111.  
  112.         {   36, 0,      ; total regs to setup}
  113.  
  114.             $1, $20,    {; enable waveform distortion}
  115.  
  116.  
  117. {; --- CHANNEL A SETUP ---}
  118.  
  119.             $20, $01,   {; Channel A operator 1 (Modulator)}
  120.             $40, $18,   {; modulator volume}
  121.             $60, $F0,
  122.             $80, $14,   {; sustain level / release level (14h)}
  123.             $e0, $02,   {; ch.A distortion, operator 1}
  124.             $c0, $0A,   {; strong feedback / operator 2 make snd}
  125.             $23, $01,   {; Channel A operator 2 (Carrier)}
  126.             $63, $F0,
  127.             $83, $13,
  128.             $e3, $00,   {; Channel A distortion, operator 1}
  129.  
  130.  
  131. {; --- CHANNEL B SETUP ---}
  132.  
  133.             $21, $01,   {; Channel B operator 1 (modulator)}
  134.             $41, $18,
  135.             $61, $F0,
  136.             $81, $14,
  137.             $e1, $02,   {; ch.b distortion, operator 1}
  138.             $C1, $0A,
  139.             $24, $01,   {; Channel B operator 2 (carrier)}
  140.             $64, $F0,
  141.             $84, $13,
  142.             $e4, $00,   {; ch.b distortion, operator 2}
  143.  
  144.  
  145. {; --- CHANNEL C SETUP ---}
  146.  
  147.             $22, $01,   {; Channel C, modulator (OP1)}
  148.             $42, $18,
  149.             $62, $F0,
  150.             $82, $14,
  151.             $e2, $02,   {; ch.C}
  152.             $C2, $0A,
  153.             $25, $01,   {; Channel C, carrier (OP2)}
  154.             $65, $F0,
  155.             $85, $13,
  156.             $e5, $00,   {; 31, ch C}
  157.  
  158.  
  159. {; --- NOISE CHANNEL SETUP. Channel 8 used for noise (vol reg=54h)}
  160.  
  161.             $34, $21,   {; ch.8 AM/VIB/EG, operator 2}
  162.             $74, $99,   {; ch.8 attack/decay rate, operator 2}
  163.             $94, $00,   {; ch.8 sustain/release level,operator 2}
  164.             $F4, $00,   {; ch.8 waveform select, operator 2}
  165.             $bd, $28);  {; SNARE drum ON, 6 melodic/4 percussion}
  166.  
  167.  
  168. var a,b: byte;
  169. i:integer;
  170. begin
  171.  
  172. for i:= 0 to 35 do begin
  173. a:= proper_init [i+i];
  174. b:= proper_init [i+i+1];
  175. Write_Adlib (A,b);
  176. end;
  177.  
  178. end;
  179.  
  180. procedure psg_init;
  181. begin
  182.                 noise_enabled := 0;
  183.                 noise_freq    := 0;
  184.                 mixer_reg     := $3f;
  185.         adlib_a_vol   := $3f;
  186.         adlib_b_vol   := $3f;
  187.         adlib_c_vol   := $3f;
  188.         noise_vol     := $3f;
  189.         ch_a_freq     := $c8;
  190.         ch_b_freq     := $c8;
  191.         ch_c_freq     := $c8;
  192.         ch_a_flsb     := $98;
  193.         ch_a_fmsb     := $11;
  194.         ch_b_flsb     := $98;
  195.         ch_b_fmsb     := $11;
  196.         ch_c_flsb     := $98;
  197.         ch_c_fmsb     := $11;
  198. end;
  199.  
  200.  
  201. procedure marat_xlat2;near;assembler;
  202. asm
  203.         mov bx, ax
  204.         or  bx, bx
  205.         je  @@1
  206.  
  207.         cmp bx, 17
  208.         jbe @@6
  209.  
  210.         cmp bx, 35
  211.         jbe @@5
  212.  
  213.         mov ax, 0CACEh
  214.         mov dx, 00023h
  215.         div bx
  216.         jmp @@3
  217.  
  218. @@6:        mov ax, 3ffh
  219.         mov bl, 7
  220.         jmp @@2
  221.  
  222. @@1:        mov ax, 1
  223.  
  224. @@3:        xor bl, bl
  225. @@4:        cmp ax, 400h
  226.         jbe @@2
  227.         shr ax, 1
  228.         inc bl
  229.         jmp @@4
  230.  
  231. @@2:        and ah, 3o
  232.         and bl, 7o
  233.         shl bl, 1
  234.         shl bl, 1
  235.         or  ah, bl
  236.         retn
  237.  
  238. @@5:        jmp @@2
  239. end;
  240.  
  241. procedure do_all_writes;assembler;
  242. var tmp:byte;
  243. begin
  244.  
  245.         Write_Adlib(ch_a_freq_lsb, ch_a_flsb);
  246.         Write_Adlib(ch_a_freq_msb, ch_a_fmsb);
  247.                 Write_Adlib(ch_b_freq_lsb, ch_b_flsb);
  248.         Write_Adlib(ch_b_freq_msb, ch_b_fmsb);
  249.                 Write_Adlib(ch_c_freq_lsb, ch_c_flsb);
  250.                 Write_Adlib(ch_c_freq_msb, ch_c_fmsb);
  251.  
  252.                 tmp := noise_vol;
  253.                 if tmp < $1f then tmp := $1f;
  254.                 if noise_enabled = 0 then tmp := $3f;
  255.                
  256.                 Write_Adlib (snare_volume, tmp);
  257.                 noise_enabled := 0;
  258. end;
  259.  
  260. procedure write_psg;//assembler;
  261. label _flow_cha, _fhigh_cha, _flow_chb, _fhigh_chb, _flow_chc,
  262. _fhigh_chc, _done, _mixer, _vol_ch_a, _vol_ch_b, _vol_ch_c;
  263.  
  264. begin
  265.  
  266. asm
  267.                 push eax
  268.         push ebx
  269.         push ecx
  270.         push edx
  271.                 not cl
  272.         mov byte ptr  [psg_value], cl
  273. end;
  274.  
  275. //    psg_value := not psg_value;
  276.  
  277. case PSG_RegNum of
  278. 0:  goto _flow_cha;
  279. 1:  goto _fhigh_cha;
  280. 2:  goto _flow_chb;
  281. 3:  goto _fhigh_chb;
  282. 4:  goto _flow_chc;
  283. 5:  goto _fhigh_chc;
  284. 6:  goto _done;
  285. 7:  goto _mixer;
  286. 8:  goto _vol_ch_a;
  287. 9:  goto _vol_ch_b;
  288. 10: goto _vol_ch_c;
  289. 11: goto _done;
  290. 12: goto _done;
  291. 13: goto _done;
  292. 14: goto _done;
  293. 15: goto _done;
  294.  
  295. end;
  296.  
  297. _flow_cha:
  298. asm
  299.         mov byte ptr ch_a_freq, cl
  300.                 mov     ax, ch_a_freq
  301.         call    marat_xlat2
  302.                 mov     ch_a_flsb, al
  303.                 mov     al, ch_a_fmsb
  304.                 and     al, 40o
  305.                 or      ah, al
  306.                 mov     ch_a_fmsb, ah
  307.                 mov     al, ch_a_freq_lsb
  308.         mov ah, ch_a_flsb
  309.         call    Wrt_Adlib
  310.         mov al, ch_a_freq_msb
  311.         mov ah, ch_a_fmsb
  312.         call    Wrt_Adlib
  313.         end; goto _done;
  314.  
  315. _fhigh_cha:    
  316. asm
  317.         and cl, 0Fh
  318.         mov byte ptr [ch_a_freq+1], cl 
  319.                 mov     ax, word ptr [ch_a_freq]    
  320.         call    marat_xlat2
  321.                 mov     byte ptr  [ch_a_flsb], al    
  322.                 mov     al, byte ptr [ch_a_fmsb]   
  323.                 and     al, 40o                        
  324.                 or      ah, al             
  325.                 mov     byte ptr [ch_a_fmsb], ah    
  326.                 mov     al, ch_a_freq_lsb              
  327.         mov ah, byte ptr  [ch_a_flsb]
  328.         call    Wrt_Adlib          
  329.         mov al, ch_a_freq_msb
  330.         mov ah, byte ptr [ch_a_fmsb]
  331.         call    Wrt_Adlib          
  332.         end; goto _done;
  333. _flow_chb:    
  334. asm
  335.         mov     byte ptr  [ch_b_freq], cl    
  336.                 mov     ax, word ptr [ch_b_freq]    
  337.         call    marat_xlat2
  338.                 mov     byte ptr  [ch_b_flsb], al    
  339.                 mov     al, byte ptr [ch_b_fmsb]
  340.                 and     al, 40o                        
  341.                 or      ah, al
  342.                 mov     byte ptr  [ch_b_fmsb], ah    
  343.                 mov     al, ch_b_freq_lsb              
  344.                 mov     ah, byte ptr [ch_b_flsb]
  345.         call    Wrt_Adlib          
  346.                 mov     al, ch_b_freq_msb
  347.                 mov     ah, byte ptr [ch_b_fmsb]
  348.         call    Wrt_Adlib          
  349.         end; goto _done;
  350.  
  351. _fhigh_chb:
  352. asm
  353.         and cl, 0Fh
  354.                 mov     byte ptr  [ch_b_freq+1], cl  
  355.                 mov     ax, word ptr [ch_b_freq]    
  356.         call    marat_xlat2
  357.                 mov     byte ptr  [ch_b_flsb], al    
  358.                 mov     al, byte ptr  [ch_b_fmsb]
  359.                 and     al, 40o                        
  360.                 or      ah, al
  361.                 mov     byte ptr [ch_b_fmsb], ah    
  362.                 mov     al, ch_b_freq_lsb              
  363.                 mov     ah, byte ptr [ch_b_flsb]
  364.         call    Wrt_Adlib          
  365.                 mov     al, ch_b_freq_msb
  366.                 mov     ah, byte ptr [ch_b_fmsb]
  367.         call    Wrt_Adlib          
  368.         end; goto _done;
  369. _flow_chc:
  370. asm
  371.         mov     byte ptr  [ch_c_freq], cl    
  372.                 mov     ax, word ptr  [ch_c_freq]    
  373.         call    marat_xlat2
  374.                 mov     byte ptr  [ch_c_flsb], al    
  375.                 mov     al, byte ptr  [ch_c_fmsb]
  376.                 and     al, 40o                        
  377.                 or      ah, al
  378.                 mov     byte ptr  [ch_c_fmsb], ah    
  379.                 mov     al, ch_c_freq_lsb              
  380.                 mov     ah, byte ptr  [ch_c_flsb]
  381.         call    Wrt_Adlib          
  382.                 mov     al, ch_c_freq_msb
  383.                 mov     ah, byte ptr  [ch_c_fmsb]
  384.         call    Wrt_Adlib          
  385.         end; goto _done;
  386. _fhigh_chc:
  387. asm
  388.         and cl, 0Fh
  389.                 mov     byte ptr  [ch_c_freq+1], cl  
  390.                 mov     ax, word ptr  [ch_c_freq]    
  391.         call    marat_xlat2
  392.                 mov     byte ptr  [ch_c_flsb], al    
  393.                 mov     al, byte ptr  [ch_c_fmsb]
  394.                 and     al, 40o                        
  395.                 or      ah, al
  396.                 mov     byte ptr  [ch_c_fmsb], ah    
  397.                 mov     al, ch_c_freq_lsb              
  398.                 mov     ah, byte ptr  [ch_c_flsb]
  399.         call    Wrt_Adlib          
  400.                 mov     al, ch_c_freq_msb
  401.                 mov     ah, byte ptr  [ch_c_fmsb]
  402.         call    Wrt_Adlib          
  403.         end; goto _done;
  404.  
  405. _vol_ch_a:
  406. asm
  407.         mov al, ch_a_volume
  408.         mov ah, cl
  409.  
  410.         and ah, 1Fh
  411.         cmp ah, 16
  412.         jne @@no_env_a
  413.         mov ah, 0Bh
  414.  
  415. @@no_env_a: mov byte ptr  [psg_volume_a], ah
  416.         not ah
  417.         shl ah, 1
  418.         shl ah, 1
  419.         and ah, 00111100b
  420.         mov byte ptr  [adlib_a_vol], ah
  421.         call    Wrt_Adlib
  422.  
  423.         test    byte ptr  [mixer_reg], 8
  424.         jne @@no_noise_A
  425.  
  426.         mov ah, byte ptr  [adlib_a_vol]
  427.         mov byte ptr  [noise_vol], ah
  428.  
  429.         cmp byte ptr  [noise_enabled], 0
  430.         je  @@no_noise_A
  431.  
  432.         mov al, snare_volume
  433.         call    wrt_adlib
  434.  
  435. @@no_noise_A:  
  436.         end; goto _done;
  437. _vol_ch_b:
  438. asm
  439.         mov al, ch_b_volume
  440.         mov ah, cl
  441.         and ah, 1Fh
  442.         cmp ah, 16
  443.         jne @@no_env_b
  444.         mov ah, 0Bh
  445.  
  446. @@no_env_b: mov byte ptr  [psg_volume_b], ah
  447.         not ah
  448.         shl ah, 1
  449.         shl ah, 1
  450.         and ah, 00111100b
  451.         mov byte ptr  [adlib_b_vol], ah
  452.         call    Wrt_Adlib
  453.  
  454.         test    byte ptr  [mixer_reg], 16
  455.         jne @@no_noise_B
  456.  
  457.         mov ah, byte ptr  [adlib_b_vol]
  458.         mov byte ptr  [noise_vol], ah
  459.  
  460.         cmp byte ptr  [noise_enabled], 0
  461.         je  @@no_noise_B
  462.  
  463.         mov al, snare_volume
  464.         call    wrt_adlib
  465.  
  466. @@no_noise_B:   end; goto _done;
  467.  
  468. _vol_ch_c:
  469. asm
  470.         mov al, ch_c_volume
  471.         mov ah, cl
  472.         and ah, 1Fh
  473.         cmp ah, 16
  474.         jne @@no_env_c
  475.         mov ah, 0Bh
  476.  
  477. @@no_env_c: mov byte ptr  [psg_volume_c], ah
  478.         not ah
  479.         shl ah, 1
  480.         shl ah, 1
  481.         and ah, 00111100b
  482.         mov byte ptr  [adlib_c_vol], ah
  483.         call    Wrt_Adlib
  484.  
  485.         test    byte ptr  [mixer_reg], 32
  486.         jne @@no_noise_C
  487.  
  488.         mov ah, byte ptr  [adlib_c_vol]
  489.         mov byte ptr  [noise_vol], ah
  490.  
  491.         cmp byte ptr  [noise_enabled], 0
  492.         je  @@no_noise_C
  493.  
  494.         mov al, snare_volume
  495.         call    wrt_adlib
  496.  
  497. @@no_noise_C:   end; goto _done;
  498.  
  499.  
  500. _mixer:
  501. asm
  502.         mov al, cl
  503.         mov byte ptr  [mixer_reg], al
  504.         not al
  505.  
  506.         shr al, 1
  507.         shr al, 1
  508.         shr al, 1
  509.         and al, 7o
  510.         or  al, al
  511.         je  @@no_noise
  512.  
  513.         test    al, 4
  514.         jne @@noise_c
  515.         test    al, 2
  516.         jne @@noise_b
  517.  
  518. @@noise_a:  mov al, byte ptr  [adlib_a_vol]
  519.         jmp @@noise_done
  520.  
  521. @@noise_c:  mov al, byte ptr  [adlib_c_vol]
  522.         jmp @@noise_done
  523.  
  524. @@noise_b:  mov al, byte ptr  [adlib_b_vol]
  525.  
  526. @@noise_done:   mov byte ptr  [noise_vol], al
  527.         mov al, 1
  528.         jmp @@noise
  529.  
  530. @@no_noise: xor al, al
  531. @@noise:    mov byte ptr  [noise_enabled], al
  532.  
  533.         test    cl, 1
  534.         je  @@ch_a_on
  535. @@ch_a_off:     and     byte ptr  [ch_a_fmsb], 11011111b
  536.                 jmp     @@ch_b_next
  537. @@ch_a_on:      or      byte ptr  [ch_a_fmsb], 00100000b
  538.  
  539. @@ch_b_next:    test    cl, 2
  540.                 je      @@ch_b_on
  541. @@ch_b_off:     and     byte ptr  [ch_b_fmsb], 11011111b
  542.                 jmp     @@ch_c_next
  543. @@ch_b_on:      or      byte ptr  [ch_b_fmsb], 00100000b
  544.  
  545. @@ch_c_next:    test    cl, 4
  546.                 je      @@ch_c_on
  547. @@ch_c_off:     and     byte ptr  [ch_c_fmsb], 11011111b
  548.                 jmp     @@ch_write
  549. @@ch_c_on:      or      byte ptr  [ch_c_fmsb], 00100000b
  550.  
  551. @@ch_write:     call    do_all_writes
  552.         end; //goto _done;
  553.  
  554. _done:
  555. asm
  556.                 pop edx
  557.         pop ecx
  558.         pop ebx
  559.         pop eax
  560. end; exit;
  561.  
  562.  
  563. end;
  564.  
  565. var a:integer = 0;
  566.  
  567. procedure TimeCallBack(uTimerID, uMessage, dwUser, dw1, dw2:longint);stdcall;
  568. var b1,b2:byte;
  569. begin
  570. //write_hex (J[a+54]);
  571. b1 := J [a+54];
  572. inc (a);
  573. b2 := J [a+54];
  574. inc (a);
  575.  
  576.  
  577. if (b1 = $ff) and (b2 = $ff) then begin
  578. a := 0; exit;end;
  579.  
  580.  
  581. asm
  582.  
  583.  
  584. MOV     AH, b1// [ESI]
  585.                 //inc esi
  586. MOV     AL, b2// [ESI]
  587.         //inc esi
  588.  
  589.                 MOV     PSG_REGNUM, AH
  590. //inc psg_regnum;
  591.                 MOV     CL, AL
  592. CALL    WRITE_PSG
  593.  
  594. //                inc si
  595. //      inc si
  596.  
  597. end;
  598. //a := a + 4;
  599. inc (a);
  600. inc (a);
  601. //kick;
  602. end;
  603.  
  604.  
  605. type str = array [0..256] of char;
  606. var P:pchar;
  607. sstring : ^str;
  608.  
  609.  
  610.  
  611. { ============================ MAIN MODULE ================================= }
  612. label loop;
  613.  
  614. var f:char;key,i:integer;
  615. FTimerHandle:dword;
  616.  
  617. //var hDevice:thandle;
  618. //prtdata : pointer;
  619.  
  620. begin
  621.  
  622. asm
  623.      mov dx, $E22
  624.      mov ax,$1F
  625.    out dx,al
  626. end;
  627.  
  628.  
  629. J := GetFile ('blade1.psg');
  630.  
  631. synt_init;
  632.  
  633. PSG_INIT;
  634.  
  635. FTimerHandle := timeSetEvent(2,0,@TimeCallBack,0,1);
  636.  
  637. loop:  Key := NeedKeyboard;
  638.  
  639. if Key <> 27 then goto loop;
  640.  
  641. timeKillevent(FTimerHandle);
  642.  
  643. Init_Adlib;
  644.  
  645. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement