Skip to content

Commit e76c113

Browse files
committed
Accurate FPU environment: Increase code reuse and add a sanity check to avoid some segmentation faults, fixes 86Box#7882.
1 parent cefa6c4 commit e76c113

6 files changed

Lines changed: 77 additions & 176 deletions

File tree

‎src/codegen/codegen_x86-64.c‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -825,14 +825,6 @@ fpu_sf_check_exceptions(void)
825825
}
826826
}
827827

828-
static void
829-
fpu_postamble(void)
830-
{
831-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
832-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
833-
cpu_state.fpu_ea = cpu_state.eaaddr;
834-
}
835-
836828
void
837829
codegen_generate_call(uint8_t opcode, OpFn op, uint32_t fetchdat, uint32_t new_pc, uint32_t old_pc)
838830
{

‎src/codegen_new/codegen.c‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -396,14 +396,6 @@ fpu_sf_check_exceptions(void)
396396
}
397397
}
398398

399-
static void
400-
fpu_postamble(void)
401-
{
402-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
403-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
404-
cpu_state.fpu_ea = cpu_state.eaaddr;
405-
}
406-
407399
void
408400
codegen_generate_call(uint8_t opcode, OpFn op, uint32_t fetchdat, uint32_t new_pc, uint32_t old_pc)
409401
{

‎src/cpu/cpu.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,4 +913,6 @@ extern void wait_cycs(int c, int bus);
913913
#define prefetch_queue_set_suspended(s) prefetch_queue_set_prefetching(!s)
914914
#define prefetch_queue_get_suspended !prefetch_queue_get_prefetching
915915

916+
extern void fpu_postamble(void);
917+
916918
#endif /*EMU_CPU_H*/

‎src/cpu/x86.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,14 @@ hardresetx86(void)
419419

420420
resetx86();
421421
}
422+
423+
void
424+
fpu_postamble(void)
425+
{
426+
if (cpu_state.ea_seg != NULL) {
427+
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
428+
cpu_state.fpu_ds = cpu_state.ea_seg->base;
429+
}
430+
431+
cpu_state.fpu_ea = cpu_state.eaaddr;
432+
}

‎src/cpu/x86_ops_fpu.h‎

Lines changed: 32 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@ opESCAPE_d8_a16(uint32_t fetchdat)
1313
cpu_state.fpu_CS = cpu_state.temp_CS;
1414
cpu_state.fpu_cs = cpu_state.temp_cs;
1515
cpu_state.fpu_pc = cpu_state.temp_pc;
16-
if ((x87_op & 0xff) < 0xc0) {
17-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
18-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
19-
cpu_state.fpu_ea = cpu_state.eaaddr;
20-
}
16+
if ((x87_op & 0xff) < 0xc0)
17+
fpu_postamble();
2118
return ret;
2219
}
2320
static int
@@ -32,11 +29,8 @@ opESCAPE_d8_a32(uint32_t fetchdat)
3229
cpu_state.fpu_CS = cpu_state.temp_CS;
3330
cpu_state.fpu_cs = cpu_state.temp_cs;
3431
cpu_state.fpu_pc = cpu_state.temp_pc;
35-
if ((x87_op & 0xff) < 0xc0) {
36-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
37-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
38-
cpu_state.fpu_ea = cpu_state.eaaddr;
39-
}
32+
if ((x87_op & 0xff) < 0xc0)
33+
fpu_postamble();
4034
return ret;
4135
}
4236

@@ -52,11 +46,8 @@ opESCAPE_d9_a16(uint32_t fetchdat)
5246
cpu_state.fpu_CS = cpu_state.temp_CS;
5347
cpu_state.fpu_cs = cpu_state.temp_cs;
5448
cpu_state.fpu_pc = cpu_state.temp_pc;
55-
if ((x87_op & 0xff) < 0xc0) {
56-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
57-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
58-
cpu_state.fpu_ea = cpu_state.eaaddr;
59-
}
49+
if ((x87_op & 0xff) < 0xc0)
50+
fpu_postamble();
6051
return ret;
6152
}
6253
static int
@@ -68,11 +59,8 @@ opESCAPE_d9_a32(uint32_t fetchdat)
6859
cpu_state.fpu_CS = cpu_state.temp_CS;
6960
cpu_state.fpu_cs = cpu_state.temp_cs;
7061
cpu_state.fpu_pc = cpu_state.temp_pc;
71-
if ((x87_op & 0xff) < 0xc0) {
72-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
73-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
74-
cpu_state.fpu_ea = cpu_state.eaaddr;
75-
}
62+
if ((x87_op & 0xff) < 0xc0)
63+
fpu_postamble();
7664
return ret;
7765
}
7866

