Subversion Repositories SvarDOS

Rev

Rev 358 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
351 mateuszvis 1
 
2
#include <i86.h>
3
#include <stdio.h>
4
#include <string.h>
5
 
6
#include "rmod.h"
7
 
8
#include "rmodinit.h"
9
 
10
 
11
/* returns segment where rmod is installed */
12
unsigned short rmod_install(unsigned short envsize) {
13
  char far *myptr, far *mcb;
14
  unsigned short far *owner;
15
  unsigned int rmodseg = 0xffff;
16
  unsigned int envseg = 0;
17
 
18
  /* read my current env segment from PSP */
19
  _asm {
20
    push ax
21
    push bx
22
    mov bx, 0x2c
23
    mov ax, [bx]
24
    mov envseg, ax
25
    pop bx
26
    pop ax
27
  }
28
 
29
  printf("original (PSP) env buffer at %04X\r\n", envseg);
30
  /* if custom envsize requested, convert it to number of paragraphs */
31
  if (envsize != 0) {
32
    envsize += 15;
33
    envsize /= 16;
34
  }
35
 
36
 
37
  _asm {
38
    /* link in the UMB memory chain for enabling high-memory allocation (and save initial status on stack) */
39
    mov ax, 0x5802  /* GET UMB LINK STATE */
40
    int 0x21
41
    xor ah, ah
42
    push ax         /* save link state on stack */
43
    mov ax, 0x5803  /* SET UMB LINK STATE */
44
    mov bx, 1
45
    int 0x21
46
    /* get current allocation strategy and save it in DX */
47
    mov ax, 0x5800
48
    int 0x21
49
    push ax
50
    pop dx
51
    /* set strategy to 'last fit, try high then low memory' */
52
    mov ax, 0x5801
53
    mov bx, 0x0082
54
    int 0x21
55
    /* ask for a memory block and save the given segment to rmodseg */
56
    mov ah, 0x48
57
    mov bx, (rmod_len + 15) / 16
58
    int 0x21
59
    jc ALLOC_FAIL
60
    mov rmodseg, ax
61
    /* ask for a memory block for the environment and save it to envseg (only if custom size requested) */
62
    mov bx, envsize
63
    test bx, bx
64
    jz ALLOC_FAIL
65
    mov ah, 0x48
66
    int 0x21
67
    jc ALLOC_FAIL
68
    mov envseg, ax
69
 
70
    ALLOC_FAIL:
71
    /* restore initial allocation strategy */
72
    mov ax, 0x5801
73
    mov bx, dx
74
    int 0x21
75
    /* restore initial UMB memory link state */
76
    mov ax, 0x5803
77
    pop bx       /* pop initial UMB link state from stack */
78
    int 0x21
79
  }
80
 
81
  if (rmodseg == 0xffff) {
82
    puts("malloc error");
83
    return(0xffff);
84
  }
85
 
86
  /* copy rmod to its destination */
87
  myptr = MK_FP(rmodseg, 0);
88
  _fmemcpy(myptr, rmod, rmod_len);
89
 
90
  /* mark rmod memory as "self owned" */
91
  mcb = MK_FP(rmodseg - 1, 0);
92
  owner = (void far *)(mcb + 1);
93
  *owner = rmodseg;
94
  _fmemcpy(mcb + 8, "SVARCOM", 8);
95
 
96
  /* mark env memory as "self owned" (only if allocated by me) */
97
  if (envsize != 0) {
98
    printf("envseg allocated at %04X:0000 with %u paragraphs\r\n", envseg, envsize);
99
    mcb = MK_FP(envseg - 1, 0);
100
    owner = (void far *)(mcb + 1);
101
    *owner = rmodseg;
102
    _fmemcpy(mcb + 8, "SVARENV", 8);
103
  }
104
 
105
  /* write env segment to rmod buffer */
106
  owner = MK_FP(rmodseg, RMOD_OFFSET_ENVSEG);
107
  *owner = envseg;
108
 
109
  /* set the int22 handler in my PSP to rmod so DOS jumps to rmod after I terminate */
110
  _asm {
111
    mov bx, 0x0a                   /* int22 handler is at 0x0A of the PSP */
112
    mov ax, RMOD_OFFSET_ROUTINE
113
    mov [bx], ax                   /* int handler offset */
114
    mov ax, rmodseg
115
    mov [bx+2], ax                 /* int handler segment */
116
  }
117
 
118
  return(rmodseg);
119
}
120
 
121
 
122
/* scan memory for rmod, returns its segment if found, 0xffff otherwise */
123
unsigned short rmod_find(void) {
124
  unsigned short i;
125
  unsigned short far *pattern;
126
 
127
  /* iterate over all paragraphs, looking for my signature */
128
  for (i = 0; i != 65535; i++) {
129
    pattern = MK_FP(i, 0);
130
    if (pattern[0] != 0x1983) continue;
131
    if (pattern[1] != 0x1985) continue;
132
    if (pattern[2] != 0x2017) continue;
133
    if (pattern[3] != 0x2019) continue;
134
    return(i);
135
  }
136
  return(0xffff);
137
}