Advertisement
Guest User

Untitled

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