@@ -88,11 +76,8 @@ opESCAPE_da_a16(uint32_t fetchdat)
8876
cpu_state.fpu_CS = cpu_state.temp_CS;
8977
cpu_state.fpu_cs = cpu_state.temp_cs;
9078
cpu_state.fpu_pc = cpu_state.temp_pc;
91-
if ((x87_op & 0xff) < 0xc0) {
92-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
93-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
94-
cpu_state.fpu_ea = cpu_state.eaaddr;
95-
}
79+
if ((x87_op & 0xff) < 0xc0)
80+
fpu_postamble();
9681
return ret;
9782
}
9883
static int
@@ -107,11 +92,8 @@ opESCAPE_da_a32(uint32_t fetchdat)
10792
cpu_state.fpu_CS = cpu_state.temp_CS;
10893
cpu_state.fpu_cs = cpu_state.temp_cs;
10994
cpu_state.fpu_pc = cpu_state.temp_pc;
110-
if ((x87_op & 0xff) < 0xc0) {
111-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
112-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
113-
cpu_state.fpu_ea = cpu_state.eaaddr;
114-
}
95+
if ((x87_op & 0xff) < 0xc0)
96+
fpu_postamble();
11597
return ret;
11698
}
11799

@@ -127,11 +109,8 @@ opESCAPE_db_a16(uint32_t fetchdat)
127109
cpu_state.fpu_CS = cpu_state.temp_CS;
128110
cpu_state.fpu_cs = cpu_state.temp_cs;
129111
cpu_state.fpu_pc = cpu_state.temp_pc;
130-
if ((x87_op & 0xff) < 0xc0) {
131-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
132-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
133-
cpu_state.fpu_ea = cpu_state.eaaddr;
134-
}
112+
if ((x87_op & 0xff) < 0xc0)
113+
fpu_postamble();
135114
return ret;
136115
}
137116
static int
@@ -146,11 +125,8 @@ opESCAPE_db_a32(uint32_t fetchdat)
146125
cpu_state.fpu_CS = cpu_state.temp_CS;
147126
cpu_state.fpu_cs = cpu_state.temp_cs;
148127
cpu_state.fpu_pc = cpu_state.temp_pc;
149-
if ((x87_op & 0xff) < 0xc0) {
150-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
151-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
152-
cpu_state.fpu_ea = cpu_state.eaaddr;
153-
}
128+
if ((x87_op & 0xff) < 0xc0)
129+
fpu_postamble();
154130
return ret;
155131
}
156132

@@ -166,11 +142,8 @@ opESCAPE_dc_a16(uint32_t fetchdat)
166142
cpu_state.fpu_CS = cpu_state.temp_CS;
167143
cpu_state.fpu_cs = cpu_state.temp_cs;
168144
cpu_state.fpu_pc = cpu_state.temp_pc;
169-
if ((x87_op & 0xff) < 0xc0) {
170-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
171-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
172-
cpu_state.fpu_ea = cpu_state.eaaddr;
173-
}
145+
if ((x87_op & 0xff) < 0xc0)
146+
fpu_postamble();
174147
return ret;
175148
}
176149
static int
@@ -185,11 +158,8 @@ opESCAPE_dc_a32(uint32_t fetchdat)
185158
cpu_state.fpu_CS = cpu_state.temp_CS;
186159
cpu_state.fpu_cs = cpu_state.temp_cs;
187160
cpu_state.fpu_pc = cpu_state.temp_pc;
188-
if ((x87_op & 0xff) < 0xc0) {
189-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
190-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
191-
cpu_state.fpu_ea = cpu_state.eaaddr;
192-
}
161+
if ((x87_op & 0xff) < 0xc0)
162+
fpu_postamble();
193163
return ret;
194164
}
195165

