home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / sigm / vol179 / cpmcall.cq / CPMCALL.C
Encoding:
C/C++ Source or Header  |  1985-02-10  |  6.1 KB  |  225 lines

  1.  
  2. /*
  3. CP/M bdos and bios calls for C/80
  4.   by T. Bolstad, ELEKTROKONSULT AS
  5.   Konnerudgaten. 3, N-3000 Drammen, Norway
  6.   1/17/83
  7.  NOTES : 
  8.          I found this in Dr. Dobb's Journal.
  9. If you have Software Toolworks C compiler and
  10. you have found it frustrating when writing asm
  11. subroutines to imbed in your C program because
  12. you could not call a BDOS or BIOS function,    
  13. here is the answer to your problems. You can modify
  14. it easily to fit your needs (by that, I mean you
  15. can reduce the number of functions to only those
  16. you require by eliminating them from the #define
  17. statements) and to use it you merely include  it
  18. in your source (#include <filename.c>).
  19. Helpful hint: when compiling this as it stands,
  20. with ALL the functions defined, you must use
  21. the -d compiler directive to expand the macro
  22. table. I used a value of 700, but I think that
  23. was a bit of overkill.
  24.  At the end of this program is something called
  25. 'LISTING 2' this is a test prohram to verify
  26. you have se written the program correctly and
  27. that it functions properly. This was also in
  28. Dr. Dobbs.
  29.                            Doug Haire
  30.                                             */
  31.  
  32.  
  33.  
  34. /*  definintion of BDOS functions  */
  35.  
  36. #define RESET    0    /* system RESET    */
  37. #define    CONSIN    1    /* console INPUT */
  38. #define    CONSOUT    2    /* reader INPUT */
  39. #define    READIN    3    /* punch input  */
  40. #define    PUNOUT    4    /* punch output  */
  41. #define    LISTOUT    5    /* list output  */
  42. #define    DIRCON    6    /* direct console I/0 */
  43. #define    GETIOB    7    /* get I/O byte  */
  44. #define    SETIOB    8    /* set I/O byte  */
  45. #define    PRNTST    9    /* print string  */
  46. #define    READCB    10    /* read console buffer */
  47. #define    GETCST    11    /* get console status  */
  48. #define    RETVN    12    /* return version number */
  49. #define    RESDSK    13    /* reset disk system */
  50. #define    SELDISK    14    /* select disk */
  51. #define    OPENF    15    /* open file   */
  52. #define    CLOSEF    16    /* close file  */
  53. #define    SRCHFF    17    /* search for first */
  54. #define    SRCHFN    18    /* search for next  */
  55. #define    DELF    19    /* delete file */
  56. #define    RDSEQ    20    /* read sequential */
  57. #define    WRSEQ    21    /* write sequential */
  58. #define    MAKEF    22    /* make file */
  59. #define    RENF    23    /* rename file */
  60. #define    RETLV    24    /* return login vector */
  61. #define    RETCD    25    /* return current disk */
  62. #define    STDMA    26    /* set dma address */
  63. #define    GETAA    27    /* get allocation address */
  64. #define    WPDSK    28    /* write protect disk */
  65. #define    GETROV    29    /* get read-only vector */
  66. #define    SETFAT    30    /* set file attributes */
  67. #define    GETDPA    31    /* get disk parameters adr */
  68. #define    SGUC    32    /* set/get user code */
  69. #define    RDRAN    33    /* read random */
  70. #define    WRRAN    34    /* write random */
  71. #define    COMFS    35    /* compute file size */
  72. #define    SETRRC    36    /* set random record */
  73. #define    RESDRV    37    /* reset drive */
  74. #define    WRRZF    38    /* write random w/ zero fill */
  75.  
  76.  
  77. bdos(funct, arg)    /* corresponds to bdos((BC),(DE)) */
  78. int    funct,arg;
  79.  
  80.  
  81. /*BCALL EXAMPLE:  bdos(RETVN,0)
  82.     BOTH ARGUMENTS MUST BE SPECIFIED!
  83.     Values are returned in HL. BDOS errors
  84.     are returned as -1.                   */
  85.  
  86. {
  87. #asm
  88. CPBASE    EQU    0        ;normal 0 org'ed CP/M 
  89. CPNTRY    EQU    CPBASE+5    ;BDOS entry
  90.  
  91.     POP    H        ;get return adr
  92.     POP    D        ;get arg (information adr)
  93.     POP    B        ;get function no.
  94.     PUSH    B        ;restore stack
  95.     PUSH    D
  96.     PUSH    H
  97.  
  98.     PUSH    B        ;save function no. on stack
  99.     CALL    CPNTRY        ;BDOS call
  100.     XCHG            ;save HL in DE
  101.     MOV    L,A        ;save A in L
  102.                 ;sign ext. to H:
  103.     RLC            ;get sign bit into CY
  104.     SBB    A        ;if CY=0, result after SBB is zero
  105.                 ;if CY=1, result after SBB is -1 (i.e. all ones
  106.     MOV    H,A        ;now A is moved to HL with sign extention
  107.     POP     B        ;get function no. in BC
  108.     MOV    A,C        ;get function no. in A
  109.     CPI    12        ;was it 'return version number' ?
  110.     JZ    RETHL1        
  111.     CPI    24        ;return login vector ?
  112.     JZ    RETHL1
  113.     CPI    27        ;set allocation adr ?
  114.     JZ    RETHL1
  115.     CPI    29        ;get read-only vector ?
  116.     JZ    RETHL1
  117.     CPI    31         ;get disk parameter adr ?
  118.     JZ    RETHL1
  119.     JMP    BDOSRET
  120.  
  121. RETHL1:    XCHG
  122. BDOSRET: RET            ;with returned value in HL
  123.  
  124. #endasm
  125. }
  126.  
  127. /*    definition of BIOS functions  */
  128.  
  129. #define BOOT    0    /* cold boot */
  130. #define WBOOT    1    /* warm boot */
  131. #define    CONST    2    /* console status */
  132. #define CONIN    3    /* console input */
  133. #define CONOUT    4    /* console output */
  134. #define LIST    5    /* list device */
  135. #define    PUNCH    6    /* punch */
  136. #define READER    7    /* reader */
  137. #define    HOME    8    /* home disk drive head */
  138. #define    SELDSK    9    /* select disk drive */
  139. #define SETTRK    10    /* set track */
  140. #define    SETSEC    11    /* set sector */
  141. #define SETDMA  12    /* set dma adr */
  142. #define READ    13    /* read one sector */
  143. #define    WRITE    14    /* write one sector */
  144. #define LISTST    15    /* list status */
  145. #define SECTRAN    16    /* sector translator */
  146.  
  147. bios(funct,arg1,arg2)    /* corresponds to bios(function, (BC),(DE)) */
  148. int funct,arg1,arg2;
  149.  
  150. /* CALL EXAMPLE:   bios(SETTRK,5,0)
  151. ALL 3 ARGUMENTS MUST BE SPECIFIED, even though
  152. the last one is only used by SELDSK and SECTRAN. */
  153.  
  154. {
  155. #asm
  156.  
  157.     POP    D        ;return adr
  158.     POP    H        ;argument 2
  159.     SHLD    ARG2S        ;save it
  160.     POP    B        ;argument 1
  161.     XCHG            ;set return adr into HL
  162.  
  163.     POP    D        ;function no
  164.  
  165.     PUSH    D        ;restore SP
  166.     PUSH    B
  167.     PUSH    B
  168.     PUSH    H        ;restore return adr
  169.  
  170.     PUSH    D        ;save function no. on stack
  171.  
  172.     LXI    H,0        ;calculate offset adr from function:
  173.     DAD    D        ; get function no. (offset) in HL
  174.     DAD    H        ; 2 * offset
  175.     DAD    D        ; 3 * offset
  176.     XCHG            ;save offset adr in DE
  177.  
  178.     LHLD    CPBASE+1    ;get pointer to BIOS WBOOT entry
  179.     DCX    H        ;decrement to
  180.     DCX    H        ; point to
  181.     DCX    H        ;  start of BIOS entry jump table
  182.  
  183.     DAD    D        ; add offset (result in HL)
  184.     XCHG            ;get result in DE
  185.     LXI    H,RET1    
  186.     PUSH    H        ;save return adr on stack
  187.  
  188.     LHLD    ARG2S        ;get argument 2
  189.     XCHG            ;get argument 2 into DE
  190.  
  191.     PCHL            ;go to BIOS
  192.  
  193. RET1:    XCHG            ;save HL in DE
  194.     MOV    L,A
  195.  
  196.     RLC            ;get sign bit into CY
  197.     SBB    A        ;if CY=0, result after SBB is zero
  198.                 ;if CY=1, result after SBB is -1
  199.     MOV    H,A
  200.     POP    B        ;get BIOS function no. in BC
  201.     MOV    A,C
  202.     CPI    9        ;select disk function ?
  203.     JZ    RETHL2
  204.     CPI    16        ;sector translation function ?
  205.     JZ    RETHL2
  206.  
  207.     JMP    RETBIOS
  208. RETHL2: XCHG            ;return value in HL
  209. RETBIOS: RET
  210. ARG2S:    DS    2
  211.  
  212. #endasm
  213. }
  214.  
  215. /* LISTING 2 - a test */
  216. /* This will return the designation of
  217.     the logged in disk */
  218.  
  219. #include "cpmcall.c"
  220. main()
  221. {
  222.     bios(c CONOUT,bdos(RETCD,0)+'A',0);
  223. }
  224.  
  225.