summaryrefslogtreecommitdiff
path: root/csim/main.c
diff options
context:
space:
mode:
authorAlex AUVOLAT <alex.auvolat@ens.fr>2013-11-04 22:05:57 +0100
committerAlex AUVOLAT <alex.auvolat@ens.fr>2013-11-04 22:05:57 +0100
commitcebd07b64f1f537c5ecf00ec21ff4b7c4032f0a3 (patch)
tree9d2048313fe3ad4c92865c8f0236bed24e64449b /csim/main.c
parentf253f98136def21b5e50c5922246e2ddfe315442 (diff)
downloadSystDigit-Projet-cebd07b64f1f537c5ecf00ec21ff4b7c4032f0a3.tar.gz
SystDigit-Projet-cebd07b64f1f537c5ecf00ec21ff4b7c4032f0a3.zip
Added stub C simulator (defined dumb-down syntax for netlists).
Diffstat (limited to 'csim/main.c')
-rw-r--r--csim/main.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/csim/main.c b/csim/main.c
new file mode 100644
index 0000000..ce3f5b6
--- /dev/null
+++ b/csim/main.c
@@ -0,0 +1,97 @@
+/*
+ Système Digital
+ 2013-2014
+ Alex AUVOLAT
+
+ main.c Main file for the C simulator
+*/
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "sim.h"
+
+void usage() {
+ printf ("\nUsage:\n\tcsim [options] <netlist_file>\n\n");
+ printf("Available options:\n");
+ printf("\n -rom <file>\n\tLoad a filename as a ROM file for the machine\n");
+ printf("\n -n <steps>\n\tOnly run #steps steps of simulation (-1 = infinity)\n");
+ printf("\n -in <in-file>\n\tRead inputs from given file (eg. named pipe). Defaults to STDIN.\n");
+ printf("\n -out <out-file>\n\tWrite outputs to given file (eg. named pipe). Defaults to STDOut.\n");
+ exit(1);
+}
+
+// Arguments to be parsed
+int steps = 42;
+char *romfile = NULL;
+char *filename = NULL;
+char *infile = NULL;
+char *outfile = NULL;
+
+int main(int argc, char **argv) {
+ int i;
+ for (i = 1; i < argc; i++) {
+ if (!strcmp(argv[i], "-rom")) {
+ if (++i == argc) usage();
+ romfile = argv[i];
+ } else if (!strcmp(argv[i], "-n")) {
+ if (++i == argc) usage();
+ steps = atoi(argv[i]);
+ } else if (!strcmp(argv[i], "-in")) {
+ if (++i == argc) usage();
+ infile = argv[i];
+ } else if (!strcmp(argv[i], "-out")) {
+ if (++i == argc) usage();
+ outfile = argv[i];
+ } else {
+ filename = argv[i];
+ }
+ }
+
+ if (filename == NULL) usage();
+
+ // Load program
+ FILE *p_in;
+ p_in = fopen(filename, "r");
+ if (!p_in) {
+ fprintf(stderr, "Error: could not open file %s for input.\n", filename);
+ return 1;
+ }
+ t_program* program = load_dumb_netlist(p_in);
+ fclose(p_in);
+
+ // Setup input and outputs
+ FILE *input = stdin, *output = stdout;
+ if (infile != NULL) {
+ input = fopen(infile, "r");
+ if (!infile) {
+ fprintf(stderr, "Error: could not open file %s for input.\n", infile);
+ return 1;
+ }
+ }
+ if (outfile != NULL) {
+ output = fopen(outfile, "w");
+ if (!output) {
+ fprintf(stderr, "Error: could not open file %s for output.\n", outfile);
+ return 1;
+ }
+ }
+
+ // Run
+ t_machine *machine = init_machine(program);
+ while (i < steps || steps == -1) {
+ read_inputs(machine, input);
+ machine_step(machine);
+ write_outputs(machine, output);
+ }
+
+ // Cleanup
+ if (input != stdin) fclose(input);
+ if (output != stdout) fclose(output);
+
+ // No need to free memory, the OS deletes everything anyways when the process ends.
+
+ return 0;
+}
+