1 // Copyright 2017 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 <hwreg/internal.h>
6 #include <stdio.h>
7 
8 namespace hwreg {
9 
10 namespace internal {
11 
Print(uint64_t value,char * buf,size_t len) const12 void FieldPrinter::Print(uint64_t value, char* buf, size_t len) const {
13     unsigned num_bits = bit_high_incl_ - bit_low_ + 1;
14     uint64_t mask = internal::ComputeMask<uint64_t>(num_bits);
15     uint64_t val = static_cast<uint64_t>((value >> bit_low_) & mask);
16 #ifdef _KERNEL
17     // The kernel does not support the * directive in printf, so we lose out on
18     // the length-matching padding.
19     snprintf(buf, len, "%s[%u:%u]: 0x%" PRIx64 " (%" PRIu64 ")", name_,
20              bit_high_incl_, bit_low_, val, val);
21 #else
22     int pad_len = (num_bits + 3) / 4;
23     snprintf(buf, len, "%s[%u:%u]: 0x%0*" PRIx64 " (%" PRIu64 ")", name_,
24              bit_high_incl_, bit_low_, pad_len, val, val);
25 #endif // _KERNEL
26     buf[len - 1] = 0;
27 }
28 
PrintRegisterPrintf(FieldPrinter fields[],size_t num_fields,uint64_t reg_value,uint64_t fields_mask,int register_width_bytes)29 void PrintRegisterPrintf(FieldPrinter fields[], size_t num_fields,
30                          uint64_t reg_value, uint64_t fields_mask,
31                          int register_width_bytes) {
32     PrintRegister([](const char* arg) { printf("%s\n", arg); },
33                   fields, num_fields, reg_value, fields_mask, register_width_bytes);
34 }
35 
36 } // namespace internal
37 
38 } // namespace hwreg
39