home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 4: The Falcon Archive / nf_archive_four_v1.0.iso / ARCHIVE / WORK / MSX / mp204a.ZIP / MP2AD / MP2.S < prev    next >
Text File  |  1996-04-21  |  9KB  |  468 lines

  1. ; mp2 - DSP 44.1kHz Stereo MPEG2 Audio decoder
  2. ; Copyright (C) 1996 Fredrik Noring
  3. ;
  4. ; This program is free software; you can redistribute it and/or modify
  5. ; it under the terms of the GNU General Public License as published by
  6. ; the Free Software Foundation; either version 2 of the License, or
  7. ; (at your option) any later version.
  8. ;
  9. ; This program is distributed in the hope that it will be useful,
  10. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ; GNU General Public License for more details.
  13. ;
  14. ; You should have received a copy of the GNU General Public License
  15. ; along with this program; if not, write to the Free Software
  16. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ;
  18. ; Fredrik Noring
  19. ; noring@lysator.liu.se
  20.  
  21. BUFFER_SIZE        equ    128*1024
  22.  
  23. INFO_PADDING        equ    9
  24. INFO_MODE        equ    6
  25. INFO_EXTMODE        equ    4
  26.  
  27. MPG_MD_STEREO    equ    0
  28. MPG_MD_JOINT_STEREO    equ    1
  29. MPG_MD_DUAL_CHANNEL    equ    2
  30. MPG_MD_MONO        equ    3
  31.  
  32.     include macrodsp.s
  33.     include macrosnd.s
  34.  
  35.     section    text
  36.  
  37. soc    move.l    4(sp),a5
  38.     move.l    a5,bp
  39.  
  40.     move.l    $0c(a5),d0
  41.     add.l    $14(a5),d0
  42.     add.l    $1c(a5),d0
  43.     add.l    #$1100,d0
  44.     move.l    a5,d1
  45.     add.l    d0,d1
  46.     and.l    #-2,d1
  47.  
  48.     move.l    d1,sp
  49.     move.l    d0,-(sp)
  50.     move.l    a5,-(sp)
  51.     move.w    d0,-(sp)
  52.     move.w    #$4a,-(sp)
  53.     trap    #1
  54.     lea    12(sp),sp
  55.  
  56.     lea    info_text0,a0
  57.     bsr    text
  58.  
  59.     move.l    bp,a0
  60.     lea    128(a0),a0
  61.     moveq    #0,d0
  62.     move.b    (a0)+,d0
  63.     subq.w    #1,d0
  64.     bmi.s    .no_command
  65.     lea    command_copy,a1
  66. .copy    move.b    (a0)+,d1
  67.     cmp.b    #' ',d1
  68.     beq.s    .no_space
  69.     move.b    d1,(a1)+
  70. .no_space    dbra    d0,.copy
  71.     clr.b    (a1)+
  72.     move.l    #command_copy,filename
  73. .no_command
  74.     clr.l    -(sp)
  75.     move.w    #$20,-(sp)
  76.     trap    #1        * Super
  77.     addq.w    #6,sp
  78.     move.l    d0,old_sp
  79.  
  80.     bsr    start
  81.  
  82.     move.l    old_sp,-(sp)
  83.     move.w    #$20,-(sp)
  84.     trap    #1        * Super
  85.     addq.w    #6,sp
  86.  
  87.     clr.w    -(sp)
  88.     trap    #1        * Pterm0
  89.  
  90. start    clr.w    mute
  91.     clr.w    nosync
  92.  
  93. .clear_keys    bsr    status_check
  94.     tst.w    d0
  95.     bne.s    .clear_keys
  96.  
  97.     bsr    fopen
  98.     tst.w    d0
  99.     bmi.s    .no_file
  100.     bsr    restart
  101.  
  102.     bsr    DSP_init
  103.     bsr    main
  104.     bsr    DSP_exit
  105.     bsr    fclose
  106.     rts
  107. .no_file    lea    error_file1,a0
  108.     bsr    text
  109.     move.l    filename,a0
  110.     bsr    text
  111.     lea    error_file2,a0
  112.     bsr    error
  113.     rts
  114.  
  115. error    move.l    a0,-(sp)
  116.     move.w    #9,-(sp)
  117.     trap    #1
  118.     addq.l    #6,sp
  119.     move.w    #8,-(sp)
  120.     trap    #1
  121.     addq.w    #2,sp
  122.     rts
  123.  
  124. text    move.l    a0,-(sp)
  125.     move.w    #9,-(sp)
  126.     trap    #1
  127.     addq.l    #6,sp
  128.     rts
  129.  
  130. info    lea    info_text1,a0
  131.     bsr    text
  132.     move.l    filename,a0
  133.     bsr    text
  134.     lea    info_text2,a0
  135.     bsr    text
  136.     move.l    bitrate,d0
  137.     bsr    display_number
  138.     lea    info_text3,a0
  139.     bsr    text
  140.     rts
  141.  
  142. DSP_init    bsr    mute_on
  143.     setmode #1
  144.     sndstatus #1
  145.     settracks #0,#0
  146.     setmontracks #0
  147.     dsptristate #1,#1
  148.     soundcmd #4,#%10
  149.     Dsp_ExecProg    #DSPbin.start,#(DSPbin.end-DSPbin.start)/3,#'NC'
  150.  
  151.     lea    DSPtables.start,a0
  152.     move.w    #(DSPtables.end-DSPtables.start)/4-1,d1
  153. .send_tables    move.l    (a0)+,d0
  154.     bsr    DSP.host_send
  155.     dbra    d1,.send_tables
  156.  
  157.     * Get DSP sync
  158.     bsr    DSP.host_get
  159.  
  160.     devconnect #0,#%0000,#1,#1,#1
  161.     devconnect #1,#%1000,#1,#1,#1   ; Connect DSPXMIT with DAC
  162.     devconnect #2,#%0000,#1,#1,#1
  163.     devconnect #3,#%0000,#1,#1,#1
  164.     bsr    mute_off
  165.     rts
  166. gogo    Dsp_SetVectors #DSP_sync,#0
  167.     rts
  168. DSP_exit    bsr    mute_on
  169.     Dsp_RemoveInterrupts #3
  170.     sndstatus #1
  171.     bsr    mute_off
  172.     rts
  173.  
  174. mute_on    tst.w    mute
  175.     bne.s    .muted
  176.     move.w    #-1,mute
  177.     soundcmd #0,#-1
  178.     move.w   d0,ltatten
  179.     soundcmd #1,#-1
  180.     move.w   d0,rtatten
  181.     soundcmd #2,#-1
  182.     move.w   d0,ltgain
  183.     soundcmd #3,#-1
  184.     move.w   d0,rtgain
  185. .muted    soundcmd #0,#$00f0
  186.     soundcmd #1,#$00f0
  187.     soundcmd #2,#$0000
  188.     soundcmd #3,#$0000
  189.     rts
  190. mute_off    tst.w    mute
  191.     beq.s    .not_muted
  192.     clr.w    mute
  193.     soundcmd #0,ltatten
  194.     soundcmd #1,rtatten
  195.     soundcmd #2,ltgain
  196.     soundcmd #3,rtgain
  197. .not_muted    rts
  198.  
  199. main    lea    buffer1,a0
  200.     move.l    #BUFFER_SIZE,d0
  201.     bsr    fread
  202.  
  203.     bsr    decode
  204.     bsr    info
  205.  
  206.     bsr    gogo
  207. .again
  208. .next1    bsr    status_check
  209.     tst.w    d0
  210.     bne.s    .exit
  211.     move.l    stream_bytes,a0
  212.     cmp.l    #buffer1,a0
  213.     blo.s    .next1
  214.     cmp.l    #buffer1+1024,a0
  215.     bhi.s    .next1
  216.     lea    buffer2,a0
  217.     move.l    #BUFFER_SIZE,d0
  218.     bsr    fread
  219.  
  220. .next2    bsr    status_check
  221.     tst.w    d0
  222.     bne.s    .exit
  223.     move.l    stream_bytes,a0
  224.     cmp.l    #buffer2,a0
  225.     blo.s    .next2
  226.     cmp.l    #buffer2+1024,a0
  227.     bhi.s    .next2
  228.     lea    buffer1,a0
  229.     move.l    #BUFFER_SIZE,d0
  230.     bsr    fread
  231.  
  232.     bra.s    .again
  233. .exit    rts
  234.  
  235. status_check    move.w    #11,-(sp)
  236.     trap    #1
  237.     addq.w    #2,sp
  238.     tst.w    d0
  239.     beq.s    .no
  240.     move.w    #8,-(sp)
  241.     trap    #1
  242.     addq.w    #2,sp
  243. .yes    moveq    #1,d0
  244.     rts
  245. .no    moveq    #0,d0
  246.     move.w    nosync,d0
  247.     rts
  248.  
  249. DSP.host_get    btst.b    #0,$ffffa202.w
  250.     beq.s    DSP.host_get
  251.     move.l    $ffffa204.w,d0
  252.     rts
  253. DSP.host_send    btst.b    #1,$ffffa202.w
  254.     beq.s    DSP.host_send
  255.     move.l    d0,$ffffa204.w
  256.     rts
  257. DSP.stream    btst.b    #1,$ffffa202.w
  258.     beq.s    DSP.host_send
  259.     moveq    #0,d0
  260.     move.w    d1,d2
  261.     asr.w    #3,d2
  262.     subq.w    #1,d2
  263.     bmi.s    .no1
  264. .1    rept    8
  265.     move.b    (a0)+,d0
  266.     move.l    d0,$ffffa204.w
  267.     endr
  268.     dbra    d2,.1
  269. .no1    and.w    #$7,d1
  270.     subq.w    #1,d1
  271.     bmi.s    .no2
  272. .2    move.b    (a0)+,d0
  273.     move.l    d0,$ffffa204.w
  274.     dbra    d1,.2
  275. .no2    rts
  276.  
  277. DSP_sync    *not.l    $ffff9800.w
  278.     tst.w    nosync
  279.     bne    .nosync
  280.     movem.l    d0-a6,-(sp)
  281.  
  282.     move.l    sblimit,d0
  283.     bsr    DSP.host_send
  284.     move.l    jsbound,d0
  285.     bsr    DSP.host_send
  286.     move.l    frame_size,d0
  287.     subq.l    #4+3,d0
  288.     bsr    DSP.host_send
  289.  
  290.     move.l    frame_size,d1
  291.     move.l    stream_bytes,a0
  292.     cmp.l    stream_read,d1
  293.     bls.s    .ok2
  294.     move.l    stream_read,d1
  295.     bsr    DSP.stream
  296.     move.l    frame_size,d1
  297.     sub.l    stream_read,d1
  298.     bsr    restart
  299.     move.l    stream_bytes,a0
  300.     sub.l    d1,stream_read
  301.     bsr    DSP.stream
  302.     move.l    a0,stream_bytes
  303.     bra.s    .resume
  304. .ok2    sub.l    d1,stream_read
  305.     bsr    DSP.stream
  306.     move.l    a0,stream_bytes
  307.  
  308. .resume    bsr    decode
  309.     movem.l    (sp)+,d0-a6
  310. .nosync    *not.l    $ffff9800.w
  311.     rts
  312.  
  313. decode    move.l    #2,stereo
  314.  
  315.     moveq    #0,d1
  316.     moveq    #4-1,d2
  317.     move.l    stream_read,d3
  318.     move.l    stream_bytes,a0
  319. .next    tst.l    d3
  320.     bne    .ok1
  321.     lea    buffer1,a0
  322.     move.l    #2*BUFFER_SIZE,d3
  323. .ok1    lsl.l    #8,d1
  324.     move.b    (a0)+,d1
  325.     subq.l    #1,d3
  326.     dbra    d2,.next
  327.     move.l    d1,d2
  328.     and.l    #$fffff,d1
  329.     swap.w    d2
  330.     lsr.w    #4,d2
  331.     and.l    #$fff,d2
  332.     cmp.w    #$fff,d2
  333.     bne    .no_sync
  334.  
  335.     move.l    d1,d2
  336.     moveq    #12,d3
  337.     lsr.l    d3,d2
  338.     and.l    #$f,d2
  339.     lea    bitrates,a0
  340.     move.w    (a0,d2.w*2),d2
  341.     move.l    d2,bitrate
  342.  
  343.     mulu.l    #144*1000,d2
  344.     divu.l    #44100,d2
  345.     btst.l    #INFO_PADDING,d1
  346.     beq.s    .no_padding
  347.     addq.l    #1,d2
  348. .no_padding    move.l    d2,frame_size
  349.  
  350.     move.l    bitrate,d2
  351.     divu.l    stereo,d2
  352.     move.l    #30,sblimit    ; table 2
  353.     cmp.l    #56,d2
  354.     blo.s    .not
  355.     cmp.l    #80,d2
  356.     bhi.s    .not
  357.     move.l    #27,sblimit    ; table 3
  358. .not
  359.     move.l    sblimit,jsbound
  360.     move.l    d1,d2
  361.     lsr.l    #INFO_MODE,d2
  362.     and.l    #$3,d2
  363.     cmp.b    #MPG_MD_JOINT_STEREO,d2
  364.     bne.s    .nix
  365.     move.l    d1,d2
  366.     lsr.l    #INFO_EXTMODE,d2
  367.     and.l    #$3,d2
  368.     asl.l    #2,d2
  369.     addq.l    #4,d2
  370.     move.l    d2,jsbound
  371. .nix    rts
  372. .no_sync    move.w    #1,nosync
  373.     rts
  374.  
  375. restart    move.l    #buffer1,stream_bytes
  376.     move.l    #2*BUFFER_SIZE,stream_read
  377.     rts
  378.  
  379. fopen    clr.w    -(sp)
  380.     move.l    filename,-(sp)
  381.     move.w    #61,-(sp)
  382.     trap    #1
  383.     addq.l    #8,sp
  384.     move.w    d0,fp
  385.     rts
  386. fread    move.l    a0,-(sp)
  387.     move.l    d0,-(sp)
  388.     move.w    fp,-(sp)
  389.     move.w    #63,-(sp)
  390.     trap    #1
  391.     lea    12(sp),sp
  392.     rts
  393. fclose    move.w    fp,-(sp)
  394.     move.w    #62,-(sp)
  395.     trap    #1
  396.     addq.l    #4,sp
  397.     rts
  398.  
  399. * Positive numbers only!
  400. display_number
  401.     lea    buffer+32,a0
  402.     clr.b    -(a0)
  403. .next    clr.l    d1
  404.     divu.l    #10,d1:d0
  405.     add.b    #'0',d1
  406.     move.b    d1,-(a0)
  407.     tst.w    d0
  408.     bne.s    .next
  409.     bsr    text
  410.     rts
  411.  
  412.     section    data
  413. DSPbin.start
  414.     incbin    mp2.dsp    ; DSP binary file
  415. DSPbin.end    even
  416. DSPtables.start
  417.     incbin    dsp.dat
  418. DSPtables.end
  419.  
  420. bitrates    dc.w    0,32,48,56,64,80,96,112,128,160,192,224,256,320,384
  421.  
  422. info_text0    dc.b    'mp2 version 0.4alpha, Copyright (C) 1996 Fredrik Noring',10,13
  423.     dc.b    'DSP 44.1kHz Stereo MPEG2 Audio decoder (external clock)',10,13
  424. ;    dc.b    'DSP 44.1kHz Stereo MPEG2 Audio decoder',10,13
  425.     dc.b    'This program is free software and distributed under',10,13
  426.     dc.b    'the conditions of the GNU General Public License.',10,13,10,13,0
  427. info_text1
  428. ;    dc.b    '(low sound quality due to linear',10,13
  429. ;    dc.b    ' interpolation for 44.1kHz to 49.17kHz conversion)',10,13
  430.     dc.b    'Now playing ',0
  431. info_text2    dc.b    ' (',0
  432. info_text3    dc.b    'kbit/sec)',10,13,0
  433. error_file1    dc.b    'mp2.ttp <filename>',10,13
  434.     dc.b    'Error: File "',0
  435. error_file2    dc.b    '" not found.',10,13,0
  436.  
  437.     section    bss
  438. fp    ds.w    1
  439. bp    ds.l    1
  440. old_sp    ds.l    1
  441.  
  442. nosync    ds.w    1
  443. sblimit    ds.l    1
  444. jsbound    ds.l    1
  445. stereo    ds.l    1
  446. frame_size    ds.l    1
  447. bitrate    ds.l    1
  448.  
  449. stream_read     ds.l    1
  450. stream_bytes    ds.l    1
  451. filename    ds.l    1
  452.  
  453. * Mute parameters
  454. mute    ds.w    1
  455. ltatten    ds.w    1
  456. rtatten    ds.w    1
  457. ltgain    ds.w    1
  458. rtgain    ds.w    1
  459.  
  460. buffer    ds.b    32
  461.  
  462. command_copy    ds.b    256
  463. buffer1    ds.b    BUFFER_SIZE
  464. buffer2    ds.b    BUFFER_SIZE
  465.     end
  466.  
  467.  
  468.