@@ -205,11 +175,8 @@ opESCAPE_dd_a16(uint32_t fetchdat)
205175
cpu_state.fpu_CS = cpu_state.temp_CS;
206176
cpu_state.fpu_cs = cpu_state.temp_cs;
207177
cpu_state.fpu_pc = cpu_state.temp_pc;
208-
if ((x87_op & 0xff) < 0xc0) {
209-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
210-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
211-
cpu_state.fpu_ea = cpu_state.eaaddr;
212-
}
178+
if ((x87_op & 0xff) < 0xc0)
179+
fpu_postamble();
213180
return ret;
214181
}
215182
static int
@@ -224,11 +191,8 @@ opESCAPE_dd_a32(uint32_t fetchdat)
224191
cpu_state.fpu_CS = cpu_state.temp_CS;
225192
cpu_state.fpu_cs = cpu_state.temp_cs;
226193
cpu_state.fpu_pc = cpu_state.temp_pc;
227-
if ((x87_op & 0xff) < 0xc0) {
228-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
229-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
230-
cpu_state.fpu_ea = cpu_state.eaaddr;
231-
}
194+
if ((x87_op & 0xff) < 0xc0)
195+
fpu_postamble();
232196
return ret;
233197
}
234198

@@ -244,11 +208,8 @@ opESCAPE_de_a16(uint32_t fetchdat)
244208
cpu_state.fpu_CS = cpu_state.temp_CS;
245209
cpu_state.fpu_cs = cpu_state.temp_cs;
246210
cpu_state.fpu_pc = cpu_state.temp_pc;
247-
if ((x87_op & 0xff) < 0xc0) {
248-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
249-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
250-
cpu_state.fpu_ea = cpu_state.eaaddr;
251-
}
211+
if ((x87_op & 0xff) < 0xc0)
212+
fpu_postamble();
252213
return ret;
253214
}
254215
static int
@@ -263,11 +224,8 @@ opESCAPE_de_a32(uint32_t fetchdat)
263224
cpu_state.fpu_CS = cpu_state.temp_CS;
264225
cpu_state.fpu_cs = cpu_state.temp_cs;
265226
cpu_state.fpu_pc = cpu_state.temp_pc;
266-
if ((x87_op & 0xff) < 0xc0) {
267-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
268-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
269-
cpu_state.fpu_ea = cpu_state.eaaddr;
270-
}
227+
if ((x87_op & 0xff) < 0xc0)
228+
fpu_postamble();
271229
return ret;
272230
}
273231

@@ -283,11 +241,8 @@ opESCAPE_df_a16(uint32_t fetchdat)
283241
cpu_state.fpu_CS = cpu_state.temp_CS;
284242
cpu_state.fpu_cs = cpu_state.temp_cs;
285243
cpu_state.fpu_pc = cpu_state.temp_pc;
286-
if ((x87_op & 0xff) < 0xc0) {
287-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
288-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
289-
cpu_state.fpu_ea = cpu_state.eaaddr;
290-
}
244+
if ((x87_op & 0xff) < 0xc0)
245+
fpu_postamble();
291246
return ret;
292247
}
293248
static int
@@ -302,11 +257,8 @@ opESCAPE_df_a32(uint32_t fetchdat)
302257
cpu_state.fpu_CS = cpu_state.temp_CS;
303258
cpu_state.fpu_cs = cpu_state.temp_cs;
304259
cpu_state.fpu_pc = cpu_state.temp_pc;
305-
if ((x87_op & 0xff) < 0xc0) {
306-
cpu_state.fpu_DS = cpu_state.ea_seg->seg;
307-
cpu_state.fpu_ds = cpu_state.ea_seg->base;
308-
cpu_state.fpu_ea = cpu_state.eaaddr;
309-
}
260+
if ((x87_op & 0xff) < 0xc0)
261+
fpu_postamble();
310262
return ret;
311263
}
312264

0 commit comments

Comments
 (0)