Ternary base representation

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

Moderator: haqreu

Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary base representation

Post by Mac Buster »

First of all -> Happy new year! :lol:
I'm not sure if a parity flag is going to be necessary.
I think the flag could be useful in some applications (e.g. some math programming), since it makes calculations easier.
There is an operation that allows you to set the status flags as though a tritwise AND operation with the accumulator has been performed. With an and-mask that masks out all trits except one arbitrary trit, you will transfer that trit to the sign flag, and from there -- for all intents and purposes, it can be a parity flag.
Wow. I don't know a way to determine number's parity by a single trit (I can only determine sign of a number in this manner). If you can give me an example of it, please post it here. In ternary you can not check single trit and say the number's parity (neverming how you count - even, odd and zero, or just even and odd).
I've got a few things to fix before I release a public alpha version of it. It shouldn't be -that- far away, things are in fairly working order, -- maybe a month? (optimistically) But when that's done, I'll be sure to post a notification here, and you're welcome to tinker in the code (it's going to be open source).
Great news, I'll wait for release :-D
User avatar
Shaos
Admin
Posts: 23989
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Ternary base representation

Post by Shaos »

In 8080 microprocessor parity flag means fact of even number of ones in binary representation of accumulator. I don't see how we may use it in ternary (probably we can come up with some kind of "trinity" flag instead of "parity" to check for errors in ternary represented data).

In case of ternary, more sense is to have regular parity in mathematical meaning (multiple of two), because it's not too easy to calculate mathematical parity using balanced ternary representation of number.
User avatar
Shaos
Admin
Posts: 23989
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Ternary base representation

Post by Shaos »

Shaos wrote: In 8080 microprocessor parity flag means fact of even number of ones in binary representation of accumulator. I don't see how we may use it in ternary (probably we can come up with some kind of "trinity" flag instead of "parity" to check for errors in ternary represented data).

In case of ternary, more sense is to have regular parity in mathematical meaning (multiple of two), because it's not too easy to calculate mathematical parity using balanced ternary representation of number.
So in case of balanced ternary it should be 2 separate flags:

1) parity of accumulator in mathematical sense - N (unknown/error), O (odd), P (even)
2) "trinity" flag to check errors (1-trit checksum) - lowest trit of sum of all trits of accumulator

Actually parity (odd or even) can be calculated by summarizing of all trits of number and then summarizing all trits of sum and so on until we get 1 trit that should have meaning of "even" for O or "odd" for P/N...
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary base representation

Post by eudoxie »

I implemented a parity flag now. Which is good, since now I don't have a spare flag in the processor status tryte.
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary base representation

Post by Mac Buster »

Is there any tool to write code, compile and debug samples for your CPU ? Or should I use a standard 6502 cross assembler ?
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary base representation

Post by eudoxie »

It is not binary compatible with regular 6502, so I've written a cross assembler for the machine. It's a bit makeshift at this point, but it gets the job done. The code syntax is a slightly modified 6502-syntax.

To get you an idea of how it looks, this is a test-file I put together as a part of the debugging process (pardon the lenth):

Code: Select all

ORG %DDDDDD 			;  Beginning of memory
@irq		DT $00
@irq_data	DT $00

ORG %DDDDDB
@redraw 	DT $00		; Screen repaints when different from 0
; -------------------------------------------------------------
ORG %DDCDDD			; Stack page
; --------------------------------------------------------------
ORG %DDBDDD		; Screen buffer
@screen 	
		DT 'INIT'
ORG %DDA000
@screen_page2

ORG %DD0DDD
@endscreen			; End of screen	

; ---------------------------------------------------------------

ORG %000000			; Initial PC value
		LAD @some_text	; Load address into X and Y
		JSR @puts
		
		LAD @some_text
		JSR @strlen
		DEBUG
@start		
		JMP @start
@some_text	DT 'HELLO' , $0
@other_text     DT 'BLA', $0


; ---------------------------------------------------------------

ORG 		%333DDD

; Semi-universal text cursor

@cursor		DT %4AD
@kbuf_length	DT $0
@keybuffer	REST $40
@kb_null	DT $0

;  ----
;   Feed screen down one line
;   Arguments: None
;
;   Not pretty, but does the work
;  ----

@fs_to	
@fs_to_high	DT %DDB
@fs_to_low	DT %DDD
@fs_from
@fs_from_high	DT %DDB 
@fs_from_low	DT %D2D

