diff --git a/Makefile b/Makefile index ba5b2f9..d13efa9 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,25 @@ CC=gcc +CFLAGS=-g +LDFLAGS=-g -elea: elea.o governo.o accumulatore.o memoria.o ricerca.o algebra.o telescrivente.o +elea: \ + elea.o \ + governo.o \ + accumulatore.o \ + memoria.o \ + ricerca.o \ + algebra.o \ + telescrivente.o \ + hello_world.o \ + long_division.o + +hello_world.c: hello_world.elea-txt caratteri.py + python caratteri.py $< + +long_division.c: long_division.elea-txt caratteri.py + python caratteri.py $< clean: FORCE - @rm -f elea *.o tags + @rm -f elea *.o tags hello_world.c long_division.c .PHONY: FORCE diff --git a/caratteri.py b/caratteri.py index d4ce1b7..43734af 100644 --- a/caratteri.py +++ b/caratteri.py @@ -1,77 +1,18 @@ # coding=utf8 +import sys from pprint import pprint -caratteri = [ - ('0', 'CAR_ZERO'), - ('1', 'CAR_UNO'), - ('~', 'CAR_ONDA'), - ('2', 'CAR_DUE'), - ('4', 'CAR_QUATTRO'), - ('+', 'CAR_PIU'), - ('#', 'CAR_DIESIS'), - ('3', 'CAR_TRE'), - ('5', 'CAR_CINQUE'), - ('6', 'CAR_SEI'), - ('/', 'CAR_BARRA'), - ('-', 'CAR_MENO'), - ('7', 'CAR_SETTE'), - ('9', 'CAR_NOVE'), - ('÷', 'CAR_DIVISO'), - ('8', 'CAR_OTTO'), - ('Φ', 'CAR_PHI'), - ('A', 'CAR_A'), - ('δ', 'CAR_DELTA'), - ('B', 'CAR_B'), - ('D', 'CAR_D'), - ('=', 'CAR_UGUALE'), - ('α', 'CAR_ALFA'), - ('C', 'CAR_C'), - ('E', 'CAR_E'), - ('F', 'CAR_F'), - ('%', 'CAR_PERCENTO'), - (',', 'CAR_VIRGOLA'), - ('G', 'CAR_G'), - ('I', 'CAR_I'), - ('γ', 'CAR_GAMMA'), - ('H', 'CAR_H'), - ('.', 'CAR_PUNTO'), - ('S', 'CAR_S'), - ('θ', 'CAR_TETA'), - ('T', 'CAR_T'), - ('V', 'CAR_V'), - ('!', 'CAR_ESCLAMATIVO'), - (')', 'CAR_CHIUSA_PARENTESI'), - ('U', 'CAR_U'), - ('W', 'CAR_W'), - ('X', 'CAR_X'), - ('?', 'CAR_INTERROGATIVO'), - ('&', 'CAR_E_COMMERCIALE'), - ('Y', 'CAR_Y'), - ("'", 'CAR_APOSTROFO'), - ('⊗', 'CAR_MOLTIPLICATO_PER'), - ('Z', 'CAR_Z'), - ('ε', 'CAR_EPSILON'), - ('J', 'CAR_J'), - ('*', 'CAR_ASTERISCO'), - ('K', 'CAR_K'), - ('M', 'CAR_M'), - ('(', 'CAR_APERTA_PARENTESI'), - ('β', 'CAR_BETA'), - ('L', 'CAR_L'), - ('N', 'CAR_N'), - ('O', 'CAR_O'), - ('$', 'CAR_DOLLARO'), - ('P', 'CAR_P'), - ('η', 'CAR_ETA'), - ('R', 'CAR_R'), - ('π', 'CAR_PI'), - ('Q', 'CAR_Q'), +mappa_caratteri = [ + "0", "1", "~", "2", "4", "+", "#", "3", "5", "6", "/", "7", "9", "-", "÷", "8", + "Φ", "A", "δ", "B", "D", "=", "α", "C", "E", "F", "%", "G", "I", ",", "γ", "H", + ".", "S", "θ", "T", "V", "!", ")", "U", "W", "X", "?", "Y", "\'", "&", "⊗", "Z", + "ε", "J", "*", "K", "M", "(", "β", "L", "N", "O", "$", "η", "R", "P", "π", "Q" ] caratteri_python_a_elea = { - caratteri[i][0].decode('utf-8'): caratteri[i] + (i,) - for i in xrange(len(caratteri)) } + char.decode('utf-8'): idx + for idx, char in enumerate(mappa_caratteri) } def stringa_python_a_elea(string): s = [] @@ -82,18 +23,58 @@ def stringa_python_a_elea(string): xx = caratteri_python_a_elea.get(i, None) if xx is None: - print "NO", repr(i) - continue + raise Exception("carattere %s non mappato" % i) s.append(xx) return s -hello_world = """ -11 017 # Φ -0X XXX # 0 -HELLO.WORLD +def elea_a_c(l): + l2 = ('%3d,' % i for i in l) + return ' ' + ' '.join(l2) + +def stringa_elea_a_c(l): + e = stringa_python_a_elea(l) + e2 = elea_a_c(e) + return e2 + " /* " + l + " */" + +PROLOGO = """#include "elea_tipi.h" + +const carattere %s[] = { """ -pprint(stringa_python_a_elea(hello_world)) +EPILOGO = """}; + +const int %s_lunghezza = sizeof(%s) / sizeof(carattere); + +""" + +def txt_to_c(stringa_txt, nome): + x = [] + x += (PROLOGO % nome).splitlines() + + for line in stringa_txt.splitlines(): + l2 = line.strip() + if len(l2) == 0: continue + x.append(stringa_elea_a_c(l2)) + + x += (EPILOGO % (nome, nome)).splitlines() + + return '\n'.join(x) + +def converti(nomefile): + nome = nomefile.split('.', 1)[0] + x = open(nomefile).read() + c = txt_to_c(x, nome) + open('%s.c' % nome, 'w').write(c) + +if __name__ == '__main__': + try: + nomefile = sys.argv[1] + except: + print "converte da txt in elea -> file c\n" + print "usaggio: %s nomefile.txt" + sys.exit() + + converti(nomefile) diff --git a/elea.c b/elea.c index 66a7428..85b6604 100644 --- a/elea.c +++ b/elea.c @@ -4,6 +4,12 @@ #include "memoria.h" #include "codifica_istruzioni.h" +extern const carattere hello_world[]; +extern const int hello_world_lunghezza; + +extern const carattere long_division[]; +extern const int long_division_lunghezza; + /* main */ int main(int argc, char *argv[]) { @@ -39,5 +45,9 @@ int main(int argc, char *argv[]) /* Esegue l'istruzione phi */ op_ms(&istr); + + mem_scrivi_blocco(1, hello_world, hello_world_lunghezza); + mem_scrivi_blocco(100, long_division, long_division_lunghezza); + return 0; } diff --git a/hello_world.c b/hello_world.c new file mode 100644 index 0000000..86cb9b7 --- /dev/null +++ b/hello_world.c @@ -0,0 +1,9 @@ +#include "elea_tipi.h" + +const carattere hello_world[] = { + 1, 1, 0, 0, 1, 11, 6, 16, /* 11 0017 # Φ */ + 0, 41, 41, 41, 41, 41, 6, 0, /* 0X XXXX # 0 */ + 31, 24, 55, 55, 57, 40, 57, 60, 55, 20, /* HELLO WORLD */ +}; + +const int hello_world_lunghezza = sizeof(hello_world) / sizeof(carattere); diff --git a/hello_world.elea-txt b/hello_world.elea-txt new file mode 100644 index 0000000..508459d --- /dev/null +++ b/hello_world.elea-txt @@ -0,0 +1,4 @@ +11 0017 # Φ +0X XXXX # 0 +HELLO WORLD + diff --git a/long_division.c b/long_division.c new file mode 100644 index 0000000..b04d322 --- /dev/null +++ b/long_division.c @@ -0,0 +1,37 @@ +#include "elea_tipi.h" + +const carattere long_division[] = { + 0, 0, 0, 0, 0, 0, 0, 0, /* 00 0000 0 0 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 00 0000 0 0 */ + 0, 0, 0, 0, 0, 0, 1, 58, /* 00 0000 1 $ */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 00 0000 0 0 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 00 0000 0 0 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 00 0000 0 0 */ + 0, 0, 0, 0, 0, 0, 0, 0, /* 00 0000 0 0 */ + 0, 0, 0, 0, 0, 0, 6, 17, /* 00 0000 # A */ + 1, 4, 0, 1, 9, 0, 6, 25, /* 14 0160 # F */ + 0, 1, 0, 3, 3, 9, 6, 18, /* 01 0226 # δ */ + 3, 3, 0, 1, 4, 9, 6, 25, /* 22 0146 # F */ + 0, 1, 0, 3, 1, 15, 6, 18, /* 01 0218 # δ */ + 3, 3, 6, 6, 6, 6, 6, 14, /* 22 #### # ÷ */ + 0, 0, 0, 1, 4, 9, 6, 1, /* 00 0146 # 1 */ + 14, 14, 0, 0, 13, 13, 0, 12, /* ÷÷ 00-- 0 9 */ + 14, 14, 0, 0, 13, 13, 0, 4, /* ÷÷ 00-- 0 4 */ + 14, 14, 0, 1, 4, 9, 16, 12, /* ÷÷ 0146 Φ 9 */ + 4, 0, 0, 3, 8, 0, 6, 43, /* 40 0250 # Y */ + 14, 14, 13, 13, 13, 13, 16, 4, /* ÷÷ ---- Φ 4 */ + 6, 6, 0, 1, 4, 9, 16, 8, /* ## 0146 Φ 5 */ + 35, 6, 0, 0, 1, 8, 32, 0, /* T# 0015 . 0 */ + 6, 6, 6, 6, 0, 0, 0, 17, /* ## ##00 0 A */ + 1, 4, 0, 1, 9, 0, 6, 31, /* 14 0160 # H */ + 40, 6, 0, 7, 3, 4, 6, 0, /* W# 0324 # 0 */ + 58, 61, 0, 1, 3, 7, 6, 14, /* $P 0123 # ÷ */ + 0, 0, 0, 0, 0, 0, 16, 7, /* 00 0000 Φ 3 */ + 38, 6, 0, 3, 15, 4, 6, 0, /* )# 0284 # 0 */ + 1, 4, 0, 1, 9, 0, 6, 23, /* 14 0160 # C */ + 14, 14, 0, 0, 0, 1, 0, 4, /* ÷÷ 0001 0 4 */ + 14, 14, 0, 0, 0, 1, 16, 5, /* ÷÷ 0001 Φ + */ + 38, 6, 0, 1, 9, 0, 6, 0, /* )# 0160 # 0 */ +}; + +const int long_division_lunghezza = sizeof(long_division) / sizeof(carattere); diff --git a/long_division.elea-txt b/long_division.elea-txt new file mode 100644 index 0000000..db0bfb9 --- /dev/null +++ b/long_division.elea-txt @@ -0,0 +1,32 @@ +00 0000 0 0 +00 0000 0 0 +00 0000 1 $ +00 0000 0 0 +00 0000 0 0 +00 0000 0 0 +00 0000 0 0 +00 0000 # A +14 0160 # F +01 0226 # δ +22 0146 # F +01 0218 # δ +22 #### # ÷ +00 0146 # 1 +÷÷ 00-- 0 9 +÷÷ 00-- 0 4 +÷÷ 0146 Φ 9 +40 0250 # Y +÷÷ ---- Φ 4 +## 0146 Φ 5 +T# 0015 . 0 +## ##00 0 A +14 0160 # H +W# 0324 # 0 +$P 0123 # ÷ +00 0000 Φ 3 +)# 0284 # 0 +14 0160 # C +÷÷ 0001 0 4 +÷÷ 0001 Φ + +)# 0160 # 0 + diff --git a/memoria.c b/memoria.c index c4c2c2b..4af41bd 100644 --- a/memoria.c +++ b/memoria.c @@ -39,3 +39,10 @@ void mem_scrivi_carattere(uint32_t ind, carattere c) { M[ind] = c; } + +void mem_scrivi_blocco(uint32_t ind, const carattere * blocco, int lunghezza) +{ + for (int i = 0; i < lunghezza; i++) + mem_scrivi_carattere(ind + i, blocco[i]); +} + diff --git a/memoria.h b/memoria.h index 69f150e..f592274 100644 --- a/memoria.h +++ b/memoria.h @@ -10,5 +10,6 @@ int op_meno_mm(istruzione *i); /* Accesso diretto alla memoria */ carattere mem_leggi_carattere(uint32_t ind); void mem_scrivi_carattere(uint32_t ind, carattere c); +void mem_scrivi_blocco(uint32_t ind, const carattere * blocco, int lunghezza); #endif