Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gray Binary Converter #54

Merged
merged 79 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 72 commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
73a0375
resolves rohd, rohd-hcl to local path
quekyj Jun 7, 2023
286e3b5
Added the web app
quekyj Jun 8, 2023
52317b3
commit only the web section of code
quekyj Jun 20, 2023
fdf7de5
Add the basic functionality
quekyj Jun 21, 2023
bfa6616
Rotate POC is ready
quekyj Jun 22, 2023
9182add
commentted out print
quekyj Jun 22, 2023
b16041c
saved the components
quekyj Jun 22, 2023
5e34692
Modified the file structure of components
quekyj Jun 22, 2023
5ee2906
delete old rohd web app
quekyj Jun 22, 2023
6a1c44f
remove sample component and add arbiter
quekyj Jun 22, 2023
fdbc08a
Merge branch 'main' into web-app
quekyj Jun 22, 2023
f1e4e7c
Added Ripple Carry Adder components
quekyj Jun 22, 2023
d61ed8a
Added pipeline integer multiplier
quekyj Jun 22, 2023
4656ea0
Added sort generator
quekyj Jun 22, 2023
255b78e
Created Sidebar for webapp
quekyj Jun 30, 2023
0c70836
Added font and provider app state
quekyj Jul 3, 2023
19bf47b
Update state management to cubit
quekyj Jul 5, 2023
2b7ccde
Fixed layout
quekyj Jul 5, 2023
93ad952
Deleted Dart Web App implementation
quekyj Jul 5, 2023
df22e69
Added default value for ConfigGenerator
quekyj Jul 5, 2023
5e436ce
Add title, Fix Text style
quekyj Jul 5, 2023
4a9326a
bloc design
quekyj Jul 26, 2023
23a5482
Added unit test for pipeline integer multiplier
quekyj Jul 26, 2023
6417f44
Add carry save multiplier and custom width test
quekyj Jul 26, 2023
7b6b0bc
Added unit test for the generator
quekyj Jul 31, 2023
9250b28
Fix UI issue - Small box
quekyj Jul 31, 2023
7de7a26
Added widget test
quekyj Aug 1, 2023
2aafacb
Use material icon instead of flutter
quekyj Aug 1, 2023
b636157
Added widget tree doc in readme
quekyj Aug 1, 2023
6521cc1
removed confapp dart web app
quekyj Aug 1, 2023
0d01867
Add github action to deploy
quekyj Aug 1, 2023
6da7b95
Update pubspec to use github link instead of relative path
quekyj Aug 1, 2023
1b1bfd5
update naming and change to sh files
quekyj Aug 1, 2023
5aa584a
update the permission of the bash files
quekyj Aug 1, 2023
fa14148
update dart sdk version
quekyj Aug 1, 2023
80eea69
update flutter pubspec sdk version
quekyj Aug 1, 2023
21adaaf
update dart version
quekyj Aug 1, 2023
caedbdf
update flutter sdk version
quekyj Aug 1, 2023
d29ae1d
Fix path
quekyj Aug 1, 2023
a6301d9
Removed unnesarry steps
quekyj Aug 1, 2023
1e9a7b6
change docs to website branch
quekyj Aug 1, 2023
bbaa6a9
enclosed bare url
quekyj Aug 1, 2023
d27640e
removed side_navigation
quekyj Aug 1, 2023
7129cb7
Add run check
quekyj Aug 1, 2023
3612206
Trying to fix blank page for flutter
quekyj Aug 1, 2023
4c5c37c
Use --profile instead of release
quekyj Aug 1, 2023
4cdafb7
Add dependency
quekyj Aug 1, 2023
56502a7
update the location of the repository
quekyj Aug 7, 2023
0bc9978
exclude confapp_flutter
quekyj Aug 7, 2023
153cf80
Fix flutter analyze
quekyj Aug 7, 2023
796b45c
Fix issue on text overflow become white
quekyj Aug 7, 2023
ac7bfb1
Removed the note on readme
quekyj Aug 7, 2023
12e273b
Update the deployment path
quekyj Aug 7, 2023
2b0ee84
Add binary to gray implmentation first draft
quekyj Oct 2, 2023
b1d2e49
Add SPDX
quekyj Oct 2, 2023
787e441
Add Case to the module
quekyj Oct 3, 2023
879838f
Added binary gray implementation for HCL
quekyj Oct 5, 2023
726eae5
Add missing 0 test case
quekyj Oct 5, 2023
6efa05b
clean up the old stash
quekyj Oct 5, 2023
83ffd8e
Remove flutter stuff stash
quekyj Oct 5, 2023
782f5f0
Merge branch 'main' into example/gray-to-binary
quekyj Oct 5, 2023
015a00a
Removed stash flutter files
quekyj Oct 5, 2023
9ecd333
Removed all workflow stuff
quekyj Oct 5, 2023
9ba25fc
removed memory stuff
quekyj Oct 5, 2023
30e7cbc
remove extra space
quekyj Oct 5, 2023
c3b3af2
remove extra space
quekyj Oct 5, 2023
9296696
Use LogicValue instead of Logic in software .map
quekyj Oct 9, 2023
8ec4ff3
Add documentation and refractor variable for graytobin
quekyj Oct 9, 2023
40f5d3d
Fix markdown lint check
quekyj Nov 14, 2023
ca5faf4
use conditional type Logic
quekyj Nov 14, 2023
c8ea171
Use LogicValue reversed
quekyj Nov 14, 2023
cbe34bf
Add test on sequential bit should differ in one range
quekyj Nov 14, 2023
3f488ff
Fix code review
quekyj Dec 6, 2023
fda46df
remove the word map
quekyj Dec 6, 2023
078805d
fix error of getter gray
quekyj Jan 12, 2024
275e4dd
Merge branch 'main' into example/gray-to-binary
quekyj Jan 12, 2024
4e40d5c
Added missing header
quekyj Jan 12, 2024
f1540c9
use barrel export
quekyj Jan 12, 2024
ad65e70
Merge branch 'main' into example/gray-to-binary
quekyj Feb 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,4 @@ jobs:
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: doc/api
branch: docs
branch: docs
35 changes: 35 additions & 0 deletions doc/components/binary_gray.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Binary Gray