@feedscreen
		PHA			; Save A

		LDA #%4AD
		STA @cursor		; Restore cursor

		LDA #%DDB		; Set from and to- page to %DDB
		STA @fs_to_high
		STA @fs_from_high
		LDA #%DDD		
		STA @fs_to_low		; Set to index
		LDA #%D2D	
		STA @fs_from_low	; Set from index

		SEI			; Mask interrupts
					; This will stop redrawing
@fs_loop
		LDA (@fs_from)		; Transfer from -> to
		STA (@fs_to)

		LDA @fs_from_high	; Increase from pointer by 1
		INC @fs_from_low
		ADD #%000
		STA @fs_from_high

		LDA @fs_to_high		; Increase to pointer by 1
		INC @fs_to_low
		ADD #%000
		STA @fs_to_high

		LDA @fs_to_high		; Loop if not on end-page
		CMP #%DD0
		JNE @fs_loop

		CLI			; Resume interrupts
		INC @redraw		; Redraw screen

		PLA			; Restore A
		RST			; Return


; ----------------------------------
;  Put string
;  Arguments: String page (X), String offset (Y)
;  String is 0-ended
;  Uses @cursor
; ----------------------------------
@ps_string
@ps_page	DT $0
@ps_offset	DT $0

@puts
		SEI		; Disable interrupts
		PHA

		STX @ps_page	; Store string in memory
		STY @ps_offset	

		LDX @cursor	; 

@ps_loop
		LDA (@ps_string)	
		JEQ @ps_done		; Continue until null

		STA @screen_page2,X	; Put char on screen (still in A)

		INX			; Increase cursor

		JVC +$5			; Skip scrolling if still on line
		LDX #%4AD		; Restore cursor to beginning of line
		JSR @feedscreen		; Scroll down

		LDA @ps_page
		INC @ps_offset
		ADD #$0			; Add with carry
		STA @ps_page

		JMP @ps_loop		;
@ps_done
		STX @cursor		; Save cursor position
		PLA			; Restore accumulator
		CLI			; Restore interrupts
		INC @redraw		; Redraw screen
		RST			; Return

; -------------------------------------
; String comparison
; Arguments:
;   (pushed in this order)
;   String1 page (stack), String1 offset (stack), 
;   String2 page (stack), String2 offset (stack)
; Returns
;   0 if equal (in acc)
; -------------------------------------

@sc_str1
@sc_str1page DT $0
@sc_str1offs DT $0
@sc_str2
@sc_str2page DT $0
@sc_str2offs DT $0

@strcmp
		PLX	; Save return address
		PLY	

		PLA	; Get string addresses
		STA @sc_str2offs
		PLA
		STA @sc_str2page
		PLA
		STA @sc_str1offs
		PLA
		STA @sc_str1page

		PHY	; Restore return address
		PHX

@strcmp_loop
		LDA (@sc_str1)
		CMP (@sc_str2)
		JNE @strcmp_mismatch

		LDA (@sc_str1)
		JEQ @strcmp_match
	
		LDA @sc_str1page
		INC @sc_str1offs
		ADD #$0
		STA @sc_str1page
	
		LDA @sc_str2page
		INC @sc_str2offs
		ADD #$0
		STA @sc_str2page

		JMP @strcmp_loop
		
@strcmp_mismatch
		DEBUG
		PLX	; Save return address
		PLY
	
		LDA #$1	; Return 1
	
		PHY	; Restore return address
		PHX
	
		RST	; Return
@strcmp_match
		PLX	; Save return address
		PLY
	
		LDA #$0	; Return 0
	
		PHY	; Restore return address
		PHX
	
		RST	; Return
	
; ------------------------------------
;  String length
;  Arguments: string page (X), string offset (Y)
;  Returns: string length (A)
; ------------------------------------
@sl_str
@sl_page 	DT $0
@sl_offset 	DT $0

@strlen
		STX	@sl_page
		STY	@sl_offset
		LDX	#$0
@sl_loop
		LDA	(@sl_str)
		JEQ	@sl_done

		LDA	@sl_page
		INC	@sl_offset
		ADD	#$0
		STA	@sl_page

		INX

		JMP	@sl_loop
@sl_done
		RST

