aboutsummaryrefslogtreecommitdiff
path: root/test/lt_bus_locked.cc
blob: f845b55f83cce7bb088249df69f53ca7e1673813 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "models/lt_bus.h"
#include "utils/log.h"

#include <tlm_utils/simple_initiator_socket.h>
#include <tlm_utils/simple_target_socket.h>

using sc_core::SC_NS;
using sc_core::sc_time;
using sc_core::sc_time_stamp;
using sc_core::SC_ZERO_TIME;

// -- TARGET -------------------------------------------------------------------

struct target : public sc_core::sc_module {
  explicit target(sc_core::sc_module_name nm) : sc_module(std::move(nm)) {
    p_sock.register_b_transport(this, &target::b_transport);
    p_sock.register_get_direct_mem_ptr(this, &target::get_direct_mem_ptr);
  }

  tlm_utils::simple_target_socket<target> p_sock{"sock"};

 private:
  void b_transport(tlm::tlm_generic_payload& tx, sc_core::sc_time&) {
    CLOGM(YELLOW, "transport 0x%llx w: %d r: %d", tx.get_address(),
          tx.is_write(), tx.is_read());

    // Miss-use byte enable len for sleep duration.
    if (u32 len = tx.get_byte_enable_length()) {
      CLOGM(YELLOW, "wait(%u)", len);
      wait(sc_time(len, SC_NS));
    }

    tx.set_response_status(tlm::TLM_OK_RESPONSE);
  }

  bool get_direct_mem_ptr(tlm::tlm_generic_payload&, tlm::tlm_dmi&) {
    return true;
  }
};

// -- INITIATOR ----------------------------------------------------------------

struct initiator : public sc_core::sc_module {
  explicit initiator(sc_core::sc_module_name nm) : sc_module(std::move(nm)) {
    SC_THREAD(run0);
    SC_THREAD(run1);
    SC_THREAD(run2);
  }

  tlm_utils::simple_initiator_socket<initiator> p_sock0{"sock0"};
  tlm_utils::simple_initiator_socket<initiator> p_sock1{"sock1"};
  tlm_utils::simple_initiator_socket<initiator> p_sock2{"sock2"};

 private:
  void setup_tx(tlm::tlm_generic_payload& tx, u64 addr, u32 len) const {
    tx.set_command(tlm::TLM_WRITE_COMMAND);
    tx.set_address(addr);
    tx.set_data_ptr(nullptr);
    tx.set_data_length(4);
    tx.set_byte_enable_ptr(nullptr);
    tx.set_byte_enable_length(len);
    tx.set_response_status(tlm::TLM_INCOMPLETE_RESPONSE);
    tx.set_dmi_allowed(false);
  }

  void send_tx(tlm_utils::simple_initiator_socket<initiator>& sock,
               u64 addr,
               u32 len,
               bool lock = false) {
    tlm::tlm_generic_payload tx;
    setup_tx(tx, addr, len);

    auto* bl = new bus_lock;
    bl->is_lock = lock;
    tx.set_extension(bl);

    CLOGM(CYAN, "[%s] ACCESS @0x%lx lock=%d", sock.name(), addr, lock);

    sc_core::sc_time t;
    sock->b_transport(tx, t);

    CLOGM(CYAN, "[%s] ACCESS ok: %d (%s)", sock.name(), tx.is_response_ok(),
          tx.get_response_string().c_str());
  }

  bool get_dmi(tlm_utils::simple_initiator_socket<initiator>& sock, u64 addr) {
    tlm::tlm_generic_payload tx;
    setup_tx(tx, addr, 1 /* len */);

    tlm::tlm_dmi dmi;
    return sock->get_direct_mem_ptr(tx, dmi);
  }

  void run0() {
    // Lock the bus.
    send_tx(p_sock0, 0x1000, 0, true);  // @0ns
    // Spend some time, keep the bus locked.
    wait(500, SC_NS);
    // Unlock the bus.
    send_tx(p_sock0, 0x1000, 0, false);  // @500ns
    assert(sc_time_stamp() == sc_time(500, SC_NS));

    // -- 500ns --

    wait(10, SC_NS);
    LOGM("------------------------------------------------------------");
    wait(90, SC_NS);

    // Waiting for bus lock, as run1 & run2 have currently an outstanding tx.
    assert(sc_time_stamp() == sc_time(600, SC_NS));
    send_tx(p_sock0, 0x1000, 0, true);  // @600ns
    assert(sc_time_stamp() == sc_time(750, SC_NS));
  }

  void run1() {
    wait(10, SC_NS);
    // No dmi while bus is locked.
    assert(get_dmi(p_sock1, 0x2000) == false);

    send_tx(p_sock1, 0x2000, 0);  // @10ns
    assert(sc_time_stamp() == sc_time(500, SC_NS));

    // -- 500ns --

    wait(50, SC_NS);
    send_tx(p_sock1, 0x2000, 100);  // @550ns
    assert(sc_time_stamp() == sc_time(650, SC_NS));
  }

  void run2() {
    wait(20, SC_NS);
    // No dmi while bus is locked.
    assert(get_dmi(p_sock2, 0x3000) == false);

    send_tx(p_sock2, 0x3000, 0);
    assert(sc_time_stamp() == sc_time(500, SC_NS));

    // -- 500ns --

    wait(50, SC_NS);
    send_tx(p_sock1, 0x3000, 200);  // @550ns
    assert(sc_time_stamp() == sc_time(750, SC_NS));
  }
};

// -- SC_MAIN ------------------------------------------------------------------

extern "C" int sc_main(int, char*[]) {
  lt_bus bus{"bus"};

  target target1{"target1"};

  initiator init1{"init1"};

  bus.attach_target(target1.p_sock, 0x0000, 0xffff);

  bus.attach_initiator(init1.p_sock0);
  bus.attach_initiator(init1.p_sock1);
  bus.attach_initiator(init1.p_sock2);

  bus.show_mmap();

  sc_core::sc_start();

  return 0;
}