Add Bocfel interpreter
[projects/chimara/chimara.git] / interpreters / bocfel / table.c
1 /*-
2  * Copyright 2010-2012 Chris Spiegel.
3  *
4  * This file is part of Bocfel.
5  *
6  * Bocfel is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version
8  * 2 or 3, as published by the Free Software Foundation.
9  *
10  * Bocfel is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Bocfel.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdlib.h>
20 #include <stdint.h>
21
22 #include "table.h"
23 #include "branch.h"
24 #include "memory.h"
25 #include "process.h"
26 #include "zterp.h"
27
28 void zcopy_table(void)
29 {
30   uint16_t first = zargs[0], second = zargs[1], size = zargs[2];
31
32   if(second == 0)
33   {
34     for(uint16_t i = 0; i < size; i++) user_store_byte(first + i, 0);
35   }
36   else if( (first > second) || (int16_t)size < 0 )
37   {
38     long n = labs((int16_t)size);
39     for(long i = 0; i < n; i++) user_store_byte(second + i, user_byte(first + i));
40   }
41   else
42   {
43     for(uint16_t i = 0; i < size; i++) user_store_byte(second + size - i - 1, user_byte(first + size - i - 1));
44   }
45 }
46
47 void zscan_table(void)
48 {
49   uint16_t addr = zargs[1];
50
51   if(znargs < 4) zargs[3] = 0x82;
52
53   for(uint16_t i = 0; i < zargs[2]; i++)
54   {
55     if(
56         ( (zargs[3] & 0x80) && (user_word(addr) == zargs[0])) ||
57         (!(zargs[3] & 0x80) && (user_byte(addr) == zargs[0]))
58       )
59     {
60       store(addr);
61       branch_if(1);
62       return;
63     }
64
65     addr += zargs[3] & 0x7f;
66   }
67
68   store(0);
69   branch_if(0);
70 }
71
72 void zloadw(void)
73 {
74   store(user_word(zargs[0] + (2 * zargs[1])));
75 }
76
77 void zloadb(void)
78 {
79   store(user_byte(zargs[0] + zargs[1]));
80 }
81
82 void zstoreb(void)
83 {
84   user_store_byte(zargs[0] + zargs[1], zargs[2]);
85 }
86
87 void zstorew(void)
88 {
89   user_store_word(zargs[0] + (2 * zargs[1]), zargs[2]);
90 }