ROHD HCL provides a module to perform conversion on binary to gray and gray to binary.

## Binary-to-Gray

A fundamental digital logic operation known as binary to gray code conversion is employed in many different applications, notably in digital communication and signal processing systems. Gray code is a binary numbering system that is useful for minimizing communication channel faults because adjacent values only differ in one bit. This conversion algorithm converts a binary value into its equivalent Gray code representation from the input binary value. Each bit in the binary input is XORed with the bit before it to produce the Gray code. In order to reduce errors during signal transmission or processing, the transition between consecutive values must be as smooth as feasible in applications like rotary encoders, error correction, and circuit design.

The `BinaryToGrayConverter` module in ROHD-HCL accept a single input `binary` to be converted to gray of type `Logic`. The output value can be access via the getter `grayCode`.

An example is shown below to convert binary to gray code.

```dart
final binaryInput = Logic(name: 'binaryInput', width: 3)..put(bin('111'));
final binToGray = BinaryToGrayConverter(binaryInput);
await binToGray.build();

print(binToGray.grayCode.value.toString(includeWidth: false)); // output: 100
```

## Gray-to-Binary

The conversion of gray to binary code is an essential process in digital logic and communication systems. A binary numbering system called gray code is appropriate for use in applications where error reduction is crucial because adjacent values only differ by one bit. This conversion algorithm converts a Gray code value into its corresponding binary representation from the input. This is accomplished by computing the equivalent binary bit by XOR operations with the preceding bit after examining each bit in the Gray code, starting with the least significant bit. In many different domains, such as rotary encoders, digital signal processing, and error correction systems, it is necessary to extract precise binary representations from Gray code inputs. These applications include Gray to Binary Code Conversion.

The `GrayToBinary` module in ROHD-HCL accept a single input `gray` to be converted to binary of type `Logic`. The output value can be access via the getter `binaryVal`.

An example is shown below to convert gray code to binary value.

```dart
final graycode = Logic(name: 'grayCode', width: 3)..put(bin('100'));
final grayToBin = GrayToBinaryConverter(graycode);
await grayToBin.build();

print(grayToBin.binaryVal.value.toString(includeWidth: false)); // output: 111
```
1 change: 1 addition & 0 deletions lib/rohd_hcl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

export 'src/adder.dart';
export 'src/arbiter.dart';
export 'src/binary_gray.dart';
export 'src/carry_save_mutiplier.dart';
export 'src/exceptions.dart';
export 'src/fifo.dart';
Expand Down
152 changes: 152 additions & 0 deletions lib/src/binary_gray.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// binary_gray.dart
// A converter that can transform binary values into Gray code values or Gray
// code values into binary values.
//
// 2023 October 5
// Author: Yao Jing Quek <[email protected]>

import 'package:rohd/rohd.dart';

