Skip to content

Mburton/sc log #28

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 9 additions & 5 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Language: Cpp
BasedOnStyle: Google

ColumnLimit: 79
ColumnLimit: 120
UseTab: Never
IndentWidth: 4
IndentCaseLabels: false
Expand All @@ -17,19 +17,23 @@ AlignConsecutiveMacros: true
AlignConsecutiveAssignments: false

AllowAllArgumentsOnNextLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
AllowShortLambdasOnASingleLine: true

BreakConstructorInitializers: AfterColon
BreakConstructorInitializers: BeforeComma
BreakBeforeBinaryOperators: None
BreakStringLiterals: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterFunction: true
# PackConstructorInitializers: Never # When we have clang14

SpaceBeforeCtorInitializerColon: false
SpacesBeforeTrailingComments: 1
AlignTrailingComments: true

SortIncludes: false
SortUsingDeclarations: false
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/_build/
/.cproject
/.project
/.venv
/.vscode
13 changes: 5 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ set(GITHUB "https://github.com/" CACHE STRING "github base url")
include(FetchContent)
include(CTest)

FetchContent_Declare(
cpm-cmake
GIT_REPOSITORY ${GITHUB}cpm-cmake/CPM.cmake.git
GIT_SHALLOW True
GIT_TAG v0.31.1
file(
DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.40.5/CPM.cmake
${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake
)

FetchContent_MakeAvailable(cpm-cmake)
include(${cpm-cmake_SOURCE_DIR}/cmake/CPM.cmake)
include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)

cpmaddpackage("${GITHUB}TheLartians/[email protected]")

Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ endmacro()

run_test(smoke)
run_test(smoke_report)
run_test(smoke_logger)
44 changes: 44 additions & 0 deletions examples/audio_example/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
Language: Cpp
BasedOnStyle: Google

ColumnLimit: 120
UseTab: Never
IndentWidth: 4
IndentCaseLabels: false

PointerAlignment: Left
DerivePointerAlignment: false

AccessModifierOffset: -4

AlignAfterOpenBracket: Align
AlignConsecutiveMacros: true
AlignConsecutiveAssignments: false

AllowAllArgumentsOnNextLine: false
AllowShortFunctionsOnASingleLine: true
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
AllowShortLambdasOnASingleLine: true

BreakConstructorInitializers: BeforeComma
BreakBeforeBinaryOperators: None
BreakStringLiterals: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterFunction: true
# PackConstructorInitializers: Never # When we have clang14

SpaceBeforeCtorInitializerColon: false
SpacesBeforeTrailingComments: 1
AlignTrailingComments: true

SortIncludes: false
SortUsingDeclarations: false

Cpp11BracedListStyle: false
PenaltyBreakAssignment: 2000

ForEachMacros: ['SC_MODULE', 'SC_CTOR']
78 changes: 78 additions & 0 deletions examples/audio_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(audioexample VERSION 1.0 LANGUAGES CXX C)

set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to build all targets.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would not force global settings, rather set it for the target.


set(GITHUB "https://github.com/" CACHE STRING "github base url")

include(FetchContent)
include(CTest)

# download CPM.cmake
file(
DOWNLOAD
https://github.com/cpm-cmake/CPM.cmake/releases/download/v0.40.2/CPM.cmake
${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake
)
include(${CMAKE_CURRENT_BINARY_DIR}/cmake/CPM.cmake)

cpmaddpackage("${GITHUB}TheLartians/[email protected]")

cpmaddpackage(
NAME SystemCLanguage
GIT_REPOSITORY ${GITHUB}accellera-official/systemc.git
GIT_SHALLOW True
GIT_TAG 3.0.1
)

set(SYSTEMC_HOME ${SystemCLanguage_SOURCE_DIR})
include_directories(${SystemCLanguage_SOURCE_DIR}/src)
include_directories(${SystemCAMS_SOURCE_DIR}/src)
cpmaddpackage(
NAME SystemCAMS
GIT_REPOSITORY ${GITHUB}/Risto97/systemc-ams.git
GIT_SHALLOW True
GIT_TAG master
)
message("including ${SystemCAMS_SOURCE_DIR}/src")
target_include_directories(systemc-ams PRIVATE ${SystemCAMS_SOURCE_DIR}/src)


cpmaddpackage(
NAME SystemCCCI
GIT_REPOSITORY ${GITHUB}accellera-official/cci.git
GIT_SHALLOW True
GIT_TAG main
)

cpmaddpackage(
NAME SCP
GIT_REPOSITORY https://github.com/accellera-official/systemc-common-practices.git
GIT_TAG main
GIT_SHALLOW TRUE
)

cpmaddpackage(
NAME MidiParser
GIT_REPOSITORY https://github.com/MStefan99/Midi-Parser.git
GIT_TAG master
GIT_SHALLOW TRUE
)

file(DOWNLOAD
https://www.midiworld.com/midis/other/bach/bwv972.mid
${CMAKE_CURRENT_BINARY_DIR}/bwv972.mid
SHOW_PROGRESS
)

include_directories(${PORTAUDIO_INCLUDE_DIRS})
include_directories(include)
add_executable(${PROJECT_NAME}
src/main.cc
)

find_package(PkgConfig REQUIRED)
pkg_check_modules(PORTAUDIO REQUIRED IMPORTED_TARGET GLOBAL portaudio-2.0)
#include_directories(${PORTAUDIO_INCLUDE_DIRS})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?


target_link_libraries(${PROJECT_NAME} PUBLIC SystemC::systemc SystemC::cci systemc-ams scp::reporting PkgConfig::PORTAUDIO libParser)
75 changes: 75 additions & 0 deletions examples/audio_example/include/argparser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

/*
* Copyright (c) 2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#ifndef __ARG_PARSER_H__
#define __ARG_PARSER_H__
#include <getopt.h>
#include <systemc>
#include <cci_configuration>
#include <scp/report.h>

std::pair<std::string, std::string> get_key_val_args(const std::string& arg)
{
std::stringstream ss(arg);
std::string key, value;

if (!std::getline(ss, key, '=')) {
SCP_FATAL("Parser") << "parameter name not found!" << std::endl;
}
if (!std::getline(ss, value)) {
SCP_FATAL("Parser") << "parameter value not found!" << std::endl;
}

SCP_INFO("Parser") << "Setting param " << key << " to value " << value;

return std::make_pair(key, value);
}
void parseCommandLineWithGetOpt(const int argc, const char* const* argv)
{
SCP_INFO("Parser") << "Parse command line (" << argc << " arguments)";

// getopt permutes argv array, so copy it to argv_cp
std::vector<std::unique_ptr<char[]>> argv_cp;
char* argv_cp_raw[argc];
for (int i = 0; i < argc; i++) {
size_t len = strlen(argv[i]) + 1; // count \0
argv_cp.emplace_back(std::make_unique<char[]>(len));
strcpy(argv_cp[i].get(), argv[i]);
argv_cp_raw[i] = argv_cp[i].get();
}

// configure getopt
optind = 0; // reset of getopt
opterr = 0; // avoid error message for not recognized option

// Don't add 'i' here. It must be specified as a long option.
const char* optstring = "p:";

struct option long_options[] = { { "param", required_argument, 0, 'p' }, // --param foo.baa=10
{ 0, 0, 0, 0 } };

while (1) {
int c = getopt_long(argc, argv_cp_raw, optstring, long_options, 0);
if (c == EOF) break;

switch (c) {
case 'p': // -p and --param
{
auto param = get_key_val_args(std::string(optarg));
cci::cci_get_global_broker(cci::cci_originator("ArgumentParser"))
.set_preset_cci_value(param.first, cci::cci_value(cci::cci_value::from_json(param.second)));
break;
}

default:
/* unrecognized option */
break;
}
}
}

