Created
January 12, 2016 22:03
-
-
Save nazarov-yuriy/c52922ce8b1ea91f6e6c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "config-host.h" | |
#include <sys/mman.h> | |
#include "hw/hw.h" | |
#include "hw/qdev.h" | |
#include "hw/isa/isa.h" | |
typedef struct FFTestdev { | |
ISADevice parent_obj; | |
MemoryRegion ioport; | |
uint32_t ioport_data; | |
} FFTestdev; | |
#define TYPE_TESTDEV "ff-testdev" | |
#define TESTDEV(obj) OBJECT_CHECK(FFTestdev, (obj), TYPE_TESTDEV) | |
static void test_ioport_write(void *opaque, hwaddr addr, uint64_t data, unsigned len) | |
{ | |
FFTestdev *dev = opaque; | |
int bits = len * 8; | |
int start_bit = (addr & 3) * 8; | |
uint32_t mask = ((uint32_t) -1 >> (32 - bits)) << start_bit; | |
dev->ioport_data &= ~mask; | |
dev->ioport_data |= data << start_bit; | |
} | |
static uint64_t test_ioport_read(void *opaque, hwaddr addr, unsigned len) | |
{ | |
FFTestdev *dev = opaque; | |
int bits = len * 8; | |
int start_bit = (addr & 3) * 8; | |
uint32_t mask = ((uint32_t) -1 >> (32 - bits)) << start_bit; | |
return (dev->ioport_data & mask) >> start_bit; | |
} | |
static const MemoryRegionOps test_ioport_ops = { | |
.read = test_ioport_read, | |
.write = test_ioport_write, | |
.endianness = DEVICE_LITTLE_ENDIAN, | |
}; | |
static void ff_testdev_realizefn(DeviceState *d, Error **errp) | |
{ | |
ISADevice *isa = ISA_DEVICE(d); | |
FFTestdev *dev = TESTDEV(d); | |
MemoryRegion *io = isa_address_space_io(isa); | |
memory_region_init_io(&dev->ioport, OBJECT(dev), &test_ioport_ops, dev, "ff-testdev-ioport", 4); | |
memory_region_add_subregion(io, 0xe0, &dev->ioport); | |
} | |
static void ff_testdev_class_init(ObjectClass *klass, void *data) | |
{ | |
DeviceClass *dc = DEVICE_CLASS(klass); | |
set_bit(DEVICE_CATEGORY_MISC, dc->categories); | |
dc->realize = ff_testdev_realizefn; | |
} | |
static const TypeInfo ff_testdev_info = { | |
.name = TYPE_TESTDEV, | |
.parent = TYPE_ISA_DEVICE, | |
.instance_size = sizeof(FFTestdev), | |
.class_init = ff_testdev_class_init, | |
}; | |
static void ff_register_types(void) | |
{ | |
type_register_static(&ff_testdev_info); | |
} | |
type_init(ff_register_types) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment