Tunguska the ternary emulator

Balanced Ternary Numeral System - forum was moved from http://ternary.info

Moderator: haqreu

eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Tunguska the ternary emulator

Post by eudoxie »

The problem was actually very silly. I used to have these constructions like

memref("DAD", "DBC");

in the code. But that always resolves to the same numerical address, and converting from two strings, to two trytes, to one integer was pretty slow. In fact, so slow it was a major bottleneck. So, instead, I just statically compute the integer value of "DADDBC", and use it as an argument for memref instead.
User avatar
Shaos
Admin
Posts: 24011
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Tunguska the ternary emulator

Post by Shaos »

I checked out last version and it does not look working...

P.S. What is operation CAD?
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Tunguska the ternary emulator

Post by eudoxie »

That's strange. I re-checked-out the entire source tree, it compiled and ran just fine. Could be an issue with the new UI I guess, try copying share/bg.png to /usr/local/share/tunguska/bg.png

CAD is Compare ADdress. It sets the G flag according to the difference between X,Y and the specified address. Example:

LAD some_address
CAD some_other_address
JEQ some_label ; some_address is equal to some_other_address
JNE some_other_label ; not equal
User avatar
Shaos
Admin
Posts: 24011
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Tunguska the ternary emulator

Post by Shaos »

eudoxie wrote: That's strange. I re-checked-out the entire source tree, it compiled and ran just fine. Could be an issue with the new UI I guess, try copying share/bg.png to /usr/local/share/tunguska/bg.png
Background is loaded fine. Then it prints border and HELP message, but no prompt - I can't enter anything!
I tried to build it under Linux (GCC 3.4.6 and SDL 1.2.11) and under Windows (using CYGWIN) - the same results.
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Tunguska the ternary emulator

Post by eudoxie »

Odd. I've added a testcase in the latest CVS version. Run "tunguska -e", and see if you get any unexpected values.

If that doesn't work, try commenting out

floppy->heartbeat() at line 113 in tunguska.cc
or
agd->heartbeat(*m) at line 115 in tunguska.cc

And see if that does anything...
User avatar
Shaos
Admin
Posts: 24011
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Tunguska the ternary emulator

Post by Shaos »

It doesn't help...

Code: Select all

> tunguska -e

