summaryrefslogtreecommitdiffhomepage
path: root/mixlib/mix_vm.c
blob: 37a0c6d3e1cb0f5a4b877db9681c4f1247545613 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* -*-c-*- ------------------ mix_vm.c :
 * Implementation of the functions declared in mix_vm.h
 * ------------------------------------------------------------------
 * Copyright (C) 2000 jose antonio ortega ruiz <jaortega@acm.org>
 *  
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 */

#include "mix.h"
#include "xmix_vm.h"

static void
vm_reset_ (mix_vm_t *vm)
{
  guint k;
  set_rA_ (vm, MIX_WORD_ZERO);
  set_rX_ (vm, MIX_WORD_ZERO);
  set_rJ_ (vm, MIX_WORD_ZERO);
  for ( k = 1; k < IREG_NO_+1; ++k )
    set_rI_ (vm, k, MIX_WORD_ZERO);
  set_over_ (vm, FALSE);
  set_cmp_ (vm, mix_EQ);
  set_loc_ (vm, MIX_WORD_ZERO);
  for ( k = 0; k < MEM_CELLS_NO_; ++k)
    set_cell_ (vm, k, MIX_WORD_ZERO);
  halt_ (vm, FALSE);
  if (vm->symbol_table != NULL )
    {
      mix_symbol_table_delete (vm->symbol_table);
      vm->symbol_table = NULL;
    }
  if (vm->line_table != NULL)
    {
      g_tree_destroy (vm->line_table);
      vm->line_table = NULL;
    }
  bp_clear_all_ (vm);
}


/* Create/destroy a mix vm */
mix_vm_t *
mix_vm_new (void)
{
  int i;
  
  mix_vm_t *vm = g_new (struct mix_vm_t,1);
  vm->line_table = NULL;
  vm->symbol_table = NULL;
  
  for (i = 0; i < BD_NO_; ++i)
    vm->devices[i] = NULL;
    
  vm_reset_ (vm);
  return vm;
}

void
mix_vm_delete (mix_vm_t * vm)
{
  int i;
  
  if (vm->line_table != NULL) g_tree_destroy (vm->line_table);
  if (vm->symbol_table != NULL) mix_symbol_table_delete (vm->symbol_table);
  for (i = 0; i < BD_NO_; ++i)
    mix_device_delete (vm->devices[i]);
  g_free (vm);
}

/* Reset a vm  (set state as of a newly created one) */
void
mix_vm_reset (mix_vm_t * vm)
{
  g_return_if_fail (vm != NULL);
  vm_reset_ (vm);
}

/* Set start address for execution */
void
mix_vm_set_start_addr (mix_vm_t *vm, mix_address_t addr)
{
  g_return_if_fail (vm != NULL);
  set_loc_ (vm, addr);
}

/* Access to the vm's registers */
mix_word_t
mix_vm_get_rA (const mix_vm_t *vm)
{
  g_return_val_if_fail (vm != NULL, MIX_WORD_ZERO);
  return get_rA_ (vm);
}

mix_word_t
mix_vm_get_rX (const mix_vm_t *vm)
{
  g_return_val_if_fail (vm != NULL, MIX_WORD_ZERO);
  return get_rX_ (vm);
}

mix_short_t
mix_vm_get_rJ (const mix_vm_t *vm)
{
  g_return_val_if_fail (vm != NULL, MIX_SHORT_ZERO);
  return mix_word_to_short_fast (get_rJ_ (vm));
}

mix_short_t
mix_vm_get_rI (const mix_vm_t *vm, guint idx)
{
  g_return_val_if_fail (vm != NULL, MIX_SHORT_ZERO);
  g_return_val_if_fail (IOK_ (idx), MIX_SHORT_ZERO);
  return mix_word_to_short_fast (get_rI_ (vm, idx));
}

void 
mix_vm_set_rA (mix_vm_t *vm, mix_word_t value)
{
  g_return_if_fail (vm != NULL);
  set_rA_ (vm, value);
}

void
mix_vm_set_rX (mix_vm_t *vm, mix_word_t value)
{
  g_return_if_fail (vm != NULL);
  set_rX_ (vm, value);
}

void 
mix_vm_set_rJ (mix_vm_t *vm, mix_short_t value)
{
  g_return_if_fail (vm != NULL);
  g_return_if_fail (mix_short_is_positive (value));
  set_rJ_ (vm, mix_short_to_word_fast (value));
}

void 
mix_vm_set_rI (mix_vm_t *vm, guint idx, mix_short_t value)
{
  g_return_if_fail (vm != NULL);
  g_return_if_fail (IOK_ (idx));
  set_rI_ (vm, idx, mix_short_to_word_fast (value));
}

