home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaDemoCD2.iso / ASCII / TEXTE / SCENE / AMIGA / Useful-Amiga / HardwareHacking / KEYWIN95.LHA / ATkeyboard / kbscan / kb-scan.asm next >
Encoding:
Assembly Source File  |  1996-12-26  |  7.3 KB  |  412 lines

  1.  
  2. ;This program is a very lame example of how to handle the Amiga.
  3. ;I use _LVODisable & idiotic loops instead of interrupts to control
  4. ;the paralellport & i'm not very proud of it but it works.
  5. ;This program is not supposed to be used very often so I think
  6. ;that's an excuse for not making it any better than this.
  7.  
  8.     incdir    LVO:
  9.     include    dos_lib.i
  10.     include    exec_lib.i
  11.     include cia_lib.i
  12.     include misc_lib.i
  13.     incdir    include:
  14.     include    exec/types.i
  15.     include    dos/dos.i
  16.     include    devices/timer.i
  17.     include    resources/misc.i
  18.     include    resources/cia.i
  19.     include    hardware/cia.i
  20.  
  21.  
  22.     ;link with amiga.lib
  23.     xref    _CreatePort
  24.     xref    _DeletePort
  25.     xref    _CreateExtIO
  26.     xref    _DeleteExtIO
  27.     xdef    _SysBase
  28.     xdef    _DOSBase
  29.  
  30. dat_out    equ    0
  31. clk_out    equ    1
  32. clk_in    equ    3
  33. dat_in    equ    2
  34.  
  35. ciaa_pra    equ    $bfe001
  36. ciaa_prb    equ    $BFE101
  37. ciab_pra    equ    $BFD000
  38. ciaa_ddra    equ    $bfe201
  39. ciaa_ddrb    equ    $BFE301
  40. ciab_ddra    equ    $BFD200
  41.  
  42.     STRUCTURE kb,0
  43.     APTR    msgport
  44.     APTR    ioreq
  45.     APTR    __usp
  46.     APTR    Misc_Base
  47.     APTR    dos_base
  48.     APTR    exec_base
  49.     APTR    out_hndl
  50.     APTR    arg_ptr
  51.     ULONG    arg_size
  52.     UWORD    _s_byte
  53.     LABEL    bss_size
  54.  
  55.     STRUCTURE T_REQ,IO_SIZE
  56.     ULONG    _TV_SECS
  57.     ULONG    _TV_MICRO
  58.     LABEL    _IOTV_SIZE
  59.  
  60.     section    start,code_p
  61.  
  62.     lea    data_zone,a4
  63.     move.l    sp,__usp(a4)
  64.     move.l    a0,arg_ptr(a4)
  65.     move.l    d0,arg_size(a4)
  66.     movea.l    4.w,a6
  67.     move.l    a6,exec_base(a4)
  68.  
  69.     lea    D_NAME(pc),a1
  70.     moveq    #0,d0
  71.     jsr    _LVOOldOpenLibrary(a6)
  72.     move.l    d0,dos_base(a4)
  73.     beq    free0
  74.  
  75.     movea.l    d0,a6
  76.     jsr    _LVOOutput(a6)
  77.     move.l    d0,out_hndl(a4)
  78.     move.l    d0,d1
  79.     lea    txt(pc),a1
  80.     move.l    a1,d2
  81.     moveq    #txt_end-txt,d3
  82.     jsr    _LVOWrite(a6)
  83.  
  84.     movea.l    arg_ptr(a4),a0
  85.     move.l    arg_size(a4),d0
  86.     cmpi.b    #$a,(a0)
  87.     beq    no_arg
  88.     clr.b    -1(a0,d0)
  89.  
  90.     movea.l    exec_base(a4),a6
  91.     lea    M_NAME(pc),a1
  92.     jsr    _LVOOpenResource(a6)
  93.     move.l    d0,Misc_Base(a4)
  94.     beq    free1
  95.  
  96.     movea.l    d0,a6
  97.     lea    Name(pc),a1
  98.     moveq    #MR_PARALLELPORT,d0
  99.     jsr    MR_AllocMiscResource(a6)
  100.     tst.l    d0
  101.     bne    free1
  102.  
  103.     lea    Name(pc),a1
  104.     moveq    #MR_PARALLELBITS,d0
  105.     jsr    MR_AllocMiscResource(a6)
  106.     tst.l    d0                ;success = 0
  107.     bne    free2
  108.  
  109. ;=========== Now we write the hardware addresses directly ===============
  110. ;---CIAA port B clk out =bit 1 dat out =bit 0 (a 1 bit means that pin is an out)-----
  111.     move.b    #%00000011,ciaa_ddrb
  112. ;----make BUSY, SEL, POUT lines "inputs" on CIAA port A---
  113.     andi.b    #$FF,ciab_ddra
  114.  
  115.     clr.l    -(sp)
  116.     clr.l    -(sp)
  117.     jsr    _CreatePort
  118.     move.l    d0,msgport(a4)
  119.     beq    free5
  120.     moveq    #_IOTV_SIZE,d1
  121.     move.l    d1,-(sp)
  122.     move.l    d0,-(sp)
  123.     jsr    _CreateExtIO
  124.     move.l    d0,ioreq(a4)
  125.     beq    free6
  126.     movea.l    d0,a1
  127.     lea    T_NAME(pc),a0
  128.     moveq    #UNIT_MICROHZ,d0
  129.     moveq    #0,d1
  130.     movea.l    exec_base(a4),a6
  131.     jsr    _LVOOpenDevice(a6)
  132.     tst.b    d0
  133.     bne    xit
  134.  
  135.     move.l    out_hndl(a4),d1
  136.     lea    txt4(pc),a1
  137.     move.l    a1,d2
  138.     moveq    #txt4_end-txt4,d3
  139.     movea.l    dos_base(a4),a6
  140.     jsr    _LVOWrite(a6)
  141.  
  142.     movea.l    exec_base(a4),a6
  143.     jsr    _LVODisable(a6)
  144.  
  145.     lea    ciaa_pra,a2
  146.     lea    ciaa_prb,a3
  147.     lea    bss_size(a4),a5
  148.     lea    400(a5),a6
  149.     move.l    a6,d3
  150.  
  151. .reset    move.b    #$ff,d0        ;reset
  152.     bsr.w    sendtoat
  153.     bsr    getkey        ;get $aa =selftest complete.
  154.     cmpi.b    #$fe,d0
  155.     beq    .reset
  156.  
  157.     move.b    #$ed,d0        ;setup
  158.     bsr    sendtoat
  159.     move.b    #%00000001,d0    ;scroll-lock on
  160.     bsr    sendtoat
  161.     lea    bss_size(a4),a5
  162.  
  163. .cp    pea    .cp(pc)
  164.  
  165. getkey:    moveq    #0,d0
  166.     moveq    #1,d2
  167.     moveq    #11,d7
  168.     move.b    #%00000011,(a3)        ;set clk & dat
  169.  
  170. .w_lo    btst    #CIAB_GAMEPORT0,(a2)    ;tst left mouse button
  171.     beq    save_file
  172.     btst    #clk_in,(a3)
  173.     bne    .w_lo
  174.     subq.b    #1,d7
  175.     beq.s    .end
  176.     cmpi.b    #10,d7
  177.     beq.s    .w_hi
  178.     move.b    (a3),d0        ;read port
  179.     cmpi.b    #1,d7
  180.     beq.s    .par_bit
  181.     btst    #dat_in,d0    ;check parity calc
  182.     beq.s    .no_par
  183.     bchg    #0,d2
  184. .no_par    roxr.b    #3,d0        ;shift databit to x
  185.     swap    d0
  186.     roxr.b    #1,d0        ;save databyte in hi-word
  187.     swap    d0
  188.  
  189. .w_hi    btst    #CIAB_GAMEPORT0,(a2)
  190.     beq    save_file
  191.     btst    #clk_in,(a3)
  192.     beq    .w_hi
  193.     bra    .w_lo
  194.  
  195. .par_bit
  196.     lsr.b    #2,d0
  197.     andi.b    #1,d0
  198.     cmp.b    d0,d2
  199.     beq    .w_hi
  200.  
  201.     move.b    #$fe,d0
  202.     pea    getkey(pc)
  203.     bra    sendtoat
  204.  
  205. .end    swap    d0
  206.     bsr    h2a
  207.     move.b    d4,(a5)+    ;save databyte
  208.     move.b    d5,(a5)+
  209.     move.b    #$20,(a5)+
  210.     cmpa.l    d3,a5
  211.     bge    save_file
  212.  
  213. .ew_hi    btst    #CIAB_GAMEPORT0,(a2)
  214.     beq    save_file
  215.     btst    #clk_in,(a3)
  216.     beq    .ew_hi
  217.     bclr    #clk_out,(a3)
  218.     rts
  219.  
  220. sendtoat:
  221.     move.b    d0,_s_byte(a4)    ;save byte
  222.     move.b    #%00000011,(a3)
  223.  
  224. .at_busy
  225.     btst    #CIAB_GAMEPORT0,(a2)
  226.     beq    save_file
  227.     move.b    (a3),d4
  228.     andi.b    #%00001100,d4
  229.     cmpi.b    #%00001100,d4
  230.     bne    .at_busy
  231.  
  232.     moveq    #7,d7
  233.     move.b    #%00000010,(a3)    ;set clk_out, clr dat_out
  234.     moveq    #1,d2
  235.     bsr.s    .chk_lo        ;send 8 bit data
  236.  
  237.     move.b    d2,d0
  238.     moveq    #0,d7
  239.     bsr.s    .chk_lo        ;send parity-bit
  240.  
  241.     moveq    #3,d0
  242.     moveq    #1,d7
  243.     bsr.s    .chk_lo        ;send stop-bit & ex-bit
  244.     bclr    #clk_out,(a3)    ;ack.
  245.  
  246.     moveq    #100,d5        ;delay 100us
  247.     bsr    delay
  248.  
  249.     bsr    getkey        ;receive ack.
  250.     cmpi.b    #$fe,d0
  251.     beq    .resend
  252.     rts
  253.  
  254. .resend    move.b    _s_byte(a4),d0
  255.     bra    sendtoat
  256.  
  257. .chk_lo    btst    #CIAB_GAMEPORT0,(a2)
  258.     beq    save_file
  259.     btst    #clk_in,(a3)
  260.     bne.s    .chk_lo
  261.  
  262.     move.b    d0,d1
  263.     ori.b    #%00000010,d1        ;set clk_out
  264.     move.b    d1,(a3)
  265.  
  266.     btst    #dat_out,d1
  267.     beq.s    .over
  268.     bchg    #0,d2
  269. .over    lsr.b    #1,d0
  270. .chk_hi    btst    #CIAB_GAMEPORT0,(a2)
  271.     beq    save_file
  272.     btst    #clk_in,(a3)
  273.     beq.s    .chk_hi
  274.     dbra    d7,.chk_lo
  275.     rts
  276.  
  277. delay    move.l    ioreq(a4),a1
  278.     move.l    d5,_TV_MICRO(a1)
  279.     moveq    #0,d5
  280.     move.l    d5,_TV_SECS(a1)
  281.     moveq    #TR_ADDREQUEST,d5
  282.     move.w    d5,IO_COMMAND(a1)
  283.     movea.l    exec_base(a4),a6
  284.     move.l    d0,-(sp)
  285.     jsr    _LVODoIO(a6)
  286.     tst.l    d0
  287.     bne    save_file
  288.     move.l    (sp)+,d0
  289.     rts
  290.  
  291. save_file
  292.     move.b    #%00000011,(a3)
  293.     clr.b    ciaa_ddrb
  294.     movea.l    exec_base(a4),a6
  295.     jsr    _LVOEnable(a6)
  296.  
  297.     move.l    dos_base(a4),a6
  298.     move.l    #MODE_NEWFILE,d2
  299.     move.l    arg_ptr(a4),d1
  300.     jsr    _LVOOpen(a6)
  301.     move.l    d0,d6
  302.     beq    no_file
  303.     move.l    d0,d1
  304.     move.l    #buffer,d2
  305.     move.l    a5,d3
  306.     sub.l    d2,d3
  307.     jsr    _LVOWrite(a6)
  308.     move.l    d6,d1
  309.     jsr    _LVOClose(a6)
  310.  
  311.     move.l    out_hndl(a4),d1
  312.     lea    txt1(pc),a1
  313.     move.l    a1,d2
  314.     moveq    #txt1_end-txt1,d3
  315.     jsr    _LVOWrite(a6)
  316.  
  317. p_file    move.l    out_hndl(a4),d1
  318.     move.l    arg_ptr(a4),d2
  319.     move.l    arg_size(a4),d3
  320.     jsr    _LVOWrite(a6)
  321.  
  322. ;=====Free resources (parallel port) and exit=====
  323. ; Now free the hardware for someone else to use
  324. xit    move.l    out_hndl(a4),d1
  325.     lea    txt_cr(pc),a0
  326.     move.l    a0,d2
  327.     moveq    #txt_cr_end-txt_cr,d3
  328.     move.l    dos_base(a4),a6
  329.     jsr    _LVOWrite(a6)
  330.  
  331.     move.l    ioreq(a4),a1
  332.     movea.l    exec_base(a4),a6
  333.     jsr    _LVOCloseDevice(a6)
  334. free6    move.l    ioreq(a4),-(sp)
  335.     jsr    _DeleteExtIO
  336. free5    move.l    msgport(a4),-(sp)
  337.     jsr    _DeletePort
  338.  
  339. free3    moveq    #MR_PARALLELBITS,d0    ;---Free MR_PARALLELBITS
  340.     movea.l    Misc_Base(a4),a6
  341.     jsr    MR_FreeMiscResource(a6)
  342.  
  343. free2    moveq    #MR_PARALLELPORT,d0    ;---Free MR_PARALLELPORT
  344.     movea.l    Misc_Base(a4),a6
  345.     jsr    MR_FreeMiscResource(a6)
  346.  
  347. free1    move.l    dos_base(a4),a1
  348.     move.l    exec_base(a4),a6
  349.     jsr    _LVOCloseLibrary(a6)
  350. free0    moveq    #0,d0
  351.     move.l    __usp(a4),a7
  352.     rts
  353.  
  354. no_file    move.l    out_hndl(a4),d1
  355.     lea    txt2(pc),a1
  356.     move.l    a1,d2
  357.     moveq    #txt2_end-txt2,d3
  358.     pea    p_file(pc)
  359.     jmp    _LVOWrite(a6)
  360.  
  361. no_arg    move.l    out_hndl(a4),d1
  362.     lea    txt3(pc),a1
  363.     move.l    a1,d2
  364.     moveq    #txt3_end-txt3,d3
  365.     pea    free0(pc)
  366.     jmp    _LVOWrite(a6)
  367.  
  368. ;=================================================
  369. h2a    moveq    #1,d6
  370.     move.w    d0,d2
  371. .conv    andi.b    #$f,d0
  372.     addi.b    #$30,d0
  373.     cmpi.b    #$3a,d0
  374.     bcs.s    .digit
  375.     addq.b    #7,d0
  376.  
  377. .digit    dbra    d6,.next
  378.     move.b    d0,d4
  379.     rts
  380.  
  381. .next    move.b    d0,d5
  382.     move.w    d2,d0
  383.     lsr.b    #4,d0
  384.     bra.s    .conv
  385.  
  386. Name    dc.b    'kb-scan',0
  387. T_NAME    TIMERNAME
  388. M_NAME    MISCNAME
  389. CA_NAME    CIAANAME
  390. D_NAME    DOSNAME
  391. txt    dc.b    $a,'    AT-keyboard scancode captor v0.01ß',$a,0
  392. txt_end
  393. txt1    dc.b    $a,'    File saved as "',0
  394. txt1_end
  395. txt2    dc.b    $a,'    Unable to create "',0
  396. txt2_end
  397. txt3    dc.b    $a,'    Error. No file specified.',$a
  398.     dc.b    '    Usage: kb-scan [capture file]',$a,0
  399. txt3_end
  400. txt4    dc.b    '    Press left mousebutton to quit.',$a,0
  401. txt4_end
  402. txt_cr    dc.b    '".',$a,0
  403. txt_cr_end
  404.  
  405.     section    storage,bss_p
  406.  
  407. data_zone
  408.     ds.b    bss_size
  409. buffer    ds.b    404
  410. _SysBase    equ    data_zone+exec_base
  411. _DOSBase    equ    data_zone+dos_base
  412.