*******************************************
* List DBASE shall be utilised to display *
* the DBASE of saved boot codes and allow *
* the user to select an item - subroutine *
* I/O is as follows:                      *
*                                         *
*       1.) a0 - pointer to name selected *
*       2.) d0 - selection made           *
*                                         *
*******************************************

List_DBASE
        
* Open 'Virus.File' which is in volume terminator

        move.l  #MODE_OLDFILE,d2
        lea     FileName(pc),a0
        move.l  a0,d1
        CALLDOS Open
        tst.l   d0
        beq     File_err

        move.l  d0,Filehandle

* Open the list window

**************************
* List DBASE  Window     *
**************************

* create window - from list window definition

        lea     ListWindow(pc),a0       load pointer to my definition
        CALLINT OpenWindow              
        tst.l   d0
        beq     List_err                if window not open - tidy up and exit
        move.l  d0,MyListWindow         otherwise save pointer to window structure

* print text - inform user of action 

        move.l  d0,a0                   window pointer still here
        move.l  wd_RPort(a0),a1         pointer to window raster port

* save rastport for later text functions

        move.l  a1,d1                   raster port pointer
        move.l  d1,List_RPort           save pointer

        move.l  #60,d0                  cursor x co-ord
        move.l  #18,d1                  cursor y co-ord
        CALLGRAF Move                   move cursor to 60,18
        
        lea     List_Message(pc),a0             my message to inform user
        moveq   #30,d0                  length of my message
        CALLGRAF Text                   print message

* Count Number of entries in Dbase - 40 chars name - 1024 chars code

        moveq   #0,d4                   for boot code count
        move.l  d4,BCode_Count

Not_end_file

        lea     Comments(pc),a0         needs rewritten *****
        move.l  a0,d2
        move.l  Filehandle,d1
        move.l  #40,d3
        CALLDOS Read
        cmp.l   #40,d0
        blt     End_of_File             fail or end of file

* increment counter - for future memory requirements

        move.l  BCode_Count,d4
        addq.l  #1,d4
        move.l  d4,BCode_Count

* Advance past boot code data - 1024 bytes (0 to 1023)

        move.l  #1024,d2                bytes to move
        move.l  Filehandle,d1
        move.l  #0,d3                   offset current
        CALLDOS Seek

        bra     Not_end_file

End_of_File

* Allocate memory to store maximum number of names  - 40 x No.

        moveq   #0,d1                   public memory
        move.l  #0,a0                   remember key - null
        move.l  #40,d0                  size times Bcode_count
        mulu    d4,d0                   Bcode_count * 40 Chars

        CALLINT AllocRemember
        move.l  d0,Search_Buffer        address of memory block

* Read File and store into search buffer

        moveq   #0,d2                   bytes to move
        move.l  Filehandle,d1
        moveq   #-1,d3                  offset current
        CALLDOS Seek

        move.l  Search_Buffer,d2

Not_end_file2

        move.l  Filehandle,d1
        moveq   #40,d3
        CALLDOS Read
        cmp.l   #40,d0
        blt     Display_list            fail or end of file

* save pointer in array

        move.l  d2,d4

* Advance past boot code data - 1024 bytes (0 to 1023)

        move.l  #1024,d2                bytes to move
        move.l  Filehandle,d1
        move.l  #0,d3                   offset current
        CALLDOS Seek

        move.l  d4,d2
        add.l   #40,d2
        bra     Not_end_file2

* File contents safe in buffer - now lets display them to the user

Display_list 

* set up prop gadget resolution - number of clicks to reach the bottom

        move.l  BCode_Count,d0
        addq.l  #1,d0
        move.w  #$ffff,d1
        and.l   #$ffff,d1
        divu    d0,d1
        lea     Prop_Info(pc),a0
        move.w  d1,pi_VPotRes(a0)       
        and.l   #$ffff,d1
        move.l  d1,VPot_Res             save potentiometer resolution

* Display first 8 entries in array list of Boot Codes stored
        

        moveq   #7,d3                   display entries 0 to 7 = (8)
        moveq   #18,d1                  offset top of window
        move.l  Search_Buffer,d4
        bsr     Display_Lines           first 7 lines of array

* Initialise Current display variables - Top and Bottom array
* values currently being displayed initialialy to first 8 entries

        move.l  #0,Last_VPot            init to zero v pot movement
        move.l  #$0,Top_Position
        move.l  #$7,Bot_Position

Mon_VPot

* find Y potentiometer value and calculate offset from top of array


        moveq   #0,d2                   zeroise
        moveq   #0,d5                   zeroise


no_movement     

        moveq   #1,d1
        CALLDOS Delay

        lea     Prop_Info(pc),a0
        move.w  pi_VertPot(a0),d0       number of vertical movements
        move.l  d0,Saved_movement
        move.l  Last_VPot,d1
        cmp.w   d0,d1
        beq     no_movement

