Skip to content

Commit 1e6af4c

Browse files
committed
Initial commit working ternary computer
0 parents  commit 1e6af4c

11 files changed

+1348
-0
lines changed

.gitignore

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
cmake-build-debug/
2+
.idea/
3+
4+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
5+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
6+
7+
# User-specific stuff:
8+
.idea/**/workspace.xml
9+
.idea/**/tasks.xml
10+
.idea/dictionaries
11+
12+
# Sensitive or high-churn files:
13+
.idea/**/dataSources/
14+
.idea/**/dataSources.ids
15+
.idea/**/dataSources.xml
16+
.idea/**/dataSources.local.xml
17+
.idea/**/sqlDataSources.xml
18+
.idea/**/dynamic.xml
19+
.idea/**/uiDesigner.xml
20+
21+
# Gradle:
22+
.idea/**/gradle.xml
23+
.idea/**/libraries
24+
25+
# CMake
26+
cmake-build-debug/
27+
28+
# Mongo Explorer plugin:
29+
.idea/**/mongoSettings.xml
30+
31+
## File-based project format:
32+
*.iws
33+
34+
## Plugin-specific files:
35+
36+
# IntelliJ
37+
/out/
38+
39+
# mpeltonen/sbt-idea plugin
40+
.idea_modules/
41+
42+
# JIRA plugin
43+
atlassian-ide-plugin.xml
44+
45+
# Cursive Clojure plugin
46+
.idea/replstate.xml
47+
48+
# Crashlytics plugin (for Android Studio and IntelliJ)
49+
com_crashlytics_export_strings.xml
50+
crashlytics.properties
51+
crashlytics-build.properties
52+
fabric.properties

CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.9)
2+
project(TernaryVM)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
set(SOURCE_FILES main.cpp virtual_machine.cpp virtual_machine.h tryte.cpp tryte.h ternary_operators.cpp ternary_operators.h rotate.h ternaryparser.h)
7+
add_executable(TernaryVM ${SOURCE_FILES})

main.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
3+
#include <cstdint>
4+
#include <bitset>
5+
#include "virtual_machine.h"
6+
#include <vector>
7+
#include <tuple>
8+
9+
int main() {
10+
std::cout << "hello world" << std::endl;
11+
tvm::VirtualMachine vm(48, 48, 12);
12+
std::vector<std::tuple<tvm::Tryte, tvm::Tryte, tvm::Tryte>> program;
13+
{
14+
using namespace tvm;
15+
program.push_back(enumToCode(TVM_ISA::MOVRI, 000010_tryte, 000010_tryte));
16+
program.push_back(enumToCode(TVM_ISA::MOVRI, 000011_tryte, 000210_tryte));
17+
program.push_back(enumToCode(TVM_ISA::SUBR, 000011_tryte, 000010_tryte));
18+
program.push_back(enumToCode(TVM_ISA::EXIT, 000000_tryte, 000000_tryte));
19+
}
20+
vm.loadProgram(program);
21+
vm.run();
22+
23+
24+
return 0;
25+
}

rotate.h

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// Created by Shae Bolt on 8/12/2017.
3+
//
4+
5+
#ifndef TERNARYVM_ROTATE_H
6+
#define TERNARYVM_ROTATE_H
7+
8+
#include <climits>
9+
#include <cassert>
10+
11+
template <typename UIntType>
12+
constexpr UIntType rotl_t (UIntType n, unsigned int c)
13+
{
14+
const unsigned int mask = (CHAR_BIT*sizeof(n) - 1); // assumes width is a power of 2.
15+
16+
// assert ( (c<=mask) &&"rotate by type width or more");
17+
c &= mask;
18+
return (n<<c) | (n>>( (-c)&mask ));
19+
}
20+
template <typename UIntType>
21+
constexpr UIntType rotr_t (UIntType n, unsigned int c)
22+
{
23+
const unsigned int mask = (CHAR_BIT*sizeof(n) - 1);
24+
25+
// assert ( (c<=mask) &&"rotate by type width or more");
26+
c &= mask;
27+
return (n>>c) | (n<<( (-c)&mask ));
28+
}
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
62+
63+
64+
65+
66+
67+
68+
69+
#endif //TERNARYVM_ROTATE_H