/// A module for converting binary values to Gray code representation.
///
/// The [BinaryToGrayConverter] class represents a module that takes a binary
/// value as input and produces the equivalent Gray code representation as
/// output. It internally uses the [binaryToGrayMap] function for the
/// conversion.
class BinaryToGrayConverter extends Module {
/// The Gray code representation output of the converter.
///
/// This [Logic] value represents the Gray code representation of the binary
/// input provided to the converter. It can be accessed using
/// the [grayCode] getter.
Logic get grayCode => output('gray_code');
quekyj marked this conversation as resolved.
Show resolved Hide resolved

/// Creates a [BinaryToGrayConverter] instance with the specified binary
/// input.
///
/// The [binary] parameter is the binary input that you want to convert
/// to Gray code. The width of the input [binary] determines the width
/// of the Gray code output.
BinaryToGrayConverter(Logic binary) {
final inputWidth = binary.width;
binary = addInput('binary_input', binary, width: inputWidth);
quekyj marked this conversation as resolved.
Show resolved Hide resolved
final grayVal = addOutput('gray_code', width: inputWidth);

Combinational([
Case(
binary,
[
for (var i = 0; i < (1 << inputWidth); i++)
CaseItem(Const(i, width: inputWidth), [
grayVal <
Const(
binaryToGrayMap(LogicValue.ofInt(i, inputWidth))
.swizzle(),
width: inputWidth)
])
],
conditionalType: ConditionalType.unique)
]);
}

/// Converts a binary value represented by [binary] to Gray code.
///
/// Given a [binary] value, this function returns a list of [LogicValue]
/// representing the equivalent Gray code.
///
/// For each bit in the [binary] input, starting from the least significant
/// bit (index 0), the function calculates the corresponding Gray code bit
/// based on XOR operation with the previous bit. The resulting Gray code
/// bits are returned in a list.
///
/// If [binary] has no bits, an empty list is returned.
///
/// Returns a list of [LogicValue] representing the Gray code.
List<LogicValue> binaryToGrayMap(LogicValue binary) {
quekyj marked this conversation as resolved.
Show resolved Hide resolved
quekyj marked this conversation as resolved.
Show resolved Hide resolved
final reverseBit = binary.reversed;
final binList = reverseBit.toList().asMap().entries.map((entry) {
final currentBit = entry.value;
final idx = entry.key;
if (idx == 0) {
return currentBit;
} else {
final previousIndex = reverseBit[idx - 1];
return currentBit ^ previousIndex;
}
});
return binList.toList();
}
}

/// A module for converting Gray code to binary representation.
///
/// The [GrayToBinaryConverter] class represents a module that takes a Gray
/// code value as input and produces the equivalent binary representation as
/// output. It internally uses the [grayToBinaryMap] function for the
/// conversion
class GrayToBinaryConverter extends Module {
/// The binary representation output of the converter.
///
/// This [Logic] value represents the binary representation of the Gray code
/// input provided to the converter. It can be accessed using the [binaryVal]
/// getter.
Logic get binaryVal => output('binary');
quekyj marked this conversation as resolved.
Show resolved Hide resolved

/// Creates a [GrayToBinaryConverter] instance with the specified Gray code
/// input.
///
/// The [gray] parameter is the Gray code input that you want to convert to
/// binary. The width of the input [gray] determines the width of the binary
/// output.
GrayToBinaryConverter(Logic gray) {
final inputWidth = gray.width;
gray = addInput('gray', gray, width: inputWidth);
final binaryVal = addOutput('binary', width: inputWidth);

Combinational([
Case(
quekyj marked this conversation as resolved.
Show resolved Hide resolved
gray,
[
for (var i = 0; i < (1 << inputWidth); i++)
CaseItem(Const(i, width: inputWidth), [
binaryVal <
Const(
grayToBinaryMap(LogicValue.ofInt(i, inputWidth))
.swizzle(),
width: inputWidth),
]),
],
conditionalType: ConditionalType.unique),
]);
}

/// Converts a Gray code value represented by [gray] to binary.
///
/// Given a [gray] value, this function returns a list of [LogicValue]
/// representing the equivalent binary representation.
///
/// For each bit in the [gray] input, starting from the least significant bit
/// (index 0), the function calculates the corresponding binary bit based
/// on XOR operation with the previous binary bit. The resulting binary bits
/// are returned in a list.
///
/// If [gray] has no bits, an empty list is returned.
///
/// Returns a list of [LogicValue] representing the binary representation.
List<LogicValue> grayToBinaryMap(LogicValue gray) {
quekyj marked this conversation as resolved.
Show resolved Hide resolved
final reverseGray = gray.reversed;
final grayList = reverseGray.toList();
var previousBit = LogicValue.zero;

final binaryList = grayList.map((currentBit) {
final binaryBit = currentBit ^ previousBit;
previousBit = binaryBit;
return binaryBit;
}).toList();

return binaryList;
}
}
122 changes: 122 additions & 0 deletions test/binary_gray_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// binary_gray_test.dart
// Tests for binary to gray and gray to binary conversion.
//
// 2023 October 5
// Author: Yao Jing Quek <[email protected]>
//