ORG %444DDD
; Interrupt handler
; (This needs to be fairly breief, since it only gets just under one page)
;
@interrupthandler
		LDA @irq	; Load IRQ into A
		CMP #$0		; IRQ = 0 => Keyboard
		JEQ @keyboard
		CMP #$1		; IRQ = 1 => Clock
		JEQ @clock

		; Unknown interrupt
		LAD @unkmsg
		JSR @puts 
		JSR @feedscreen
		RTI

@clock		INC @redraw	; Tell the screen to redraw
		RTI
; Keyboard interrupt
@keyboard	LDA @irq_data
		CMP #$1			; Space
		JEQ @kb_disp
		CMP #$9			; > 9 => Regular symbol
		JGT @kb_disp

		CMP #$2			; New line
		JEQ @kb_newline

@kb_huh		RTI

@kb_disp	LDA @irq_data		; Print to screen
		STA @printbuf
		LAD @printbuf
		JSR @puts


		LDA @kbuf_length	; Buffer only holds 39 chars
		CMP #$39
		JGT @kb_huh
		TAX

		LDA @irq_data		; Load input to A
		STA @keybuffer,X	; Put it in buffer
		INX			; Increase buffer length
		LDA #$0
		STA @keybuffer,X	; Zero-terminate string
		STX @kbuf_length	; Save buffer length

		RTI
@kb_newline
		JSR @feedscreen		; Scroll the screen down

		LDX @kbuf_length
		LDA #$0
		STA @keybuffer,X
		STA @kbuf_length	; Reset buf length to zero
		LAD @keybuffer		; 
		JSR @puts

		RTI
		

@printbuf	DT $0 $0
@unkmsg		DT 'UNKNOWN INTERRUPT' , $0

; ----------------------------------------------------------------

ORG %444441
		DT %444		; Set clock to once every 364 instructions
ORG %444442
@irvector 	DT %444 %DDD	; Interrupt handler vector
ORG %444444
@endcode			; End of memory
The assembler is in a pretty finished state, all that I really feel is lacking is local labels (it's pretty obvious how much of a mess the code becomes without them).
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary base representation

Post by Mac Buster »

To get you an idea of how it looks, this is a test-file I put together as a part of the debugging process (pardon the lenth):
Thanks for example! Fairly clear and not much complicated code :)
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary base representation

Post by eudoxie »

Just curious: What OS are you guys using? I've written my machine on Linux, but it shouldn't take that much convincing to get it running on Windows and BSD systems. It's all standard C++ and all I/O is either standard C functions or SDL (which is available on most platforms.)
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary base representation

Post by Mac Buster »

I can use Windows, Linux (some different ones) and Mac OS X.
User avatar
Shaos
Admin
Posts: 23989
Joined: 08 Jan 2003 23:22
Location: Silicon Valley

Re: Ternary base representation

Post by Shaos »

I'm using DOS (real FreeDOS or DosBox under Linux), Windows (XP or Wine under Linux), Linux (Slackware for x86 or Debian for PowerPC G3/G4) and MacOS X 10.4 on MacBook G4 ;)
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary base representation

Post by eudoxie »

Ok, just wondering how necessary it would be to get a Windows port working at this stage. Not extremely important, it seems.
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary base representation

Post by Mac Buster »

Not much need indeed.
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary base representation

Post by eudoxie »

Just a little progress report: Things are going quite well.

The virtual machine part has been behaving less and less buggy as of late, so now I'm really just tweaking the assembler. It now has (while the code for the assembler is horrifying) most of the features you would expect out of an assembler, including local labels and inclusion of other source-files :-)
Mac Buster
Retired
Posts: 1474
Joined: 03 Aug 2003 22:37
Location: Moscow

Re: Ternary base representation

Post by Mac Buster »

Great! Does your machine have any kind of GUI or just uses a terminal mode ?
eudoxie
Maniac
Posts: 277
Joined: 17 Sep 2012 13:36
Location: 81.170.128.52

Re: Ternary base representation

Post by eudoxie »

It has a minimalist GUI, which really isn't a GUI, but a "monitor emulator". It's just a single window that functions like a computer monitor. No buttons or anything to click.

I went with this because I hope to add different display modes in the future (right now I only have a text mode, but I hope to add at least vector graphics some time in the future.)

But this doesn't mean you -have- to run it from X, with a nicely configured SDL, you can also run it on the framebuffer.