簡單記錄一下,以免要用時忘了:
// pci.h extern "C" DWORD ReadPCI_DWORD(unsigned long addr);
數位生活筆記 - 免費軟體 、 程式設計 、 網站架設 、 技巧應用
簡單記錄一下,以免要用時忘了:
// pci.h extern "C" DWORD ReadPCI_DWORD(unsigned long addr);
在使用 Synology Drive/CloudStation 同步時,如果本地端檔案屬性為唯讀時將造成同步失敗。
ClearRO 是我寫的一個在背景中 (tray) 執行的小程式,用來處理 Drive/CloudStation 同步因為唯讀造成失敗的問題。下載後直接執行即可,不需安裝。
Ubuntu 18.04
通用型 I/O module source code (Makefile, giodrv.h, giodrv.c),測試程式顯示 Super I/O temperature sensor 温度 (Fintek F71889A):
# Makefile KVERSION := $(shell uname -r) obj-m := giodrv.o all: $(MAKE) -C /lib/modules/$(KVERSION)/build M=${PWD} modules clean: $(MAKE) -C /lib/modules/$(KVERSION)/build M=${PWD} clean
// gpio.h #ifndef GPIO_H_ #define GPIO_H_ int gpio_is_requested(unsigned int gpio); int gpio_request(unsigned int gpio); int gpio_free(unsigned int gpio); int gpio_direction_input(unsigned int gpio); int gpio_direction_output(unsigned int gpio, int value); int gpio_get_value(unsigned int gpio); int gpio_set_value(unsigned int gpio, int value); #endif
DOS 下使用 Flat mode 讀寫 4G memory 的 sample code。
所謂 flat mode 就是在主程式執行在 Real Mode 下,可是卻能讀寫 4G memory space的特殊模式,要進入 flat mode 也很簡單,首先先取得 gdt_table 的線性位址 (linear address),將 linear address 填入 gdtr offset 欄位,使用 lgdt 指令載入 gdt table,再來將 cr0 register bit0 設為1進入 protect mode,然後 selector (fs, fs, es, ss…) 選擇 gdt table 中的 data group,再清掉 cr0 register bit0 返回 real mode,以後要讀寫 4G memory space 時就直接使用 fs:[address] 來讀寫即可。
要編譯要先安裝 build-essential 與 libelf-dev 套件, build-essential 包含了編譯 C / C++ 所需的套件,lebelf-dev 則包含編譯 driver (kernel module) 所需的程式。
$ sudo apt-get update $ sudo apt-get install build-essential $ sudo apt-get install libelf-dev
// debug_card.h #ifndef _DEBUG_CARD_H_ #define MSG(format, arg...) printk(KERN_INFO "DEBUG CARD: " format "\n", ## arg) #include <linux/ioctl.h> #define DEV_MAJOR 121 #define DEV_NAME "debug" #define DEV_IOCTLID 0xD0 #define IOCTL_WRITE _IOW(DEV_IOCTLID, 10, int) #define IOCTL_RESET _IOW(DEV_IOCTLID, 0, int) #endif
兩個由 Turbo C++ 寫的在 DOS 下讀寫 DMI 的 sample code。
Sample code 1:這是使用搜尋方法尋找記憶體中 BIOS 區域 (F000:0000 ~ F000:FFFF) 中的 SMBIOS 與 DMI table來讀取 DMI 訊息的方法,這個方法只能讀取無法改寫 DMI 內容。
在 UAC 開啟的情況下,要開機自動執行需管理管權限的(requireAdministrator) 的程式,必需使用 Windows 的排程設定(schtasks)來啟動程式,才不會在開機時出現 UAC 要求權限的視窗。
範例:
void RegAutoRun(bool cmd) { if (cmd == 1) { //加入啟動排程 CString str; str.Format(L"/Create /F /TN \"AppName\" /SC ONLOGON /RL HIGHEST /TR \"\\\"%s\\App.EXE\\\"\"", GetLaunchDir()); ShellExecute(NULL, _T("open"), _T("schtasks.exe"), str, _T(""), 0); } else { //從排程中移除 ShellExecute(NULL, _T("open"), _T("schtasks.exe"), _T("/Delete /F /TN \"AppName\""), _T(""), 0); } }
typedef 傳回值 (*函式指標名稱) (傳遞參數) ;
typedef int (*PFUN) (int x, int y); int ADD(int x, int y) { return x+y; } int SUB(int x, int y) { return x-y; } main() { int n; PFUN pf = NULL; pf = ADD; n = pf(10,5); //n=15 pf = SUB; n = pf(10, 5); //n=5 }
compiler為了程式的執行效率,大都會對程式進行最佳化的動作,在結構中則會進行資料對齊以加快執行速度,作業系統、Compiler、CPU都影響資料對齊的邊界 (boundary alignment)。
Wiki – Data structure alignment 說明
來看看這個例子:
struct { char c1; //1 bytes char c2; //1 bytes int i1; //4 bytes char c3; //1 bytes }S;