import 'dart:math';

import 'package:rohd/rohd.dart';
import 'package:rohd_hcl/rohd_hcl.dart';
import 'package:test/test.dart';

Future<void> main() async {
quekyj marked this conversation as resolved.
Show resolved Hide resolved
group('binary to gray code', () {
test('should return 100 if binary is 111 and width is 3.', () async {
final binaryInput = Logic(name: 'binaryInput', width: 3)..put(bin('111'));
final binToGray = BinaryToGrayConverter(binaryInput);
await binToGray.build();

expect(binToGray.grayCode.value.toString(includeWidth: false), '100');
});

test('should return 0 if binary is 0 and width is 1.', () async {
final binaryInput = Logic(name: 'binaryInput')..put(bin('0'));
final binToGray = BinaryToGrayConverter(binaryInput);
await binToGray.build();

expect(binToGray.grayCode.value.toInt(), 0);
});

test('should return 1 if binary is 1 and width is 1.', () async {
final binaryInput = Logic(name: 'binaryInput')..put(bin('1'));
final binToGray = BinaryToGrayConverter(binaryInput);
await binToGray.build();

expect(binToGray.grayCode.value.toInt(), 1);
});

test('should return 111110 if binary is 101011 and width is 6.', () async {
final binaryInput = Logic(name: 'binaryInput', width: 6)
..put(bin('101011'));
final binToGray = BinaryToGrayConverter(binaryInput);
await binToGray.build();

expect(binToGray.grayCode.value.toString(includeWidth: false), '111110');
});
});

group('gray code to binary', () {
test('should return 101 if gray code is 111 and width is 3.', () async {
final graycode = Logic(name: 'grayCode', width: 3)..put(bin('111'));
final grayToBin = GrayToBinaryConverter(graycode);
await grayToBin.build();

expect(grayToBin.binaryVal.value.toString(includeWidth: false), '101');
});

test('should return 0 if gray code is 0 and width is 1.', () async {
final grayCode = Logic(name: 'grayCode')..put(bin('0'));
final grayToBin = GrayToBinaryConverter(grayCode);
await grayToBin.build();

expect(grayToBin.binaryVal.value.toInt(), 0);
});

test('should return 1 if gray code is 1 and width is 1.', () async {
final grayCode = Logic(name: 'grayCode')..put(bin('1'));
final grayToBin = GrayToBinaryConverter(grayCode);
await grayToBin.build();

expect(grayToBin.binaryVal.value.toInt(), 1);
});

test('should return 101011 if gray code is 111110 and width is 6.',
() async {
final grayCode = Logic(name: 'grayCode', width: 6)..put(bin('111110'));
final grayToBin = GrayToBinaryConverter(grayCode);
await grayToBin.build();

expect(grayToBin.binaryVal.value.toString(includeWidth: false), '101011');
});

test(
'sequential values should differ in just one bit in integer and bigInt'
' bit range.', () async {
Future<void> checkBitDiff({required int width}) async {
for (var i = 0; i < pow(2, width) - 1; i++) {
final binaryInput1 = Logic(name: 'binaryInputSeq1', width: width)
..put(bin(i.toRadixString(2)));
final binaryInput2 = Logic(name: 'binaryInputSeq2', width: width)
..put(bin((i + 1).toRadixString(2)));

final binToGray1 = BinaryToGrayConverter(binaryInput1);
final binToGray2 = BinaryToGrayConverter(binaryInput2);

await binToGray1.build();
await binToGray2.build();

final gray1 = binToGray1.grayCode.value;
final gray2 = binToGray2.grayCode.value;

var diff = gray1 ^ gray2;

var setBits = 0;
while (diff.toInt() != 0) {
setBits++;
diff &= diff - 1;
}

expect(setBits, 1);
}
}

await checkBitDiff(width: 64);
await checkBitDiff(width: 100);
});
});
}