/* Access to the comparison flag and overflow toggle */
mix_cmpflag_t
mix_vm_get_cmpflag (const mix_vm_t *vm)
{
  g_return_val_if_fail (vm != NULL, mix_EQ);
  return get_cmp_ (vm);
}

void
mix_vm_set_cmpflag (mix_vm_t *vm, mix_cmpflag_t value)
{
  g_return_if_fail (vm != NULL);
  set_cmp_ (vm, value);
}

gboolean
mix_vm_get_overflow (const mix_vm_t *vm)
{
  g_return_val_if_fail (vm != NULL, TRUE);
  return get_over_ (vm);
}

void
mix_vm_set_overflow (mix_vm_t *vm, gboolean value)
{
  g_return_if_fail (vm != NULL);
  set_over_ (vm, value);
}

void
mix_vm_toggle_overflow (mix_vm_t *vm)
{
  g_return_if_fail (vm != NULL);
  set_over_ (vm, !get_over_ (vm));
}

/* Access to memory cells */
mix_word_t
mix_vm_get_addr_contents (const mix_vm_t *vm, mix_address_t addr)
{
  g_return_val_if_fail (vm != NULL, MIX_WORD_ZERO);
  g_return_val_if_fail (MEMOK_ (addr), MIX_WORD_ZERO);
  return get_cell_ (vm, addr);
}

void
mix_vm_set_addr_contents (mix_vm_t *vm, mix_address_t addr, mix_word_t value)
{
  g_return_if_fail (vm != NULL);
  g_return_if_fail (MEMOK_ (addr));
  set_cell_ (vm, addr, value);
}

gboolean
mix_vm_is_halted (const mix_vm_t *vm)
{
  return is_halted_ (vm);
}

/* Execution of instructions */
gboolean /* TRUE if success */
mix_vm_exec_ins (mix_vm_t *vm, const mix_ins_t *ins)
{
  g_return_val_if_fail (vm != NULL, FALSE);
  g_return_val_if_fail (ins != NULL, FALSE);
  return (*ins_handlers_[ins->opcode]) (vm,ins);
}

/* comparison function for the line table tree */
static gint
cmp_lineno_ (gconstpointer a, gconstpointer b)
{
  return GPOINTER_TO_UINT (a) - GPOINTER_TO_UINT (b);
}

gboolean
mix_vm_load_file (mix_vm_t *vm, const gchar *name)
{
  mix_code_file_t *file;
  mix_ins_desc_t ins;
  
  g_return_val_if_fail (vm != NULL, FALSE);
  file = mix_code_file_new_read (name);
  if (file == NULL) return FALSE;
  vm_reset_ (vm);
  if ( mix_code_file_is_debug (file) )
    {
      vm->symbol_table = mix_code_file_get_symbol_table (file);
      vm->line_table = g_tree_new (cmp_lineno_);
    }
  while ( mix_code_file_get_ins (file, &ins) ) 
    {
      set_cell_ (vm, ins.address, ins.ins);
      if ( vm->line_table != NULL )
	g_tree_insert (vm->line_table, 
		       GUINT_TO_POINTER (ins.lineno),
		       GUINT_TO_POINTER ((guint)ins.address));
    }
  set_loc_ (vm, mix_code_file_get_start_addr (file));
  set_start_ (vm, get_loc_ (vm));
  mix_code_file_delete (file);
  
  return TRUE;
}

const mix_symbol_table_t *
mix_vm_get_symbol_table (const mix_vm_t *vm)
{
  g_return_val_if_fail (vm != NULL, NULL);
  return vm->symbol_table;
}

mix_address_t 
mix_vm_get_prog_count (const mix_vm_t *vm)
{
  g_return_val_if_fail (vm != NULL, MIX_SHORT_ZERO);
  return get_loc_ (vm);
}

/* continue execution of instructions in memory */
int
mix_vm_run (mix_vm_t *vm)
{
  mix_ins_t ins;
  g_return_val_if_fail (vm != NULL, MIX_VM_ERROR);
  
  if ( is_halted_ (vm) )
    {
      reset_loc_ (vm);	/* set current location to start address */
      halt_ (vm, FALSE);
    }
  
  while ( !is_halted_ (vm) )
    {
      mix_word_to_ins_uncheck (get_cell_ (vm, get_loc_ (vm)), ins);
      if ( !(*ins_handlers_[ins.opcode]) (vm,&ins) ) 
	return MIX_VM_ERROR;
      if (bp_is_set_ (vm, get_loc_ (vm)))
	return MIX_VM_BREAK;
      if (get_loc_ (vm) >= MIX_VM_CELL_NO) halt_ (vm, TRUE);
    }
  return MIX_VM_HALT;
}

/* execute next memory instruction */
int
mix_vm_exec_next (mix_vm_t *vm)
{
  mix_ins_t ins;
  g_return_val_if_fail (vm != NULL, MIX_VM_ERROR);
  if ( is_halted_ (vm) ) return MIX_VM_HALT;
  mix_word_to_ins_uncheck (get_cell_ (vm, get_loc_ (vm)), ins);
  if (!(*ins_handlers_[ins.opcode]) (vm, &ins))
    return MIX_VM_ERROR;
  return MIX_VM_BREAK;
}

/* Breakpoints */
typedef struct
{
  mix_address_t address;
  gulong lineno;
} fline_t;

static int
find_break_ (gpointer key, gpointer value, gpointer data)
{
  fline_t *fl = (fline_t *)data;
  
  if ( mix_short_new (GPOINTER_TO_UINT (value)) == fl->address )
    {
      fl->lineno = GPOINTER_TO_UINT (key);
      return TRUE;
    }
  return FALSE;
}

gulong
mix_vm_get_break_lineno (const mix_vm_t *vm)
{
  fline_t fl;
  
  g_return_val_if_fail (vm != NULL, 0);
  if (vm->line_table == NULL) return 0;
  fl.lineno = 0;
  fl.address = get_loc_ (vm);
  g_tree_traverse (vm->line_table, find_break_, G_IN_ORDER, (gpointer) &fl);
  return fl.lineno;
}

typedef struct 
{
  mix_vm_t *vm;
  guint lineno;
  gint result;
} bp_traverse_t;

static gint
set_break_ (gpointer key, gpointer value, gpointer data)
{
  bp_traverse_t *tr = (bp_traverse_t *)data;
  if (GPOINTER_TO_UINT (key) >= tr->lineno)
    {
      bp_set_ (tr->vm, mix_short_new (GPOINTER_TO_UINT (value)));
      tr->lineno = GPOINTER_TO_UINT (key);
      tr->result = MIX_VM_BP_OK;
      return TRUE;
    }
  return FALSE;
}

gint /* if >0 the line no. of the break point */
mix_vm_set_breakpoint (mix_vm_t *vm, guint lineno)
{
  bp_traverse_t tr;
  
  g_return_val_if_fail (vm != NULL, MIX_VM_BP_ERROR);
  if (!vm->line_table) return MIX_VM_BP_NDEBUG;
  tr.lineno = lineno;
  tr.vm = vm;
  tr.result = MIX_VM_BP_INV_LINE;
  g_tree_traverse (vm->line_table, set_break_, G_IN_ORDER, (gpointer)&tr);
  if (tr.result == MIX_VM_BP_OK)
    return tr.lineno;
  else
    return tr.result;
}

gint
mix_vm_set_breakpoint_address (mix_vm_t *vm, guint address) 
{
  g_return_val_if_fail (vm != NULL, MIX_VM_BP_ERROR);
  if (address >= MIX_VM_CELL_NO)
    return MIX_VM_BP_INV_ADDRESS;
  else
    bp_set_ (vm, mix_short_new (address));
  return MIX_VM_BP_OK;
}

static gint
clear_break_ (gpointer key, gpointer value, gpointer data)
{
  bp_traverse_t *tr = (bp_traverse_t *)data;
  if (GPOINTER_TO_UINT (key) == tr->lineno)
    {
      bp_clear_ (tr->vm, mix_short_new (GPOINTER_TO_UINT (value)));
      tr->result = MIX_VM_BP_OK;
      return TRUE;
    }
  else if (GPOINTER_TO_UINT (key) > tr->lineno)
    return TRUE;
  
  return FALSE;
}

gint /* one of MIX_VM_BP_ */
mix_vm_clear_breakpoint (mix_vm_t *vm, guint lineno)
{
  bp_traverse_t tr;
  
  g_return_val_if_fail (vm != NULL, MIX_VM_BP_ERROR);
  if (!vm->line_table) return MIX_VM_BP_NDEBUG;
  tr.lineno = lineno;
  tr.vm = vm;
  tr.result = MIX_VM_BP_INV_LINE;
  g_tree_traverse (vm->line_table, clear_break_, G_IN_ORDER, (gpointer)&tr);
  return tr.result;
}

gint
mix_vm_clear_breakpoint_address (mix_vm_t *vm, guint address) 
{
  g_return_val_if_fail (vm != NULL, MIX_VM_BP_ERROR);
  if (address >= MIX_VM_CELL_NO)
    return MIX_VM_BP_INV_ADDRESS;
  else
    bp_clear_ (vm, mix_short_new (address));
  return MIX_VM_BP_OK;
}

void
mix_vm_clear_all_breakpoints (mix_vm_t *vm)
{
  g_return_if_fail (vm != NULL);
  bp_clear_all_ (vm);
}