ternary_operators.cpp

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
//
2+
// Created by Shae Bolt on 8/11/2017.
3+
//
4+
5+
#include "ternary_operators.h"
6+
7+
namespace tvm {
8+
9+
10+
std::uint64_t native_to_10_encoded_ternary(std::uint64_t ternary_bits) {
11+
return ((ternary_bits & ALT_101010) >> 1) ^ ternary_bits;
12+
}
13+
14+
std::uint64_t ternary_coded_bin_to_bin(std::uint64_t bits) {
15+
std::uint64_t acc = native_to_10_encoded_ternary(bits);
16+
acc = acc - (((acc >> 2) & 0x3333333333333333) * (4 - 3));
17+
acc = acc - (((acc >> 4) & 0x0F0F0F0F0F0F0F0F) * (16 - 9));
18+
acc = acc - (((acc >> 8) & 0x00FF00FF00FF00FF) * (256 - 81));
19+
acc = acc - (((acc >> 16) & 0x0000FFFF0000FFFF) * (65536 - 6561));
20+
acc = acc - (((acc >> 32) & 0x00000000FFFFFFFF) * (4294967296ULL - 43046721));
21+
return acc;
22+
}
23+
24+
std::uint64_t isolate_uknown_op(std::uint64_t bits_110100) {
25+
std::uint64_t conv_bits = native_to_10_encoded_ternary(bits_110100);
26+
return conv_bits & ALT_010101;
27+
}
28+
29+
std::uint64_t not_op(std::uint64_t bits_110100) {
30+
std::uint64_t inv_bits_001011 = ~bits_110100;
31+
std::uint64_t isolated_01 = isolate_uknown_op(bits_110100);
32+
std::uint64_t isolated_shr_10 = isolated_01 << 1;
33+
std::uint64_t removed_u = inv_bits_001011 ^isolated_shr_10;
34+
return removed_u ^ isolated_01;
35+
}
36+
37+
//alternate way, but slower
38+
// std::uint64_t xor_op(std::uint64_t lhs, std::uint64_t rhs){
39+
// return (lhs & not_op(rhs)) | (not_op(lhs) & rhs);
40+
// }
41+
std::uint64_t xor_op(std::uint64_t lhs_110100, std::uint64_t rhs_001101) {
42+
std::uint64_t lhs_010100 = lhs_110100 & ALT_010101;
43+
std::uint64_t rhs_000101 = rhs_001101 & ALT_010101;
44+
std::uint64_t lrs_000100 = lhs_010100 & rhs_000101;
45+
std::uint64_t lrs_001100 = lrs_000100 | (lrs_000100 << 1);
46+
std::uint64_t lrs_110011 = ~lrs_001100;
47+
std::uint64_t xor_111001 = lhs_110100 ^rhs_001101;
48+
std::uint64_t xor_110001 = xor_111001 & lrs_110011;
49+
return xor_110001 | lrs_000100;
50+
}
51+
52+
std::uint64_t shl_op(std::uint64_t bits, std::uint64_t shl_amount) {
53+
54+
std::uint64_t bin_shl_amount = ternary_coded_bin_to_bin(shl_amount);
55+
return bits << (bin_shl_amount * 2);
56+
}
57+
58+
std::uint64_t shr_op(std::uint64_t bits, std::uint64_t shr_amount, std::uint64_t mask) {
59+
std::uint64_t masked_bits = bits & mask;
60+
std::uint64_t bin_shr_amount = ternary_coded_bin_to_bin(shr_amount);
61+
return masked_bits >> (bin_shr_amount * 2);
62+
}
63+
64+
std::uint64_t not_false_op(std::uint64_t bits_110100) {
65+
std::uint64_t bits_010100 = bits_110100 & ALT_010101;
66+
return bits_010100 | (bits_010100 << 1);
67+
}
68+
69+
std::uint64_t not_true_op(std::uint64_t bits_110100) {
70+
std::uint64_t bits_001010 = ~bits_110100 & ALT_101010;
71+
return bits_001010 | (bits_001010 >> 1);
72+
}
73+
74+
std::uint64_t is_unknown_op(std::uint64_t bits) {
75+
std::uint64_t isolated_unknown = isolate_uknown_op(bits);
76+
return isolated_unknown | (isolated_unknown << 1);
77+
}
78+
79+
std::uint64_t not_unknown_op(std::uint64_t bits_110100) {
80+
return ~is_unknown_op(bits_110100);
81+
}
82+
83+
std::uint64_t is_true_op(std::uint64_t bits_110100) {
84+
std::uint64_t bits_100000 = bits_110100 & ALT_101010;
85+
return bits_100000 | (bits_100000 >> 1);
86+
}
87+
88+
std::uint64_t unknown_only_true_op(std::uint64_t bits_110100) {
89+
std::uint64_t bits_100000 = bits_110100 & ALT_101010;
90+
return (bits_100000 >> 1);
91+
}
92+
93+
std::uint64_t unknown_only_false_op(std::uint64_t bits_110100) {
94+
std::uint64_t bits_000001 = ~bits_110100 & ALT_010101;
95+
return bits_000001;
96+
}
97+
98+
std::uint64_t is_false_op(std::uint64_t bits_110100) {
99+
std::uint64_t bits_000001 = ~bits_110100 & ALT_010101;
100+
return bits_000001 | (bits_000001 << 1);
101+
}
102+
103+
std::uint64_t sharkfin_op(std::uint64_t lhs, std::uint64_t rhs) {
104+
std::uint64_t isolate_TT = is_true_op(lhs & rhs);
105+
std::uint64_t isolate_TT_U = unknown_only_true_op(isolate_TT);
106+
std::uint64_t isolate_UOR = isolate_uknown_op(lhs | rhs);
107+
108+
109+
std::uint64_t alt_true_false_lhs = (lhs ^ ALT_010101);
110+
std::uint64_t alt_true_false_rhs = (rhs ^ ALT_010101);
111+
std::uint64_t is_unk_lhs = is_unknown_op(lhs);
112+
std::uint64_t is_unk_rhs = is_unknown_op(rhs);
113+
std::uint64_t is_unk = is_unk_lhs | is_unk_rhs;
114+
115+
std::uint64_t tf_11 = (alt_true_false_lhs ^ alt_true_false_rhs) & ~(is_unk);
116+
std::uint64_t uu_11 = is_unk_lhs & is_unk_rhs;
117+
std::uint64_t all_11 = tf_11 | uu_11;
118+
// std::uint64_t lhs_010100 = lhs & ALT_010101;
119+
// std::uint64_t rhs_010100 = ~rhs & ALT_101010;
120+
// std::uint64_t lrs = lhs_010100 & (rhs_010100 >> 1);
121+
// std::uint64_t lrs_11 = lrs | (lrs << 1);
122+
return isolate_TT_U | isolate_UOR | all_11;
123+
}
124+
125+
std::uint64_t find_carry_op(std::uint64_t lhs, std::uint64_t rhs) {
126+
std::uint64_t notf_and = not_false_op(lhs & rhs);
127+
std::uint64_t true_or = is_true_op(lhs | rhs);
128+
return (notf_and & true_or) & ALT_010101;
129+
}
130+
131+
std::uint64_t add_op(std::uint64_t x, std::uint64_t y){
132+
while(y != 0){
133+
std::uint64_t carry = find_carry_op(x, y);
134+
135+
x = sharkfin_op(x,y);
136+
137+
y = shl_op(carry, 1);
138+
}
139+
return x;
140+
}
141+
142+
std::uint64_t sub_op(std::uint64_t x, std::uint64_t y){
143+
std::uint64_t y_comp = add_op(not_op(y), 1);
144+
return add_op(x, y_comp);
145+
}
146+
147+
std::uint64_t comp_op(std::uint64_t lhs, std::uint64_t rhs){
148+
std::uint64_t ret = 1;
149+
if(lhs < rhs){
150+
ret = 0;
151+
}
152+
else if(lhs > rhs){
153+
ret = 3;
154+
}
155+
return ret;
156+
}
157+
std::uint64_t rotl_op(std::uint64_t bits, std::uint64_t rotate_amount, std::uint64_t mask, std::uint64_t max_bits){
158+
std::uint64_t lower_bits = shr_op(bits, max_bits - rotate_amount, mask);
159+
std::uint64_t upper_bits = shl_op(bits, rotate_amount);
160+
return lower_bits | upper_bits;
161+
}
162+
163+
std::uint64_t rotr_op(std::uint64_t bits, std::uint64_t rotate_amount, std::uint64_t mask, std::uint64_t max_bits){
164+
std::uint64_t lower_bits = shr_op(bits, rotate_amount, mask);
165+
std::uint64_t upper_bits = shl_op(bits, max_bits - rotate_amount);
166+
return lower_bits | upper_bits;
167+
}
168+
}

0 commit comments

Comments
 (0)