-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
167 lines (163 loc) · 5.64 KB
/
main.c
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
/****************************************************************************
* Copyright (C) 2020 by Ted Kotz *
* *
* This file is part of TernCpuEmu. *
* *
* TernCpuEmu is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* *
* TernCpuEmu 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with TernCpuEmu. If not, see *
* <http://www.gnu.org/licenses/>. *
****************************************************************************/
/**
* @file main.c
* @author Ted Kotz <[email protected]>
* @version 0.1
*
* The main program for the Ternary Computer Emulator. This file plumbs
* together all the parts of the system and kicks it off.
*
*/
/* Includes ******************************************************************/
#include "cpu.h"
#include "mem.h"
#include <stdio.h>
#include <ctype.h>
/* Defines *******************************************************************/
/* Types *********************************************************************/
/* Interfaces ****************************************************************/
/* Data **********************************************************************/
/* Functions *****************************************************************/
static int fileToMem( const char* fname , int verbose)
{
FILE* filein=NULL;
int returnVal=-3;
filein = fopen(fname,"r");
if(filein == NULL)
{
printf("Error in opening file \"%s\".", fname);
}
else
{
returnVal=readFileIntoMem(filein, fname, verbose);
fclose(filein);
}
return returnVal;
}
/**
* Main Program entry point.
*
* @param argc number of command line arguments
* @param argv array of pointers to command line arguments
* @return 0 if successful, otherwise error code
*/
int main( int argc, char** argv )
{
char name[80];
char c='\r';
int i;
TriCpu cpu;
TriWord addr;
TriWord val;
TriCpu disposable;
resetMem();
if( argc > 1 )
{
i=fileToMem(argv[1], 1);
if(i)
{
return i;
}
}
resetCPU(&disposable);
resetCPU(&cpu);
printCpuState(&cpu);
printf("\n\nh for help, <Enter> for step: \n");
c=getchar();
while ('q' != c)
{
switch(c)
{
case 'r':
printf("ADDR: ");
ternaryScan( &addr );
printf("\nVAL: ");
val = ReadAddr(addr);
ternaryPrint( val, TRITS_PER_TRYTE );
printf("(%"PRIi64")\n", ternary2int(val));
break;
case 'w':
printf("ADDR: ");
ternaryScan( &addr );
printf("\nVAL: ");
ternaryScan( &val );
WriteAddr(addr, val);
printf("done.\n");
break;
case 's':
getchar();
printCpuState(&cpu);
break;
case 'c':
getchar();
resetCPU(&cpu);
printCpuState(&cpu);
break;
case 'm':
getchar();
resetMem();
printCpuState(&cpu);
break;
case 'f':
printf("filename: ");
do
{
c=getchar();
}
while( isspace(c) );
i=0;
while( !isspace(c) )
{
if(i<sizeof(name)-1)
{
name[i++]=c;
}
c=getchar();
}
name[i]='\0';
if(0==fileToMem( name , 1))
{
printf("Done. Press c to Reset CPU or s to Print CPU State.\n");
}
break;
case 'h':
case '?':
getchar();
printf("Help for Ternary Computer Emulator \n");
printf("NOTE: Addresses and values are entered in balanced ternary(10-). \n");
printf(" q : Quit\n");
printf(" s : Print CPU State\n");
printf(" r <ADDR> : Peek value of Tryte at Address\n");
printf(" w <ADDR> <VAL> : Poke Value to Tryte at Address\n");
printf(" c : Reset CPU\n");
printf(" m : Reset Memory\n");
printf(" f : Read file into memory\n");
printf(" ? : help\n");
break;
default:
runCPU(&cpu, 1);
printCpuState(&cpu);
}
printf("\n\nh for help, <Enter> for step: \n");
c=getchar();
}
return 0;
}