* interogate movement - has it moved far enough yet

        move.l  VPot_Res,d4
        move.w  d0,d5
        move.w  d1,d2                   save for next subtract

* Test for movement downwards >> Prev - Pres = (- Down) (+ Up)

        sub.w   d0,d1
        bmi     greater_than

* Test for sufficient movement to register a scroll >> is it greater than 
* the Potentiometer resolution

        cmp.w   d1,d4
        bgt     no_movement
        bra     Scroll_Up

* Test for movement upward  >> Prev - Pres = (- Down) (+ Up)

greater_than

        move.w  d2,d1                   restore
        sub.l   d1,d0                   other way d1 less than d0
        cmp.w   d0,d4
        bgt     no_movement
        bra     Scroll_Down

Scroll_Up       

        move.l  Saved_movement,d0       number of vertical movements    
        and.l   #$ffff,d0
        move.l  d0,Last_VPot            update to next VPot

        subq.l  #1,Top_Position
        subq.l  #1,Bot_Position         next position

        moveq   #-10,d1                 pixels to scroll
        bsr     Scroll_Window

        move.l  Search_Buffer,d4        poiter to array

        move.l  Top_Position,d0
        moveq   #40,d2
        mulu    d2,d0
        add.l   d0,d4   

* Calculate start line for text

        move.l  #18,d1
        moveq   #0,d3                   one line only ( 0 to -1)
        bsr     Display_Lines
        bra     Mon_VPot

Scroll_Down

        move.l  Saved_movement,d0       number of vertical movements
        and.l   #$ffff,d0
        move.l  d0,Last_VPot            update to next VPot

        addq.l  #1,Bot_Position
        addq.l  #1,Top_Position         next position

        moveq   #10,d1                  pixels to scroll

        bsr     Scroll_Window

        move.l  Search_Buffer,d4        pointer to array


        move.l  Bot_Position,d0
        moveq   #40,d2
        mulu    d2,d0
        add.l   d0,d4   

* Calculate start line for text

        move.l  #90,d1
        moveq   #0,d3                   one line only ( 0 to -1)
        bsr     Display_Lines


* Return to check for next Pot movement

        bra     Mon_VPot


* Error and end of processing - tidy up

End_list

List_err

        move.l  MyListWindow(pc),a0
        CALLINT CloseWindow

        move.l  Filehandle,d1
        CALLDOS Close
File_err

        rts

***********************************
* Subroutine Scroll Window        *
***********************************

Scroll_Window

        move.l  List_RPort,a1
        moveq   #0,d0
        moveq   #10,d2
        moveq   #10,d3
        move.l  #350,d4
        move.l  #91,d5
        CALLGRAF ScrollRaster           

        rts

* ---------------------------------------------------------------------- *

***********************************
* Subroutine DISPLAY Lines        *
***********************************

Display_Lines

* Initialise pass d4 with address of first string

loop_until
        
        move.l  d1,d5
        move.l  List_RPort,a1
        moveq   #10,d0
        CALLGRAF Move

        bsr     Number_chars            find number of chars in str
        move.l  d4,a0                   position in buffer
        CALLGRAF Text   

        add.l   #40,d4
        move.l  d5,d1
        add.l   #10,d1

        dbra    d3,loop_until

        rts

* ---------------------------------------------------------------------- *

Number_chars

        move.l  d4,a2
        moveq   #-1,d0
num_chars
        addq.l  #1,d0
        cmpi.b  #31,(a2)+
        bgt     num_chars

        rts

* --------------------------------------------------------------------- *


* Structure definitions and declarations


MyListWindow    dc.l    0
List_RPort      dc.l    0
BCode_Count     dc.l    0
Last_VPot       dc.l    0
New_VPot        dc.l    0
VPot_Res        dc.l    0
Top_Position    dc.l    0
Bot_Position    dc.l    0
Pot_direction   dc.l    0
Saved_movement  dc.l    0


ListWindow      dc.w    40,20           x co-ord,y co-ord
                dc.w    440,100         width,height
                dc.b    3,1             detail,block pen colours
                dc.l    0
                dc.l    SMART_REFRESH!ACTIVATE!WINDOWDRAG
                dc.l    List_Prop       no program defined gadgets
                dc.l    0               no checkmarks
                dc.l    List_title      my title string
MyScreen3       dc.l    0               custom screen ptr (wbenchscreen)
                dc.l    0               no custom bitmap
                dc.w    0,0,0,0         no sizing
                dc.w    CUSTOMSCREEN    type of screen

List_title      dc.b    'D B A S E   Contents',0
List_Message    dc.b    'Searching DBASE please wait...',0



