summaryrefslogtreecommitdiff
path: root/csim/util.c
diff options
context:
space:
mode:
authorAlex AUVOLAT <alex.auvolat@ens.fr>2013-11-11 17:30:46 +0100
committerAlex AUVOLAT <alex.auvolat@ens.fr>2013-11-11 17:30:46 +0100
commitcc78e19aa9e6b7c8ec0e1e5ac26685e57406c69d (patch)
tree1adcf146d6a649e92fe4aa13232d70f143dc448b /csim/util.c
parentaca9d65df0a04d5161ec3485659bbb974b802fb0 (diff)
downloadSystDigit-Projet-cc78e19aa9e6b7c8ec0e1e5ac26685e57406c69d.tar.gz
SystDigit-Projet-cc78e19aa9e6b7c8ec0e1e5ac26685e57406c69d.zip
Added ROM support, coded a 24/60/60 watch with output for 7-bit LCD display
Diffstat (limited to 'csim/util.c')
-rw-r--r--csim/util.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/csim/util.c b/csim/util.c
new file mode 100644
index 0000000..a09e959
--- /dev/null
+++ b/csim/util.c
@@ -0,0 +1,48 @@
+/*
+ Système Digital
+ 2013-2014
+ Alex AUVOLAT
+
+ util.c Various utility functions used elsewhere
+*/
+
+
+#include "sim.h"
+
+int pow2(int exp) {
+ if (exp == 0) return 1;
+ if (exp == 1) return 2;
+ int k = pow2(exp / 2);
+ return (exp % 2 == 0 ? k * k : 2 * k * k);
+}
+
+t_value read_bool(FILE *stream, t_value *mask) {
+ t_value r = 0;
+ t_value pow = 1;
+
+ char c;
+ if (mask != NULL) *mask = 0;
+
+ for(;;) {
+ fscanf(stream, "%c", &c);
+ if (c == '1') {
+ r |= pow;
+ } else if (c != '0') {
+ break;
+ }
+ if (mask != NULL) (*mask) |= pow;
+
+ pow *= 2;
+ }
+
+ return r;
+}
+
+int is_prefix(char *prefix, char *str) {
+ while (*prefix) {
+ if (*prefix != *str) return 0;
+ prefix++;
+ str++;
+ }
+ return 1;
+}