1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "mock_mmio_reg/mock_mmio_reg.h"
6 
mmio_fake_read(uintptr_t base,size_t size,zx_off_t off,void * value)7 void mmio_fake_read(uintptr_t base, size_t size, zx_off_t off, void* value) {
8     ZX_ASSERT(size == sizeof(uint8_t) ||
9               size == sizeof(uint16_t) ||
10               size == sizeof(uint32_t) ||
11               size == sizeof(uint64_t));
12 
13     ddk_mock::MockMmioRegRegion* mock_regs = reinterpret_cast<ddk_mock::MockMmioRegRegion*>(base);
14     ZX_ASSERT(mock_regs != nullptr);
15 
16     uint64_t value_64 = (*mock_regs)[off].Read();
17 
18     switch (size) {
19     case sizeof(uint8_t):
20         *reinterpret_cast<uint8_t*>(value) = static_cast<uint8_t>(value_64);
21         break;
22     case sizeof(uint16_t):
23         *reinterpret_cast<uint16_t*>(value) = static_cast<uint16_t>(value_64);
24         break;
25     case sizeof(uint32_t):
26         *reinterpret_cast<uint32_t*>(value) = static_cast<uint32_t>(value_64);
27         break;
28     case sizeof(uint64_t):
29         *reinterpret_cast<uint64_t*>(value) = value_64;
30         break;
31     }
32 }
33 
mmio_fake_write(uintptr_t base,size_t size,const void * value,zx_off_t off)34 void mmio_fake_write(uintptr_t base, size_t size, const void* value, zx_off_t off) {
35     ZX_ASSERT(size == sizeof(uint8_t) ||
36               size == sizeof(uint16_t) ||
37               size == sizeof(uint32_t) ||
38               size == sizeof(uint64_t));
39 
40     ddk_mock::MockMmioRegRegion* mock_regs = reinterpret_cast<ddk_mock::MockMmioRegRegion*>(base);
41     ZX_ASSERT(mock_regs != nullptr);
42 
43     switch (size) {
44     case sizeof(uint8_t):
45         (*mock_regs)[off].Write(*reinterpret_cast<const uint8_t*>(value));
46         break;
47     case sizeof(uint16_t):
48         (*mock_regs)[off].Write(*reinterpret_cast<const uint16_t*>(value));
49         break;
50     case sizeof(uint32_t):
51         (*mock_regs)[off].Write(*reinterpret_cast<const uint32_t*>(value));
52         break;
53     case sizeof(uint64_t):
54         (*mock_regs)[off].Write(*reinterpret_cast<const uint64_t*>(value));
55         break;
56     }
57 }
58