#endif
129 changes: 129 additions & 0 deletions examples/audio_example/include/async_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be part of CPS base instead of the example

* Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All Rights Reserved.
* Author: GreenSocs 2022
*
* SPDX-License-Identifier: BSD-3-Clause
*/

#ifndef ASYNC_EVENT
#define ASYNC_EVENT

#include <iostream>
#define SC_HAS_SUSPENDING
#define SC_HAS_ASYNC_ATTACH_SUSPENDING
#include <systemc>
#include <tlm>
#include <thread>
#include <mutex>

namespace gs {
class async_event : public sc_core::sc_prim_channel, public sc_core::sc_event
{
private:
sc_core::sc_time m_delay;
std::thread::id tid;
std::mutex mutex; // Belt and braces
bool outstanding = false;
enum { none, attach, detach } attach_pending_state = none;

public:
async_event(bool start_attached = true): outstanding(0)
{
tid = std::this_thread::get_id();
enable_attach_suspending(start_attached);
}

void async_notify() { notify(); }

void notify()
{
if (tid == std::this_thread::get_id()) {
sc_core::sc_event::notify();
} else {
notify(sc_core::SC_ZERO_TIME);
}
}
void notify(double d, sc_core::sc_time_unit u) { notify(sc_core::sc_time(d, u)); }
void notify(sc_core::sc_time delay)
{
if (tid == std::this_thread::get_id()) {
sc_core::sc_event::notify(delay);
} else {
mutex.lock();
m_delay = delay;
outstanding = true;
mutex.unlock();
async_request_update();
#ifndef SC_HAS_SUSPENDING
sc_core::sc_internal_async_wakeup();
#endif
}
}

void async_attach_suspending()
{
#ifndef SC_HAS_ASYNC_ATTACH_SUSPENDING
sc_core::async_attach_suspending(this);
#else
if (tid == std::this_thread::get_id()) {
this->sc_core::sc_prim_channel::async_attach_suspending();
} else {
mutex.lock();
attach_pending_state = attach;
mutex.unlock();
async_request_update();
}
#endif
}

void async_detach_suspending()
{
#ifndef SC_HAS_ASYNC_ATTACH_SUSPENDING
sc_core::async_detach_suspending(this);
#else
if (tid == std::this_thread::get_id()) {
this->sc_core::sc_prim_channel::async_detach_suspending();
} else {
mutex.lock();
attach_pending_state = detach;
mutex.unlock();
async_request_update();
}
#endif
}

void enable_attach_suspending(bool e) { e ? this->async_attach_suspending() : this->async_detach_suspending(); }

private:
void update(void)
{
mutex.lock();
// we should be in SystemC thread
if (outstanding) {
sc_event::notify(m_delay);
outstanding = false;
}
switch (attach_pending_state) {
case attach:
this->async_attach_suspending();
break;
case detach:
this->async_detach_suspending();
break;
default:
break;
}
attach_pending_state = none;
mutex.unlock();
}
void start_of_simulation()
{
// we should be in SystemC thread
if (outstanding) {
request_update();
}
}
};
} // namespace gs

#endif // ASYNC_EVENT
Loading
Loading