summaryrefslogtreecommitdiff
path: root/bootblocks/i86_funcs.c
blob: a17e7a45fb2bd0e0f2cddbcc5061496438dca6af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

#include <stdio.h>
#include <errno.h>
#include "i86_funcs.h"

int   x86 = 0;			/* CPU major number */
char *x86_name = "";		/* and it's name */
int   x86_emu = 0;		/* Is this a PC emulator ? */
int   x86_a20_closed = 1;	/* Is the A20 gate closed ? */
int   x86_test = 0;		/* In test mode */
int   x86_fpu = 0;

unsigned boot_mem_top = 0x4000;	/* Default 64k, the minimum */
long     main_mem_top = 0;	/* K of extended memory */

int a20_closed()
{
   register int v, rv = 0;
   if (x86_test)
      return 1;			/* If not standalone don't try */

   __set_es(0);
   v = __peek_es(512);
   __set_es(0xFFFF);
   if (v == __peek_es(512+16))
   {
      __set_es(0);
      __poke_es(512, v+1);
      __set_es(0xFFFF);
      if (v+1 == __peek_es(512+16))
	 rv = 1;
      __set_es(0);
      __poke_es(512, v);
   }
   return x86_a20_closed = rv;
}

void open_a20()
{
#asm
  call	empty_8042
  mov	al,#0xD1	! command write
  out	#0x64,al
  call	empty_8042
  mov	al,#0xDF	! A20 on
  out	#0x60,al
empty_8042:
  .word	0x00eb,0x00eb
  in	al,#0x64	! 8042 status port
  test	al,#2		! is input buffer full?
  jnz	empty_8042	! yes - loop, with no timeout!
#endasm
}

void cpu_check()
{
   static char *name_808x[] =
   {
      "8088", "8086", "80C88", "80C86", "NEC V20", "NEC V30", "808x Clone"
   };

   static char *name_8018x[] =
   {
      "80188", "80186", "8018x Clone"
   };
   static char cpubuf[] = "80x86+";
   int   c, major;

   c = cputype(0);
   x86_fpu = (c < 0);
   major = ((c >> 8) & 0x1F);
   c &= 0xFF;
   x86 = (major&0xF);
   if (major == 0)
   {
      if (c > 6) c = 6;
      x86_name = name_808x[c];
   }
   else if (major == 1)
   {
      if (c > 3) c = 3;
      x86_name = name_8018x[c];
   }
   else
   {
      cpubuf[2] = (major&0xF)+'0';
      cpubuf[5] = (major > 15 ? '+' : '\0');
      x86_name = cpubuf;
      if (c & 0x01) x86_emu = 1;	/* Already in protected mode !!! */
   }
}

void mem_check()
{
   if (x86_test) return;	/* If not standalone don't try */
   {
#asm
  int	0x12		! Amount of boot memory
  mov	cl,#6
  sal	ax,cl		! In segments
  mov	[_boot_mem_top],ax

			! Next check for extended 
  mov	al,[_x86] 	! If we ain't got a 286+ we can't access it anyway
  cmp	al,#2
  jl  	is_xt

  mov	ah,#0x88	!
  int	0x15
  jnc	got_ext		! Error!? This should _not_ happen ... but ...
is_xt:
  xor	ax,ax
got_ext:
  mov	word ptr [_main_mem_top+2],#0
  mov	[_main_mem_top],ax

#endasm
  }

  if( main_mem_top == 0xFFFFL )
  {
     /* It say 64Mb-1k - Hmmmm I think it might be 128! */
  }
}

#define RIGHTS (0x93000000L)

static struct {
   char gdt0[8];
   char gdt1[8];
   unsigned short src_len;
   long           src_seg;
   unsigned int   spad;
   unsigned short dst_len;
   long           dst_seg;
   unsigned int   dpad;
   char gdt5[8];
} GDT = {
  "","",
  0xFFFF, RIGHTS, 0,
  0xFFFF, RIGHTS, 0,
  ""
};

ext_put(from, to, length)
unsigned int from, to, length;
{
   if(x86_test) return 3;
   GDT.src_seg = RIGHTS + from + ((long)__get_ds()<<4);
   GDT.dst_seg = RIGHTS + ((long)to<<8);
   if( length == 0xFFFF ) length = 0x8000;
   else                   length = ((length+1)>>1);
   return asm_copy(length);
}

ext_get(from, to, length)
unsigned int from, to, length;
{
   if(x86_test) return 3;
   GDT.src_seg = RIGHTS + ((long)from<<8);
   GDT.dst_seg = RIGHTS + to + ((long)__get_ds()<<4);
   if( length == 0xFFFF ) length = 0x8000;
   else                   length = ((length+1)>>1);
   return asm_copy(length);
}

static asm_copy(length)
{
#asm
#if !__FIRST_ARG_IN_AX__
  mov	bx,sp
  mov	ax,[bx+2]
#endif
  push	es
  push	si
  mov	cx,ax
  mov	ah,$87
  push	ds
  pop	es
  mov	si,#_GDT
  int	$15
  jc	err
  xor	ax,ax
err:
  mov	al,ah
  xor	ah,ah
  push	si
  push	es
#endasm
}