forked from code4fukui/Wirth
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ and.nor,4. and | |
halfadd.nor,5. half add | ||
fulladd.nor,6. full add | ||
add.nor,7. 4bit add | ||
sub.nor,8. 4bit sub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# 8. 4bit sub | ||
function not(a) | ||
return a nor a | ||
end | ||
|
||
function or(a, b) | ||
return not(a nor b) | ||
end | ||
|
||
function and(a, b) | ||
return not(a) nor not(b) | ||
end | ||
|
||
function halfadd(a, b) | ||
c = and(a, b) | ||
s = and(or(a, b), not(c)) | ||
return [c, s] | ||
end | ||
|
||
function fulladd(a, b, x) | ||
cs1 = halfadd(a, b) | ||
cs2 = halfadd(cs1[1], x) | ||
c = or(cs1[0], cs2[0]) | ||
s = cs2[1] | ||
return [c, s] | ||
end | ||
|
||
function add(a, b) | ||
x = 0 | ||
s = [0, 0, 0, 0] | ||
for i = 3 to 0 step -1 | ||
cs = fulladd(a[i], b[i], x) | ||
x = cs[0] | ||
s[i] = cs[1] | ||
next | ||
return s | ||
end | ||
|
||
function complement(a) | ||
s = [0, 0, 0, 0] | ||
for i = 0 to 3 | ||
s[i] = not(a[i]) | ||
next | ||
return add(s, [0, 0, 0, 1]) | ||
end | ||
|
||
function sub(a, b) | ||
return add(a, complement(b)) | ||
end | ||
|
||
a = [1, 0, 1, 0] # 10 | ||
b = [0, 1, 1, 1] # 7 | ||
print complement(a) | ||
print sub(a, b) |