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

rohd-hcl: find #37

Merged
merged 22 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions lib/rohd_hcl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
export 'src/arbiter.dart';
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
export 'src/exceptions.dart';
export 'src/fifo.dart';
export 'src/find.dart';
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
export 'src/interfaces/interfaces.dart';
export 'src/memory.dart';
export 'src/one_hot.dart';
Expand Down
154 changes: 154 additions & 0 deletions lib/src/find.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// fifo.dart
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
// Implementation of FIFOs.
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
//
// 2023 March 13
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
// Author: Rahul Gautham Putcha <[email protected]>
//

import 'dart:math';

import 'package:rohd/rohd.dart';
import 'package:rohd_hcl/rohd_hcl.dart';
import 'package:rohd_hcl/src/utils.dart';

/// Doc coming soon...
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
class FindFirstOne extends Module {
/// Doc coming soon...
late Logic _output;

/// Doc coming soon...
Logic get index => _output;

/// Doc coming soon... First 1 from the least significan (right)
FindFirstOne(Logic bus) {
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
final oneHotList = <Logic>[];
for (var i = 0; i < bus.width; i++) {
oneHotList.add(bus[i] & ~bus.getRange(0, i).or());
}

final bin = OneHotToBinary(oneHotList.rswizzle()).binary;
_output = addOutput('findFirstOne', width: bin.width);
_output <= bin;
}
}

/// Doc coming soon...
class FindFirstZero extends Module {
/// Doc coming soon...
late Logic _output;

/// Doc coming soon...
Logic get index => _output;

/// Doc coming soon... First 1 from the least significan (right)
FindFirstZero(Logic bus) {
final oneHotList = <Logic>[];
for (var i = 0; i < bus.width; i++) {
if (i == 0)
oneHotList.add(~bus[i]);
else
oneHotList.add(~bus[i] & bus.getRange(0, i).and());
}

final bin = OneHotToBinary(oneHotList.rswizzle()).binary;
_output = addOutput('findFirstZero', width: bin.width);
_output <= bin;
}
}

/// Doc coming soon...
class FindNthOne extends Module {
/// Doc coming soon...
late Logic _output;

/// Doc coming soon...
Logic get index => _output;

/// Doc coming soon... First 1 from the least significan (right)
FindNthOne(Logic bus, Logic n) {
final oneHotList = <Logic>[];
for (var i = 0; i < bus.width; i++) {
final oneCount = Count(bus.getRange(0, i + 1));

var paddedNValue = n;
var paddedCountValue = oneCount.index;
if (n.width < oneCount.index.width) {
paddedNValue = n.zeroExtend(oneCount.index.width);
} else {
paddedCountValue = oneCount.index.zeroExtend(n.width);
}

// If `bus[i]` is a `1` and the number of `1`'s from index 0 to `i` is `n`
oneHotList.add(bus[i] & paddedCountValue.eq(paddedNValue));
}

final bin = OneHotToBinary(oneHotList.rswizzle()).binary;
_output = addOutput('findNthOne', width: bin.width);
_output <= bin;
}
}

/// Doc coming soon...
class FindNthZero extends Module {
/// Doc coming soon...
late Logic _output;

/// Doc coming soon...
Logic get index => _output;

/// Doc coming soon... First 1 from the least significan (right)
FindNthZero(Logic bus, Logic n) {
final oneHotList = <Logic>[];
for (var i = 0; i < bus.width; i++) {
if (i == 0) {
oneHotList.add(~bus[i] & n.eq(0));
} else {
final zeroCount = Count(bus.getRange(0, i), countOne: false);

var paddedNValue = n;
var paddedZeroCountValue = zeroCount.index;
if (n.width < zeroCount.index.width) {
paddedNValue = n.zeroExtend(zeroCount.index.width);
} else {
paddedZeroCountValue = zeroCount.index.zeroExtend(n.width);
}

// If `bus[i]` is a `0` and the number of `0`'s from index 0 to `i` is `n`
oneHotList.add(~bus[i] & paddedZeroCountValue.eq(paddedNValue));
}
}

final bin = OneHotToBinary(oneHotList.rswizzle()).binary;
_output = addOutput('findNthOne', width: bin.width);
_output <= bin;
}
}

/// Doc coming soon...
class Count extends Module {
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
/// Doc coming soon...
late Logic _output;

/// Doc coming soon...
Logic get index => _output;

/// Doc coming soon... First 1 from the least significan (right)
Count(Logic bus, {bool countOne = true}) {
Logic count = Const(0, width: max(1, log2Ceil(bus.width)));
for (var i = 0; i < bus.width; i++) {
count += bus[i].zeroExtend(count.width);
}
_output =
addOutput('count${countOne ? "One" : "Zero"}', width: count.width);

_output <=
(countOne
// count one
? count
// Count zero by removing one's from bus width
: Const(bus.width, width: count.width) - count);
}
}
52 changes: 52 additions & 0 deletions test/find_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (C) 2023 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
//
// find_test.dart
// Tests for Find
//
// 2023 June 8
// Author: Max Korbel <[email protected]>
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
//

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

void main() {
mkorbel1 marked this conversation as resolved.
Show resolved Hide resolved
test('count all 1s', () {
final bus = Const(bin('01101'), width: 5);
final mod = Count(bus);
expect(mod.index.value.toInt(), 3);
});

test('count all 0s', () {
final bus = Const(bin('001101'), width: 6);
final mod = Count(bus, countOne: false);
expect(mod.index.value.toInt(), 3);
});

test('find first one', () {
final bus = Const(bin('0111000100'), width: 10);
final mod = FindFirstOne(bus);
expect(mod.index.value.toInt(), 2);
});

test('find nth one', () {
final bus = Const(bin('10110'), width: 5);
final mod = FindNthOne(bus, Const(3, width: log2Ceil(5)));
expect(mod.index.value.toInt(), 4);
});

test('find first zero', () {
final bus = Const(bin('0111011111'), width: 10);
final mod = FindFirstZero(bus);
expect(mod.index.value.toInt(), 5);
});

test('find nth zero', () {
final bus = Const(bin('0111001010'), width: 10);
final mod = FindNthZero(bus, Const(3, width: log2Ceil(10)));
expect(mod.index.value.toInt(), 5);
});
}