'bg.bmp' not found in '/usr/local/share/tunguska/'
Falling back to 'share/bg.bmp' ... success!
Tunguska loaded null memory, basically
rendering it a drooling vegetable :-(

It's a lot more fun with something in the
memory to execute...Running testcase
't = 10' => 10 (expect 10)
't = t+10' => 20 (expect 20)
't = -365' => 364 (expect 364)
't = 365' => -364 (expect -364)
't = 5; t = t * 2' => 10 (expect 10)
't = 5; t = t / 2' => 2 (expect 2)
't = 5; t = t % 3' => 2 (expect 2)
'm.memref(25) = 10; m.memref(0, 25) += 5' => 15 (expect 15)
'm.memref("DDD", "DDD") = 10; m.memref(-729*729/2) += 5' => 15 (expect 15)
't = 1000; t.to_int();' => 271 (expect 271)
't = -1000; t.to_int();' => -271 (expect -271)
Initial benchmark: 67.935 thousand operations per second
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Tunguska the ternary emulator

Post by eudoxie »

Hm. I have an idea, try isolating when the problem occurred by running cvs with the "-D" parameter.

Like this:

cvs [the usual sourceforge stuff] -D "4 days ago" co tunguska_sources
User avatar
Shaos
Admin
Posts: 24011
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Tunguska the ternary emulator

Post by Shaos »

eudoxie wrote: Hm. I have an idea, try isolating when the problem occurred by running cvs with the "-D" parameter.

Like this:

cvs [the usual sourceforge stuff] -D "4 days ago" co tunguska_sources
I checked out both sources folders with different -D and found that

-D "2008-03-05 13:00" returns working code
-D "2008-03-05 14:00" returns NOT working code

P.S. time is probably shifted to my time zone -0500
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Tunguska the ternary emulator

Post by eudoxie »

Could you run

cvs [... sourceforge stuff ...] co -D "2008-03-05 14:00" -P tunguska_sources
cd tunguska_sources
cvs diff -D "2008-03-05 13:00"

And paste the results here?
User avatar
Shaos
Admin
Posts: 24011
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Tunguska the ternary emulator

Post by Shaos »

Code: Select all

Index: Makefile
===================================================================
RCS file: /cvsroot/tunguska/tunguska_sources/Makefile,v
retrieving revision 1.7
retrieving revision 1.8
diff -r1.7 -r1.8
7,8c7,8
< tunguska: trit.o tryte.o machine.o tunguska.o display.o keyboard.o interrupt.o disk.o coprocessor.o
< 	g++ trit.o tryte.o machine.o tunguska.o interrupt.o display.o keyboard.o disk.o coprocessor.o -o tunguska ${LDFLAGS}
---
> tunguska: trit.o tryte.o machine.o tunguska.o display.o keyboard.o interrupt.o disk.o agdp.o
> 	g++ trit.o tryte.o machine.o tunguska.o interrupt.o display.o keyboard.o disk.o agdp.o -o tunguska ${LDFLAGS}
Index: disk.cc
===================================================================
RCS file: /cvsroot/tunguska/tunguska_sources/disk.cc,v
retrieving revision 1.9
retrieving revision 1.10
diff -r1.9 -r1.10
46c46,47
< 	tryte &d = m->memref("DDD", "DDA");
---
> 	static int offset = tryte::word_to_int("DDD", "DDA");
> 	tryte &d = m->memref(offset);
Index: display.cc
===================================================================
RCS file: /cvsroot/tunguska/tunguska_sources/display.cc,v
retrieving revision 1.9
retrieving revision 1.10
diff -r1.9 -r1.10
101c101,103
< 	tryte displaytryte = m.memref("DDD", "DDB");
---
> 	static int displaymem = tryte::word_to_int("DDD", "DDB");
> 
> 	tryte displaytryte = m.memref(displaymem);
110c112
< 	if(refresh == 1) { m.memref("DDD", "DDB")[5] = 0; }
---
> 	if(refresh == 1) { m.memref(displaymem)[5] = 0; }
Index: machine.cc
===================================================================
RCS file: /cvsroot/tunguska/tunguska_sources/machine.cc,v
retrieving revision 1.13
retrieving revision 1.15
diff -r1.13 -r1.15
74c74,75
< 	STRTOOPC(LAD); STRTOOPC(BIT); STRTOOPC(BLT); STRTOOPC(BLS);
---
> 	STRTOOPC(LAD); STRTOOPC(BIT); STRTOOPC(PGT); STRTOOPC(PGS);
> 	STRTOOPC(CAD);
93,94c94,97
< 	memref("DDD", "DDD") = i->IRQ();
< 	memref("DDD", "DDC") = i->data();
---
> 	static int memirq = tryte::word_to_int("DDD", "DDD");
> 	static int memirqdata = tryte::word_to_int("DDD", "DDC");
> 	memref(memirq) = i->IRQ();
> 	memref(memirqdata) = i->data();
119c122,123
< 	if((CL > memref("444", "441")) == 1) CL = 0;
---
> 	static int moffset = tryte::word_to_int("444", "441");
> 	if((CL > memref(moffset)) == 1) CL = 0;
261,262c265,267
< 		case BLT: blt(arg); break;
< 		case BLS: bls(arg); break;
---
> 		case PGT: pgt(arg); break;
> 		case PGS: pgs(arg); break;
> 		case CAD: cad(abs1, abs2); break;
455a461,471
> /* Compare ADdress */
> void machine::cad(const tryte* t, const tryte* t2) {
> 	int V =  tryte::word_to_int(*t, *t2);
> 	int V2 = tryte::word_to_int(X, Y);
> 
> 	int diff = V - V2;
> 	if(diff != 0) diff = diff / abs(diff);
> 
> 	pflag(diff);
> }
> 
464c480
< void machine::blt(const tryte* t) {
---
> void machine::pgt(const tryte* t) {
474c490
< void machine::bls(const tryte* t) {
---
> void machine::pgs(const tryte* t) {
Index: machine.h
===================================================================
RCS file: /cvsroot/tunguska/tunguska_sources/machine.h,v
retrieving revision 1.7
retrieving revision 1.9
diff -r1.7 -r1.9
61c61
< 			IVC, PRM, TSH, BUT, LAD, BLT, BLS
---
> 			IVC, PRM, TSH, BUT, LAD, PGT, PGS, CAD
167,168c167,170
< 		void blt(const tryte* t);
< 		void bls(const tryte* t);
---
> 		void cad(const tryte* t, const tryte* t2);
> 
> 		void pgt(const tryte* t);
> 		void pgs(const tryte* t);
Index: tunguska.cc
===================================================================
RCS file: /cvsroot/tunguska/tunguska_sources/tunguska.cc,v
retrieving revision 1.13
retrieving revision 1.15
diff -r1.13 -r1.15
26c26
< #include "coprocessor.h"
---
> #include "agdp.h"
35c35
< #define POLL_INTERVAL 5000
---
> #define POLL_INTERVAL 10000
69c69
< 		coprocessor* cop) {
---
> 		agdp* agd) {
114c114
< 		cop->heartbeat(*m);
---
> 		agd->heartbeat(*m);
117c117
< }
---
> 	}
155c155
< 	coprocessor* cop = new coprocessor();
---
> 	agdp* agd= new agdp();
192c192
< 		cycle(m, disp, floppy, keyboard, cop);
---
> 		cycle(m, disp, floppy, keyboard, agd);
204c204
< 		cycle(m, disp, floppy, keyboard, cop); 
---
> 		cycle(m, disp, floppy, keyboard, agd); 

Index: ram.asm
===================================================================
RCS file: /cvsroot/tunguska/tunguska_memory_image_sources/ram.asm,v
retrieving revision 1.20
retrieving revision 1.21
diff -r1.20 -r1.21
236c236
< 		BLS	#%DDD
---
> 		PGS	#%DDD
277c277
< 		BLS	#%DDD
---
> 		PGS	#%DDD
Index: stdlib.asm
===================================================================
RCS file: /cvsroot/tunguska/tunguska_memory_image_sources/stdlib.asm,v
retrieving revision 1.6
retrieving revision 1.7
diff -r1.6 -r1.7
627a628
> 
User avatar
Shaos
Admin
Posts: 24011
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Tunguska the ternary emulator

Post by Shaos »

When I changed in current code
static int moffset = tryte::word_to_int("444", "441");
to
int moffset = tryte::word_to_int("444", "441");
it bacames working again!

P.S. Probably you need to put all this kind of constants as class members and initialize them in constructors, because it looks like optimization mistake of compiler (you use -O3) or something like this...
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Tunguska the ternary emulator

Post by eudoxie »

Okay, try the latest CVS. I've worked around the static variable approach with compiler macros instead. That should be as fast, or even faster than static variables, since it allows the compiler to "understand" them better.

Also, what sort of speeds does the benchmark give you. I saw you post earlier with a really slow speed. It should be at least in the range 250+ on a reasonably fast computer...
User avatar
Shaos
Admin
Posts: 24011
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Tunguska the ternary emulator

Post by Shaos »

Yes, it's working now!
But scrolling looks strange
And I have benchmark 349 now
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Tunguska the ternary emulator

Post by eudoxie »

Can you elaborate, how does the scrolling look?
User avatar
Shaos
Admin
Posts: 24011
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Tunguska the ternary emulator

Post by Shaos »

eudoxie wrote: Can you elaborate, how does the scrolling look?
Before scrolling:

Image

Scrolling in progress:

Image

Result looks fine (except broken lines):

Image